amazon-bookmark-2
code:js
code:bookmarklet.js
javascript:(() => {
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",
);
});
})();