Greasy Fork

Greasy Fork is available in English.

Show points on Amazon.co.jp wishlist

Amazon.co.jpの欲しいものリストにポイントを表示

当前为 2018-01-06 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Show points on Amazon.co.jp wishlist
// @namespace    http://greasyfork.icu/ja/users/165645-agn5e3
// @version      1.3
// @description  Amazon.co.jpの欲しいものリストにポイントを表示
// @author       agn5e3
// @license      Apache-2.0
// @match        https://www.amazon.co.jp/*/wishlist/*
// ==/UserScript==
(function () {
  'use strict';
  window.addEventListener('load', function () {
    const domParser = new DOMParser();

    let observer = new MutationObserver((records) => {
      for (const record of records) {
        for (const node of record.addedNodes) {
          displayPoint(node);
        }
      }
    });

    let modifyCharacter = ((str) => {
      return str.replace(/[\s]+/g, '').replace( /(/, '(' ).replace( /)/, ')' ).replace( /:/, ':' ).replace( /の割引/, '円' ).replace( /\(/, ' (' );
    });

    let setColor = ((str) => {
      var ratio = str.match(/[0-9]+%/);
      if(ratio === null){
        ratio = 0;
      }else{
        ratio = parseInt(ratio);
      }

      var color;
      if(ratio < 25){
        color = '#111';
      }else if(ratio < 50){
        color = '#f60';
      }else if(ratio < 75){
        color = '#f0f';
      }else{
        color = '#f00';
      }

      return '<p style="margin:0;color:' + color + '">' + str + '</p>';
    });

    let displayPoint = ((node) => {
      if (node.querySelectorAll && node.getElementsByClassName('fetched')[0] === undefined && node.querySelector('div[id^="item_"]') !== null && /Kindle/.test(node.innerText)) {
        node.getElementsByTagName('div')[0].classList.add('fetched');
        const priceSection = node.getElementsByClassName('price-section')[0];
        const asin = JSON.parse(priceSection.attributes["data-item-prime-info"].value).asin;
        console.log(asin);
        fetch('https://www.amazon.co.jp/dp/' + asin).then((response) => response.text()).then((text) => {
          const html = domParser.parseFromString(text, 'text/html');
          const printPrice = html.getElementsByClassName('print-list-price')[0];
          const kindleSave = html.getElementsByClassName('ebooks-price-savings')[0];
          const loyaltyPoint = html.getElementsByClassName('loyalty-points')[0];
          if (printPrice !== undefined) priceSection.insertAdjacentHTML('afterbegin', setColor(modifyCharacter(printPrice.innerText)));
          if (kindleSave !== undefined) priceSection.insertAdjacentHTML('beforeend', setColor('割引:' + modifyCharacter(kindleSave.innerText).replace( /¥/, '' )));
          if (loyaltyPoint !== undefined) priceSection.insertAdjacentHTML('beforeend', setColor(modifyCharacter(loyaltyPoint.innerText)));
          node.getElementsByClassName('a-price-symbol')[0].remove();
          node.getElementsByClassName('a-price-whole')[0].insertAdjacentHTML('afterbegin', '¥');
          node.getElementsByClassName('a-price')[0].classList.add('a-color-price', 'a-size-large');
        }).catch((error) => console.error(error));
      }
    });

    let main = ((node) => {
      for (const node of document.getElementsByClassName('g-item-sortable')) {
        displayPoint(node);
      }

      observer.observe(document.getElementById('g-items'), {
        childList: true,
        subtree: true,
      });
    });

    main();
  });
})();