Scrapboxのカスタマイズ

scrapbox

Web上を検索していて見つけたScrapbox向け便利機能。試してみました。

何かというとAmazonの書誌情報をワンクリックでScrapboxに引っ張ってくる機能(エクセレント!)

ISBNや出版社の情報も取り込むブックマークレットからの引用です。

Contents

    ブックマークレットを作成する

    まずはスクリプトの準備。前述の倉下氏のサイトより最新版を取得してきました。

     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){
           	pubdata[1] = pubdata[1].replace(/: /,':[');//出版社名をリンクにしないならこの2行は削除する
           	pubdata[1] = (pubdata[1].match(/;/)?pubdata[1].replace(/;/,'];'):pubdata[1].trim() + ']');
            //pubdata[2] = pubdata[2] + ' ';//リンクなし
            //pubdata[2] = pubdata[2].replace(/\((\d+)\//, '([$1]/') + ' ';//年をリンクに
            pubdata[2] = pubdata[2].replace(/\((\d+\/\d+)\//, '([$1]/') + ' ';//年月をリンクに
        }else{
        	var pubdata = ['','',''];
        }
        var isbookDesc = document.getElementById("bookDescription_feature_div") != null
       	if (isbookDesc){
        	var decsdocP = document.getElementById("bookDescription_feature_div");//内容紹介の処理
        	var decsdoc = decsdocP.firstElementChild;
        	if (decsdoc){
         	var d = decsdoc.firstElementChild;
          		if (d){//内容紹介が存在しているなら
          			var d1 = d.innerText.replace(/\n/g,'\n>');
          		}else{
          			var d1 = "";//内容紹介が空っぽの場合
          		}
         	}else{
         	var d1 = "";//内容紹介が空っぽの場合
        		}
        }
    	
        var image=document.getElementById("imgBlkFront");//書影の処理
        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 = c[g].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' + pubdata[1] +  pubdata[2] + a + '\n>' + d1 + '\n#書籍名\n';//ページへの書き込み内容。ここで順番を変えればページ内容も変わります。
        var body = encodeURIComponent(lines);
        window.open('https://scrapbox.io/rashitaobj/'+encodeURIComponent(title.trim())+'?body='+body)
     })();

    ※最後のwindow.open以下のURLは指定のURLに置き換えます。

    window.open('https://scrapbox.io/rashitaobj/'+encodeURIComponent(title.trim())+'?body='+body)

    Minifyバージョンも用意されています。ブックマークレットを登録する時はMinifyバージョンを使うとスッキリしますね。

    javascript:(function(){var c=document.getElementById("productTitle");c||(c=document.getElementById("ebooksProductTitle"));if(c=window.prompt('Scrap "Amazon" to your scrapbox.',c.innerText.trim())){c="\u300e"+c+"\u300f";var b=document.getElementById("ASIN");b?b="ISBN:"+b.value:(b=document.getElementsByName("ASIN.0")[0],b="ASIN:"+b.value);var a=document.getElementById("detailBullets_feature_div");a||(a=document.getElementById("product-description-iframe").contentWindow.document.getElementById("productDetailsTable"));
    (a=a.innerText.match(/(\u51fa\u7248\u793e : .+)(\(.+\))/))?(a[1]=a[1].replace(/:/,":["),a[1]=a[1].match(/;/)?a[1].replace(/;/,"];"):a[1]+"]",a[2]=a[2].replace(/\((\d+\/\d+)\//,"([$1]/")+" "):a=["","",""];if(null!=document.getElementById("bookDescription_feature_div")){var d=document.getElementById("bookDescription_feature_div").firstElementChild.firstElementChild;d=d?d.innerText.replace(/\n/g,"\n>"):""}else d="";var e=document.getElementById("imgBlkFront");e||(e=document.getElementById("ebooksImgBlkFront"));
    e=e.getAttribute("src");var h=[],k=document.getElementsByClassName("author");for(g=0;g<k.length;g++){var f=k[g].innerText.replace(/\r?\n/g,"").replace(/,/,""),l=f.match(/\(.+\)/);f=f.replace(/\(.+\)/,"").replace(/ /g,"");h.push(l+" ["+f+"]")}b="["+e+" "+window.location.href+"]\n"+h.join(" ")+"\n"+a[1]+a[2]+b+"\n>"+d+"\n#\u66f8\u7c4d\u540d\n";b=encodeURIComponent(b);window.open("https://scrapbox.io/rashitaobj/"+encodeURIComponent(c.trim())+"?body="+b)}})();

    ブックマークレットとして登録する

    スクリプトの用意ができたら、ブックマークレットとして登録しましょう。

    ブックマークのURLの代わりにスクリプトを貼り付けて保存すれば登録完了です。

    テストを行う

    Amazonの書籍ページを開いた状態で、ブックマークレットを起動すると….Scrapboxに新しいリンクページが作成されていました。超簡単!

    終わりに

    操作は以上。やり方は思った以上に簡単で、ブックマークレットとしてJavascriptを登録するだけです。試しに使ってみましたがめちゃくちゃ便利ですね!いちいち手入力していた時代が走馬灯のように….

    なくてはならないツールになりました。

    こんなに便利なら、他のことにもScrapboxを活用しようと思います。