Skip to content

Instantly share code, notes, and snippets.

@nora-tetsu
Created February 21, 2023 12:06
Show Gist options
  • Save nora-tetsu/3294ef2b1db728cb1b239913cf89c45f to your computer and use it in GitHub Desktop.
Save nora-tetsu/3294ef2b1db728cb1b239913cf89c45f to your computer and use it in GitHub Desktop.
ScrapboxにAmazonの書籍情報をクリップする
(function () {
// Amazonでなければ無効
const href = window.location.href;
if(!href.includes('www.amazon.co.jp') || !href.includes('/dp/')) return alert('Amazonの書籍ページではありません。');
// 自分のScrapboxのプロジェクトURLを指定
const projectName = 'hoge';
// 書名の欄を取得する
const titleElm = document.getElementById('productTitle') || document.getElementById('ebooksProductTitle');
// 取得した書名をデフォルト値としてページタイトルを決める
let title = window.prompt('選択範囲を概要としてスクラップします。', titleElm.innerText.trim()).trim();
if (!title) return;
title = '『' + title + '』'; // 『』をつける
// サブタイトル(書名直後のバージョンと発売日が書いてある部分)を取得する
const subtitleElm = document.getElementById('productSubtitle');
const sub = subtitleElm ? '\n' + subtitleElm.innerText : ''; // サブタイトルがあれば内容を取得して頭に改行を付与
// 選択範囲を取得する
const getSelection = window.getSelection(); // 選択範囲を文字列として取得
let selection = '';
if (getSelection && getSelection.toString()) { // 選択範囲があるとき
selection = getSelection.toString().replace(/(\W+)( )(\W+)/g, '$1$3'); // 字間に時々紛れている半角スペースを除去
selection = '\n>' + getSelection.toString().replace(/\n/g, '\n> '); // 行ごとに引用の「>」を付与
}
// 書影を取得する
const image = document.getElementById('imgBlkFront') || document.getElementById('ebooksImgBlkFront');
const imageSrc = image.getAttribute('src');
// 著者を取得する
const authors = [];
const authorElms = document.getElementsByClassName('author');
for (const elm of authorElms) {
const content = elm.innerText.replace(/\r?\n|\t/g, '').replace(/,/, '');
const category = content.match(/\(.+\)/); // (著)などの部分
const authorName = content.replace(/\(.+\)/, '').replace(/ /g, '');
authors.push(`${category}[${authorName}]`);
}
// 以上の情報を、実際に書き込みたい形に整えてひとつの文字列にまとめる(必要があれば任意の文字列を付け足す)
const body = `[${imageSrc} ${window.location.href}]\n${authors.join(' ')}${sub}\n\n${selection}`;
// エンコードしてURLをつくる
const url = `https://scrapbox.io/${projectName}/${encodeURIComponent(title)}?body=${encodeURIComponent(body)}`;
// URLを開いてページを作成(または同名のページに追記)
window.open(url);
})();
// お試し(実行するとプロジェクト名の入力欄が表示されます)
/*
javascript:(function(){const href=window.location.href;if(!href.includes('www.amazon.co.jp')||!href.includes('/dp/')) return alert('Amazonの書籍ページではありません。');const projectName=prompt('今開いている書籍情報をScrapboxに記録します。\nプロジェクトURLを入力してください。');if(!projectName)return;const titleElm=document.getElementById('productTitle')||document.getElementById('ebooksProductTitle');let title=window.prompt('選択範囲を概要としてスクラップします。',titleElm.innerText.trim());if(!title)return;title='『'+title.trim()+'』';const subtitleElm=document.getElementById('productSubtitle');const sub=subtitleElm?'\n'+subtitleElm.innerText:'';const getSelection=window.getSelection();let selection='';if(getSelection&&getSelection.toString()){selection=getSelection.toString().replace(/(\W+)( )(\W+)/g,'$1$3');selection='\n>'+getSelection.toString().replace(/\n/g,'\n> ')};const image=document.getElementById('imgBlkFront')||document.getElementById('ebooksImgBlkFront');const imageSrc=image.getAttribute("src");const authors=[];const authorElms=document.getElementsByClassName('author');for(const elm of authorElms){const content=elm.innerText.replace(/\r?\n|\t/g,'').replace(/,/,'');const category=content.match(/\(.+\)/);const authorName=content.replace(/\(.+\)/,'').replace(/ /g,'');authors.push(`${category}[${authorName}]`)};const body=`[${imageSrc} ${window.location.href}]\n${authors.join(' ')}${sub}\n\n${selection}`;const url=`https://scrapbox.io/${projectName}/${encodeURIComponent(title)}?body=${encodeURIComponent(body)}`;window.open(url)})()
*/
@nora-tetsu
Copy link
Author

このコードはこちらのブックマークレットを参考にしています。
AmazonからScrapboxにスクラップするブックマークレット - Scrapbox研究会

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment