Amazon_Keepa_Sakura_Button

Add links to Keepa and Sakura Checker to the Amazon.co.jp product screen.

질문, 리뷰하거나, 이 스크립트를 신고하세요.
// ==UserScript==
// @name            Amazon_Keepa_Sakura_Button
// @name:ja         Amazonの商品画面に価格履歴とサクラチェックのボタンを追加
// @namespace       https://greasyfork.org/users/1324207
// @match           https://www.amazon.co.jp/dp/*
// @match           https://www.amazon.co.jp/*/dp/*
// @match           https://www.amazon.co.jp/gp/product/*
// @match           https://www.amazon.co.jp/exec/obidos/ASIN/*
// @match           https://www.amazon.co.jp/o/ASIN/*
// @version         1.2.0
// @author          乾かしカラス
// @description     Add links to Keepa and Sakura Checker to the Amazon.co.jp product screen.
// @description:ja  Amazonの商品画面にKeepaとサクラチェッカーへのリンクを追加します。
// @license         MIT
// @icon            https://www.amazon.co.jp/favicon.ico
// ==/UserScript==

(() => {
    'use strict';

    const TARGET_SELECTORS = [
        '#buyNow',
        '#add-to-cart-button',
        '#buybox .a-button-stack',
        '#add-to-cart-button-ubb',
        '#buybox-see-all-buying-choices',
        '#buybox-see-all-buying-choices-announce',
        '#rcx-subscribe-submit-button-announce',
        '#dealsAccordionRow',
        '#outOfStock'
    ];

    function extractAsin() {
        return (
            window.location.pathname.split('/').find(part => part.length === 10 && /^[A-Z0-9]+$/.test(part)) ||
            new URLSearchParams(window.location.search).get('asin') ||
            document.querySelector('[name="ASIN.0"], [name="ASIN"]')?.value ||
            ''
        );
    }

    function findTargetElement() {
        for (const selector of TARGET_SELECTORS) {
            const target = document.querySelector(selector);
            if (target) return target.closest('div.a-section');
        }
        return null;
    }

    function addCheckerLinks() {
        if (document.getElementById('checker-links')) return;

        const asin = extractAsin();
        if (!asin) return;

        const target = findTargetElement();
        if (!target) return;

        const div = document.createElement('div');
        div.id = 'checker-links';
        div.className = 'checker';
        div.innerHTML = `
            <a href='https://keepa.com/#!product/5-${asin}/' target='_blank' class='price-history-link'>価格履歴</a>
            <a href='https://sakura-checker.jp/search/${asin}/' target='_blank' class='sakura-checker-link'>サクラチェック</a>
        `;
        target.insertAdjacentElement('afterend', div);
    }

    function addStyles() {
        if (document.getElementById('checker-style')) return;

        const style = document.createElement('style');
        style.id = 'checker-style';
        style.textContent = `
            .checker a {
                display: inline-block;
                border: 0;
                height: 4ex;
                line-height: 4ex;
                margin-bottom: 1.2ex;
                width: 100%;
                text-align: center;
                color: black;
                border-radius: 10em;
                text-decoration: none;
                font-size: 1em;
            }
            .sakura-checker-link {
                background: deeppink;
            }
            .sakura-checker-link:hover {
                background: Crimson;
            }
            .price-history-link {
                background: DeepSkyBlue;
            }
            .price-history-link:hover {
                background: DodgerBlue;
            }
            @media screen and (max-width: 768px) {
                .checker a {
                    height: 5.5ex;
                    line-height: 5.5ex;
                }
            }
        `;
        document.head.appendChild(style);
    }

    let lastAsin = '';
    const observer = new MutationObserver(() => {
        const currentAsin = extractAsin();
        if (lastAsin !== currentAsin) {
            lastAsin = currentAsin;
            addCheckerLinks();
        }
    });

    observer.observe(document.documentElement, { childList: true, subtree: true });

    addStyles();
    addCheckerLinks();
})();