書誌情報を取り込むブックマークレット
やり方
このページをブラウザの「お気に入り」(ブックマーク)に登録する
登録したブックマークを編集する(リンク先を変更する)
Firefoxの場合はこんな感じ
https://gyazo.com/e122999e862fd00dc57a96a856d40d3d
もともとあったURLはまるっと削除して、代わりに以下のコードを貼り付ける
javascript:~(document) 部分まで
ブックマークレット用のコード
code:sample.js
javascript:(function(d,s){s=d.createElement('script');s.src='https://scrapbox.io/api/code/BCBookReadingCircle/%E6%9B%B8%E8%AA%8C%E6%83%85%E5%A0%B1%E3%82%92%E5%8F%96%E3%82%8A%E8%BE%BC%E3%82%80%E3%83%96%E3%83%83%E3%82%AF%E3%83%9E%E3%83%BC%E3%82%AF%E3%83%AC%E3%83%83%E3%83%88/script.js';d.body.appendChild(s);})(document)
goryugo.iconここまでで設定は完了
あとは、Amazonの取り込みたい本のページで、先ほどのブックマークを押す。
タイトルを尋ねられるので、問題なければそのままOKで。
こういうページができる。
https://gyazo.com/dc243228648f76b9a4d8edc9731bc43d
ブックマークレットによって動作するコード本体
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番号の処理
//<20230108 ISBNにASINが入力される問題
//if(asin){
// var a = 'ISBN:' + asin.value;
//}else{
// var asin = document.getElementsByName('ASIN.0')0,a = 'ASIN:' + asin.value;
//}
if(asin.value.match(/B.+/)){
var a = 'ASIN:' + asin.value;
}else{
var a = 'ISBN:' + asin.value;
}
//20230108>
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;
//<20230108出版社のデータ取得の正規表現見直し
//var pubdata = detailtext.match(/(出版社 : .+) (\(.+\))/);//1出版社:シーアンドアール研究所,2(2018/7/27)
var pubdata = detailtext.match(/(出版社.+: .+) (\(.+\))/);//1出版社:シーアンドアール研究所,2(2018/7/27)
if (pubdata){
//pubdata1 = pubdata1.replace(/: /,':[');//出版社名をリンクにしないならこの2行は削除する
pubdata1 = pubdata1.replace(/:./,':[');//出版社名をリンクにしないならこの2行は削除する
pubdata1 = (pubdata1.match(/;/)?pubdata1.replace(/;/,'];'):pubdata1.trim() + ']');
//pubdata2 = pubdata2 + ' ';//リンクなし
//pubdata2 = pubdata2.replace(/\((\d+)\//, '($1/') + ' ';//年をリンクに
//pubdata2 = pubdata2.replace(/\((\d+\/\d+)\//, '($1/') + ' ';//年月をリンクに
pubdata2 = pubdata2.replace(/\((\d+\/\d+)\//, '($1/') + ' \n';//年月をリンクに
//20230108>
}else{
var pubdata = '','','';
}
//内容紹介の処理
//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 decsdoc = decsdocP0;
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 lines=''+imageurl+' '+window.location.href+'\n' + pub.join(' ') + '\n' + pubdata1 + pubdata2 + a + '\n>' + d1 + '\n#書籍名\n';//ページへの書き込み内容。ここで順番を変えればページ内容も変わります。
var body = encodeURIComponent(lines);
window.open('https://scrapbox.io/BCBookReadingCircle/'+encodeURIComponent(title.trim())+'?body='+body)
})();
2022/6/17rashita.icon微妙に修正。
2022/7/10rashita.icon微妙に修正。
出版日が取得できていなかったので、その部分をfix
2023/1/08mskizm.icon微妙に修正。
出版社と出版日の取得、ASINの場合でもISBNになっていた部分をfix
&rlm; ってマッチングする方法あるのかしらん
2023/3/30mskizm.icon微妙に修正
出版社のマッチが変更していた部分をfix
2023/9/30rashita.icon微妙に修正
Amazonの書影imgタグのIDが変更されていたので、それに対応
2024/1/13rashita.icon微妙に修正
Amazonの内容紹介のDiv構成が替わっていたので、それに対応
ブックマークレットからページを作成したときのamazonリンクに、ブックカタリストのアフィリエイトタグを付与するようにして頂くのもありかもと思いましたtakkii.icon
rashita.iconなるほど。
TsutomuZ.icon 2024/01/26
説明箇所が
undefined
になってしまいます。お時間があれば修正をお願いします。
rashita.icon修正しておきました。たぶん大丈夫だと思います。
TsutomuZ.icon ありがとうございます。OKです。2024/02/06
2025/5/1
出版社の情報が取得できなくなっていたので修正。
code:script_min.js
javascript:(function(){var p=document.getElementById("productTitle");if(!p)p=document.getElementById("ebooksProductTitle");var t=window.prompt('Scrap "Amazon" to your scrapbox.',p.innerText.trim());if(!t)return;t='%E3%80%8E'+t+'%E3%80%8F';var a=document.getElementById('ASIN');if(a){var i='ISBN:'+a.value;}else{a=document.getElementsByName('ASIN.0')0,i='ASIN:'+a.value;}var d=document.getElementById('detailBullets_feature_div');if(!d){var s=document.getElementById("product-description-iframe").contentWindow.document;d=s.getElementById("productDetailsTable");}var dd=d.querySelector("ul li span");var l=dd.innerText;console.log(l);var u=l.match(/(%E5%87%BA%E7%89%88%E7%A4%BE : .+) (\(.+\))/);if(u){u1=u1.replace(/: /,":[");u1=(u1.match(/;/)?u1.replace(/;/,"];"):u1.trim()+"]");u2=u2.replace(/\((\d+\/\d+)\//,"($1/")+" ";}else{u='','','';}var b=document.getElementById("bookDescription_feature_div");if(b){if(b.innerText==""){var e=document.getElementById("editorialReviews_feature_div");if(e)c=e.innerText.replace(/\n/g,"\n>");}else{var c=b.innerText.replace(/\n/g,"\n>").replace("%E7%B6%9A%E3%81%8D%E3%82%92%E8%AA%AD%E3%82%80","");}}else{var o=document.getElementById("productDescription_feature_div");if(o){var n=document.getElementsByClassName("pInfoTabCExpander-content");var c=n0.innerText.replace(/\n/g,"\n>");}}var m=document.getElementById("landingImage");if(!m)m=document.getElementById("ebooksImgBlkFront");var r=m.getAttribute("src");var v=[];var g=document.getElementsByClassName('author');for(h=0;h<g.length;h++){var y=gh.innerText.replace(/\r?\n/g,'').replace(/,/,'');var q=y.match(/\(.+\)/);var j=y.replace(/\(.+\)/,'').replace(/ /g,'');v.push(q+' '+j.trim()+'');}var w=''+r+' '+window.location.href+'\n'+v.join(' ')+'\n'+u1+u2+i+'\n>'+c+'\n#%E6%9B%B8%E7%B1%8D%E5%90%8D\n';var%20k=encodeURIComponent(w);window.open('https://scrapbox.io/rashitaobj/'+encodeURIComponent(t.trim())+'?body='+k);})();
→書誌情報登録のブックマークが動かない場合