bookmarklet_amazon_book_attr
code:bookmarklet.js
code:script.js
javascript: (
function () {
// ページ情報取得
var productTitle = document.getElementById("productTitle");
var ebookProductTitle = document.getElementById("ebooksProductTitle");
var asin_by_id = document.getElementById('ASIN');
var asin_by_name = document.getElementsByName('ASIN.0')0; var detail_from_div = document.getElementById('detailBullets_feature_div');
var description_iframe = document.getElementById("product-description-iframe");
if (description_iframe) {
var document_iframe = description_iframe.contentWindow.document;
var detail_from_iframe = description_iframe.getElementById("detailBullets_feature_div");
}
var productImage = document.getElementById("imgBlkFront");
var ebookImage = document.getElementById("ebooksImgBlkFront");
var authors_by_classname = document.getElementsByClassName('author');
// ドメイン取得ルール
if (productTitle) {
var title = productTitle.innerText.trim();
} else {
var title = ebookProdctTitle.innerText.trim();
}
if (asin_by_id) {
var asin = asin_by_id.value;
} else {
var asin = asin_by_name.value;
}
if (detail_from_div) {
var detailtext = detail_from_div.innerText;
} else {
var detailText = detail_from_iframe.innerText;
}
if (productImage){
image = productImage;
} else {
image = ebookImage;
}
// ドメイン1 URL (url)
// ドメイン2 著者 (authors)、原著者(original_authors)、翻訳者(translators)
var authors = [];
var original_authors = [];
var translators = [];
for (let author_element of authors_by_classname) {
var at = author_element.innerText.replace(/\r?\n/g, '').replace(/,/g, '').replace(/\t/g, '');
var ct = at.replace(/\{.+\}/, '').replace(/\(.+\)/, '').replace(/ +/g, ' ').replace(/ $/g, '');
if ( at.indexOf('翻訳)') != -1 ){
translators.push( ct.replace(/ /g, '') );
} else if ( at.indexOf('原著)') != -1 ){
original_authors.push( ct );
} else {
authors.push( ct );
}
}
// ドメイン3 書名(h1title)
var h1title = title ;
// ドメイン4 書影URL(imageurl)
var imageurl = image.getAttribute("src");
// ドメイン5, 6 出版社(publisher)、発売日(published_date)
var pubdata_array = detailtext.split(/\n/);
for (let pubdata_element of pubdata_array){
if (pubdata_element.indexOf('出版社') != -1){
var publisher = pubdata_element.slice(10);
}
if (pubdata_element.indexOf('発売日') != -1){
var published_date = pubdata_element.slice(10);
}
}
if (!publisher) {
var publisher = '';
}
if (!published_date) {
var published_date = '1900/1/1';
}
// Obsidianビュー1 タグ(tag)
var dpd = new Date(published_date + ' 09:00:00');
var str_dpd = dpd.toISOString().replace(/-/g,'/').substring(0,10);
var tag1 = str_dpd;
var tag2 = 'AmazonBooks';
if ( h1title.indexOf('コミックス') != -1 || h1title.indexOf('マンガ') != -1 ){
var tag3_comma = ',';
var tag3 = 'comics';
} else if ( h1title.indexOf('小説') != -1 || h1title.indexOf('ノベル') != -1 || h1title.indexOf('文庫') != -1){
var tag3_comma = ',';
var tag3 = 'novel';
} else {
var tag3_comma = '';
var tag3 = '';
}
var tag = 'tags: + tag1 + ',' + tag2 + tag3_comma + tag3 + '\n';
// Obsidianビュー2 エイリアス(alias)
var alias1_pubdate = '(' + published_date.substring(0,4) + ')';
if (authors.length > 1){
var alias1_authors = authors0 + '他'; } else {
var alias1_authors = authors0; }
if (publisher != ''){
var alias2_publisher_pubdate = publisher;
} else {
var alias2_publisher_pubdate = '(' + published_date + ')';
}
if (translators.length != 0 && original_authors.length != 0 ){
var alias2_translators = '. (原著:' + original_authors.join(' ') + ' 翻訳:' + translators.join(' ') + ')';
} else if ( translators.length != 0 ){
var alias2_translators = '. (翻訳:' + translators.join(' ') + ')';
} else if ( original_authors.length != 0 ){
var alias2_translators = '. (原著:' + original_authors.join(' ') + ')';
} else {
var alias2_translators = '';
}
var alias2 = authors.join(' ').replace(/ $/g, '') + '『' + h1title.replace(/:/g,'').replace(/,/g,'') + '』' + alias2_publisher_pubdate;
var alias3 = asin;
var alias4 = h1title.replace(/,/g,'').replace(/:/g,',') ;
var alias = 'aliases: ['
+ alias1_authors + alias1_pubdate
+ ', '
+ alias2 + alias2_translators
+ ', '
+ alias3
+ ', '
+ alias4
+ ']';
// Obsidianビュー3 リンク(link)
// Obsidianビュー4 書影(mdimage)
var mdimage = '\n!|100(' + imageurl + ')\n'; // Obsidianビュー総合(lines)
var lines = tag + alias + '\n---\n' + link + mdimage;
// 結果表示
document.getElementById('bookDescription_feature_div').innerHTML = '<textarea style="height:500px">' + lines + '</textarea>';
})();