bookjs
code:script.js
javascript:(function(){
var p = document.getElementById("productTitle");//書籍のタイトルの処理
if (!p) var p = document.getElementById("ebooksProductTitle");
var title = window.prompt('Scrap "Amazon" to your scrapbox.', p.innerText.trim());
if (!title) return;
title = '『'+ title +'』';
var asin = document.getElementById('ASIN');//ASIN番号の処理
if(asin){
var a = 'ISBN:' + asin.value;
}else{
var asin = document.getElementsByName('ASIN.0')0,a = 'ASIN:' + asin.value; }
var detail = document.getElementById('detailBullets_feature_div');//出版社と出版年月の処理
if (!detail) {
var subdoc = document.getElementById("product-description-iframe").contentWindow.document;
var detail = subdoc.getElementById("productDetailsTable");
}
var detailtext = detail.innerText;
var pubdata = detailtext.match(/(出版社 : .+) (\(.+\))/);//1出版社:シーアンドアール研究所,2(2018/7/27) if (pubdata){
pubdata1 = pubdata1.replace(/: /,':[');//出版社名をリンクにしないならこの2行は削除する pubdata1 = (pubdata1.match(/;/)?pubdata1.replace(/;/,'];'):pubdata1.trim() + ']'); //pubdata2 = pubdata2 + ' ';//リンクなし //pubdata2 = pubdata2.replace(/\((\d+)\//, '($1/') + ' ';//年をリンクに pubdata2 = pubdata2.replace(/\((\d+\/\d+)\//, '($1/') + ' ';//年月をリンクに }else{
}
//内容紹介の処理
//bookDescription_feature_div
const isbookDesc = document.getElementById("bookDescription_feature_div")
if(isbookDesc){
if (isbookDesc.innerText == ""){
const eDiv = document.getElementById("editorialReviews_feature_div")
if (eDiv) d1 = eDiv.innerText.replace(/\n/g,"\n>")
}else{
var d1 = isbookDesc.innerText.replace(/\n/g,"\n>").replace("続きを読む","")
}
}else{
const probookDesc = document.getElementById("productDescription_feature_div")
if (probookDesc){
var decsdocP = document.getElementsByClassName("pInfoTabCExpander-content");
var d1 = decsdoc.innerText.replace(/\n/g,"\n>")
}
}
//書影の処理
var image=document.getElementById("landingImage");
if (!image) var image = document.getElementById("ebooksImgBlkFront");
var imageurl = image.getAttribute("src");
//著者情報の処理
var pub = [];
var c = document.getElementsByClassName('author');
for (g = 0; g < c.length ;g++){
var at = cg.innerText.replace(/\r?\n/g, '').replace(/,/,''); var pu = at.match(/\(.+\)/);
var ct = at.replace(/\(.+\)/,'').replace(/ /g,'');
pub.push(pu + ' + ct.trim() + '');
}
var body = encodeURIComponent(lines);
})();
出典
code:ver2.js
(function () {
// setting内を書き換えてください。
const setting = {
/** Scrapboxのプロジェクト名を記述する */
projectName: 'puartya-books',
/** 本文の書式を記述する(各要素を<>で囲む) */
/** ページタイトルの書式を記述する */
pageTitle: '『<書名>』',
}
// ========================================================
try {
const href = window.location.href;
// Amazonでなければ無効
if (!href.includes('www.amazon.co.jp') || !href.includes('/dp/')) return alert('Amazonの書籍ページではありません。');
// 自分のScrapboxのプロジェクトURLを指定
const PROJECT_NAME = setting.projectName || window.prompt('プロジェクトURL');
if (!PROJECT_NAME) return alert('処理を中断しました。');
// 書名の欄を取得する
const titleElm = document.getElementById('productTitle');
if (!titleElm) throw new Error('タイトルのelement指定が誤っています。');
// 取得した書名をデフォルト値としてページタイトルを決める
const titleContent = titleElm.textContent || '';
const pageTitle = window.prompt('以下を書名として取得します。必要があれば書き換えてください。\nなお選択範囲があれば引用書式でスクラップします。', titleContent.trim());
// タイトルが空欄またはキャンセルを選択の場合は処理を終了
if (!pageTitle) return alert('キャンセルしました。');
// 情報を格納するMapを用意する
const map = new Map();
map.set('URL', href);
map.set('書名', pageTitle);
map.set('書名補足', (() => {
// サブタイトル(書名直後の、バージョンと発売日が書いてある部分)を取得する
const elm = document.getElementById('productSubtitle');
if (!elm) console.error('サブタイトルの要素指定が誤っています。');
return elm ? elm.textContent.trim() : '';
})());
map.set('選択範囲', (() => {
// 選択範囲を取得する
const getSelection = window.getSelection(); // 選択範囲を文字列として取得
if (getSelection && getSelection.toString()) { // 選択範囲があるとき
let text = getSelection.toString();
text = text.replace(/(\W+)( )(\W+)/g, '$1$3'); // 字間に時々紛れている半角スペースを除去
text = text.replace(/(^|\n)/g, '$1> '); // 各行に「> 」を追加
return text;
} else {
return '';
}
})());
map.set('書影', (() => {
// 書影を取得する
const image = document.getElementById('landingImage');
if (!image) console.error('書影の要素指定が誤っています。')
return image ? image.getAttribute('src') : '';
})());
map.set('著者', (() => {
// 著者を取得する
const authors = [];
const elms = document.getElementsByClassName('author');
for (const elm of elms) {
if (!elm.textContent) continue;
const content = elm.textContent.replace(/\r?\n|\t/g, '').replace(/,/,
'');
const category = content.match(/\(.+\)/); // (著)などの部分
const authorName = content.replace(/\(.+\)/, '').replace(/ /g, '');
authors.push({
category, name: [${authorName}]
});
}
return authors.map(obj => obj.category + obj.name).join('/');
})());
// 登録情報を取得する
const container = document.getElementById('detailBullets_feature_div');
if (container) {
const items = container.querySelectorAll('.a-list-item');
items.forEach(item => {
const span = Array.from(item.querySelectorAll('span'));
if (!span.length || span.length > 2) return; // 条件に合わない要素はスキップ
try {
const key = span0.textContent.match(/^^\s*/).toString(); const value = span1.textContent; map.set(key, value);
} catch (e) {
return;
}
})
} else {
console.error('登録情報の指定が誤っています。');
}
console.log(map);
const title = setting.pageTitle.replace(/<(.+?)>/g, (match, target) => map.get(target) || '');
const body = setting.format.replace(/<(.+?)>/g, (match, target) => map.get(target) || '');
// エンコードしてURLをつくる
const url = https://scrapbox.io/${PROJECT_NAME}/${encodeURIComponent(title)}?body=${encodeURIComponent(body)};
// URLを開いてページを作成(または同名のページに追記)
window.open(url);
} catch (err) {
console.error(err);
const p = prompt(
'Amazonの仕様変更によりブックマークレットが無効になっています。\nお手数ですが以下のページで更新をご確認ください。\n「OK」でページを開きます。',
url
);
if (p) window.open(url);
return;
}
})();