/*
* @title Amazon #adツイート
* @description Twitterでシェアする際のツイート内容を自分用にカスタマイズ
* @include https://www.amazon.co.jp/*
* @license MIT License
* @javascript_url
*/
(() => {
const
show_text_link = () => {
document.querySelector('#amzn-ss-text-link a').click();
},
get_shortlink_url = () => {
const
shortlink_url = document.querySelector('#amzn-ss-text-shortlink-textarea')?.value;
if (! shortlink_url) return null;
return shortlink_url;
},
hide_text_link = () => {
document.querySelector('button[data-action="a-popover-close"]').click();
},
observe_main = ( records ) => {
observer.disconnect();
const
shortlink_url = get_shortlink_url();
if (! shortlink_url) {
observer.observe(document.body, {childList : true, subtree : true});
return;
}
finish(shortlink_url);
},
finish = (shortlink_url) => {
const
item_infos = document.title.split('|').map(info => info.trim()),
intent_tweet_url_obj = new URL('https://twitter.com/intent/tweet'),
url_params = intent_tweet_url_obj.searchParams;
url_params.set('related', 'amazondeals,amazonmp3');
if ((item_infos.length == 5) && ((item_infos[2] == '本') || (/Kindle/.test(item_infos[3])))) {
url_params.set('text', `${item_infos[0]}\n${item_infos[1]}\n#ad`);
}
else {
url_params.set('text', `${item_infos[(item_infos.length < 2) || (item_infos[0] != 'Amazon') ? 0 : 1]}\n#ad`);
}
url_params.set('url', shortlink_url);
window.open(intent_tweet_url_obj.href);
hide_text_link();
},
observer = new MutationObserver( observe_main );
show_text_link();
observe_main();
})();