tkrkt
6/8/2017 - 6:01 AM

[userscript] [Amazon] Add toc/review link to Amazon.co.jp

[userscript] [Amazon] Add toc/review link to Amazon.co.jp

// ==UserScript==
// @name        Amazon toc/review links
// @namespace   https://gist.github.com/tkrkt
// @description Add toc/review link to Amazon Book Page
// @include     https://www.amazon.co.jp/*
// @version     1.3
// @grant       GM_setClipboard
// ==/UserScript==

const storeID = document.querySelector('#storeID');
if (storeID) {
  const append = elem => {
    document.querySelector('#centerCol > hr + div').appendChild(elem);
  };

  const createButton = (label, action) => {
    const button = document.createElement('button');
    const inner = document.createElement('span');
    const text = document.createElement('span');
    button.className = 'a-button a-button-small';
    inner.className = 'a-button-inner';
    text.className = 'a-button-text';
    button.style.marginLeft = '.385em';
    text.textContent = label;
    button.onclick = () => action(button, inner, text);
    button.appendChild(inner);
    inner.appendChild(text);
    return button;
  };

  const asin = document.getElementById('ASIN').value;
  const shortUrl = 'http://www.amazon.co.jp/dp/' + asin;

  if (storeID.value === 'books') {
    append(createButton('目次を見る', () => {
      window.open('http://www.amazon.co.jp/dp/toc/' + asin);
    }));
  }
  append(createButton('レビューを見る', function () {
    window.open('http://www.amazon.co.jp/product-reviews/' + asin);
  }));
  append(createButton('レビューをググる', function () {
    const review = storeID.value === 'books' ? '(レビュー OR 感想 OR 書評) ' : '(レビュー OR 感想) ';
    const query = review + document.getElementById('productTitle').textContent.trim();
    window.open('https://www.google.co.jp/search?q=' + query);
  }));

  const urlContainer = document.createElement('div');

  const urlBox = document.createElement('input');
  urlBox.value = shortUrl;
  Object.assign(urlBox.style, {margin: '5px 0 5px 5px', width: '300px'});
  urlContainer.appendChild(urlBox);

  const copyButton = createButton('Copy', (_a, _b, text) => {
    urlBox.select();
    if (document.execCommand('copy')) {
      text.textContent = 'Copied!';
    } else {
      text.textContent = 'Error!';
    }
    setTimeout(() => {
      text.textContent = 'Copy';
    }, 5000);
  });
  urlContainer.appendChild(copyButton);

  append(urlContainer);
}