amazon-bookmark-2
2025-03-26 12:27:11 CSPに引っ掛かるようになったので、minifyしたのを使う
を\tに、改行を\nに書き換える必要あり
code:js
javascript:(()=>{if(location.hostname!=="www.amazon.co.jp")return;const s=window.prompt('Scrap "Amazon" to your scrapbox:',"takker");if(!s)return;const r=『${(document.getElementById("productTitle")??document.getElementById("ebooksProductTitle")).textContent.trim()}』,u=document.getElementById("landingImage").src,d=document.querySelector("#bookDescription_feature_div .a-expander-content")?.innerText??"",i=document.getElementsByClassName("book_details-publication_date")?.0?.closest?.(".rpi-attribute-content")?.getElementsByClassName?.("rpi-attribute-value")?.0?.textContent?.trim?.()?.split?.("/")?.map?.(t=>t.padStart(2,"0"))?.join("-"),l=document.getElementsByClassName("book_details-publisher")?.0?.closest?.(".rpi-attribute-content")?.getElementsByClassName?.("rpi-attribute-value")?.0?.textContent?.trim?.()??"",c=document.getElementsByClassName("book_details-isbn13")?.0?.closest?.(".rpi-attribute-content")?.getElementsByClassName?.("rpi-attribute-value")?.0?.textContent?.trim?.()?.replaceAll?.("-","")??"",o=new Map;for(const t of document.querySelectorAll(".author > a.a-link-normal, .author > span > a.a-link-normal")){const a=t.textContent.trim(),n=t.closest(".author").lastElementChild.textContent.trim().match(/\((^)+)\)/)?.1;o.set(n,[a,...o.get(n)??[]])}const m=document.getElementById("ASIN"),g=m?https://${location.hostname}/dp/${m.value}:location.href,e=t=>String(t).padStart(2,"0"),$=(t=>${t.getFullYear()}-${e(t.getMonth()+1)}-${e(t.getDate())} ${e(t.getHours())}:${e(t.getMinutes())}:${e(t.getSeconds())})(new Date),p=[[${u}#.png ${g}],"","table:bibliography",......o.entries().map((t,a)=> ${t} ${a.map(n=>${n}).join(", ")})," 件名標目\t", 出版日\t${i===""?"":${i}}, 出版社\t${l===""?"":${l}}, ISBN-13\t${c===""?"":c}," NDC10\t","NDL search","",...d.split(\n).map(t=>>${t}),"","なんでbookmarkしたの?"," ","","目次"," ","",#${$} scraped];navigator.clipboard.writeText(${r.trim()}\n${p.join(\n)}).then(()=>{confirm("copied. Make the new page?")&&window.open(https://scrapbox.io/${s}/${encodeURIComponent(r.trim())}?body=${encodeURIComponent(p.join(\n))},"_self")})})(); code:bookmarklet.js
(() => {
if (location.hostname !== "www.amazon.co.jp") return;
const project = window.prompt('Scrap "Amazon" to your scrapbox:', "takker");
if (!project) return;
書誌情報を取得する
書名
#productTitle
code:bookmarklet.js
const p = document.getElementById("productTitle")
?? document.getElementById("ebooksProductTitle");
const title = 『${p.textContent.trim()}』;
書影
#landingImage
code:bookmarklet.js
const img = document.getElementById("landingImage");
const imageUrl = img.src;
概要
#bookDescription_feature_div .a-expander-content
code:bookmarklet.js
const description = document.querySelector("#bookDescription_feature_div .a-expander-content")
?.innerText ?? "";
出版日
.book_details-publication_dateの祖先要素.rpi-attribute-contentの子要素.rpi-attribute-valueの中身
/を-に変える
code:bookmarklet.js
const published = document.getElementsByClassName("book_details-publication_date")?.0 ?.closest?.(".rpi-attribute-content")
?.getElementsByClassName?.("rpi-attribute-value")?.0 ?.textContent?.trim?.()
?.split?.("/")
?.map?.((n) => n.padStart(2, "0"))
?.join("-");
出版社
.book_details-publisherの祖先要素.rpi-attribute-contentの子要素.rpi-attribute-valueの中身
code:bookmarklet.js
const publisher = document.getElementsByClassName("book_details-publisher")?.0 ?.closest?.(".rpi-attribute-content")
?.getElementsByClassName?.("rpi-attribute-value")?.0 ?.textContent?.trim?.() ?? "";
ISBN-13
.book_details-isbn13の祖先要素.rpi-attribute-contentの子要素.rpi-attribute-valueの中身
-は削る
code:bookmarklet.js
const isbn = document.getElementsByClassName("book_details-isbn13")?.0 ?.closest?.(".rpi-attribute-content")
?.getElementsByClassName?.("rpi-attribute-value")?.0 ?.textContent?.trim?.()
?.replaceAll?.("-", "") ?? "";
著者情報の取得
.author > a.a-link-normalか.author > span > a.a-link-normalに入っている
code:bookmarklet.js
const authorMap = new Map();
for (const a of
document.querySelectorAll(".author > a.a-link-normal, .author > span > a.a-link-normal")
) {
const author = a.textContent.trim();
const type = a.closest(".author").lastElementChild.textContent.trim().match(/\((^)+)\)/)?.1; authorMap.set(type, [author, ...authorMap.get(type) ?? []]);
}
余計なparametersを取り除いたAmazonのURL
#ASINのvalueをhttps://${location.hostname}/dp/につなげる
code:bookmarklet.js
const asin = document.getElementById("ASIN");
const url = asin ? https://${location.hostname}/dp/${asin.value} : location.href;
scrapした日時を取得する
code:bookmarklet.js
const zero = n => String(n).padStart(2, "0");
const today = (
d => ${d.getFullYear()}-${zero(d.getMonth() + 1)}-${zero(d.getDate())} ${zero(d.getHours())}:${zero(d.getMinutes())}:${zero(d.getSeconds())}
)(new Date());
templateからページを作成する
code:bookmarklet.js
const lines = [
[${imageUrl}#.png ${url}],
"",
"table:bibliography",
(author) => [${author}]
).join(", ")}`),
" 件名標目\t",
ISBN-13\t${isbn === "" ? "" : isbn},
" NDC10\t",
"NDL search", // リンクなどを書く欄
"",
...description.split("\n").map((line) => >${line}),
"",
"なんでbookmarkしたの?",
" ",
"",
"目次",
" ",
"",
#${today} scraped,
];
同じタブで開く
code:bookmarklet.js
navigator.clipboard.writeText(${title.trim()}\n${lines.join("\n")})
.then(() => {
if (!confirm("copied. Make the new page?")) return;
window.open(
encodeURIComponent(title.trim())
}?body=${
encodeURIComponent(lines.join('\n'))
}`,
"_self",
);
});
})();