Amazon書籍をScrapboxに保存するbookmarklet
参考
画像のURLそのままではなくgyazoってからgyazoのURLがほしい
氏名
日本語の場合は、姓名間のスペースを消す
英語の場合は残す
code:JavaScript
javascript: (function () {
const project = "mrsekut-p";
const title = encodeURIComponent(getTitle());
const authors = getAuthors();
const imageUrl = getImageUrl();
const date = getPubDate();
const publisher = getPublisher();
const lines = renderLines({ authors, imageUrl, date, publisher });
const body = encodeURIComponent(lines);
const url = https://scrapbox.io/${project}/『${title}』?body=${body};
window.open(url);
})();
function renderLines({ authors, imageUrl, date, publisher }) {
const authorLines = authors.map(renderAuthor);
return lines.map((l) => ${l}).join("\n");
}
function renderAuthor({ author, pu }) {
return ${link(author)} ${pu};
}
function link(str) {
return [${str}];
}
function getTitle() {
return (
document.getElementById("productTitle")?.innerHTML.trim() ?? "untitled"
);
}
function getImageUrl() {
return document.getElementById("landingImage")?.getAttribute("src");
}
function getAuthors() {
const spans = document.getElementsByClassName("author");
const at = span.innerText.replace(/,/, "");
const author = at
.replace(/\(.+\)/, "")
.replace(/ +/g, "")
.replace(/(A-Z)/g, " $1") .trim();
const pu = at.match(/\(.+\)/)0.replace(/\((.+)\)/, "$1"); return {
author,
pu,
};
});
}
function getPubDate() {
return document
.getElementById("productSubtitle")
?.innerText.match(/(\d*\/\d+\/\d+)$/)?.0; }
function getPublisher() {
const ul = document.querySelector(
"#detailBullets_feature_div ul.detail-bullet-list"
);
if (ul == null) {
return null;
}
const firstLi = ul.querySelector("li:first-child span:nth-child(2)");
if (firstLi == null) {
return null;
}
const publisherRegex = /^(.*)\s\(.*\)$/;
const match = firstLi.innerText.trim().match(publisherRegex);
return match ? match1 : null; }