Greasy Fork 支持简体中文。

Open in Goodreads

Adds a button to Amazon book pages to redirect to Goodreads page based on ASIN/ISBN

// ==UserScript==
// @name         Open in Goodreads
// @namespace    open-in-goodreads
// @version      2.4
// @description  Adds a button to Amazon book pages to redirect to Goodreads page based on ASIN/ISBN
// @match        https://*.amazon.com/*
// @match        https://*.amazon.co.uk/*
// @match        https://*.amazon.com.au/*
// @match        https://*.amazon.com.be/*
// @match        https://*.amazon.com.br/*
// @match        https://*.amazon.ca/*
// @match        https://*.amazon.cn/*
// @match        https://*.amazon.eg/*
// @match        https://*.amazon.fr/*
// @match        https://*.amazon.de/*
// @match        https://*.amazon.in/*
// @match        https://*.amazon.it/*
// @match        https://*.amazon.co.jp/*
// @match        https://*.amazon.com.mx/*
// @match        https://*.amazon.nl/*
// @match        https://*.amazon.pl/*
// @match        https://*.amazon.sa/*
// @match        https://*.amazon.sg/*
// @match        https://*.amazon.es/*
// @match        https://*.amazon.se/*
// @match        https://*.amazon.com.tr/*
// @match        https://*.amazon.ae/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    function redirectToGoodreads() {
        var asinElements = document.getElementsByName('ASIN');
        if (asinElements.length === 0) asinElements = document.getElementsByName('ASIN.0');

        if (asinElements.length > 0) {
            const asin = asinElements[0].value;
            const goodreadsUrl = asin.match(/\D/) === null
                ? `http://www.goodreads.com/review/isbn/${asin}`
                : `https://www.goodreads.com/book/isbn?isbn=${asin}`;
            window.open(goodreadsUrl, '_blank');
        } else {
            alert("No ASIN or ISBN Found.");
        }
    }

    function addButton() {
        const imageBlockNew = document.getElementById('imageBlockNew_feature_div');
        const imageBlock = document.getElementById('imageBlock_feature_div');
        const booksImageBlock = document.getElementById('booksImageBlock_feature_div');

        // Only add button if one of the target image blocks is present
        if (imageBlockNew || booksImageBlock || imageBlock) {
            // Check for Publication date in the carousel section
            const carousel = document.querySelector('.a-carousel');
            const publicationDateExists = carousel && carousel.innerText.includes('Publication date');

            if (publicationDateExists) {
                const button = document.createElement('button');
                button.innerText = 'Open in Goodreads';
                button.style.cssText = `
                    margin: 10px auto;
                    display: block;
                    color: #ffffff;
                    background-color: #377458;
                    border: none;
                    border-radius: 4px;
                    padding: 8px 12px;
                    font-family: Arial, sans-serif;
                    font-size: 14px;
                    font-weight: bold;
                    cursor: pointer;
                `;
                button.onclick = redirectToGoodreads;

                const centerDiv = document.createElement('div');
                centerDiv.style.textAlign = 'center';
                centerDiv.appendChild(button);

                // Insert the button after the image block
                if (imageBlockNew) imageBlockNew.parentNode.insertBefore(centerDiv, imageBlockNew.nextSibling);
                else if (booksImageBlock) booksImageBlock.parentNode.insertBefore(centerDiv, booksImageBlock.nextSibling);
                else if (imageBlock) imageBlock.parentNode.insertBefore(centerDiv, imageBlock.nextSibling);
            }
        }
    }

    // Observer to detect when the target elements are available
    const observer = new MutationObserver(() => {
        const imageBlockNew = document.getElementById('imageBlockNew_feature_div');
        const imageBlock = document.getElementById('imageBlock_feature_div');
        const booksImageBlock = document.getElementById('booksImageBlock_feature_div');

        if (imageBlockNew || booksImageBlock || imageBlock) {
            addButton();
            observer.disconnect(); // Stop observing once the button is added
        }
    });

    // Start observing for changes in the DOM
    observer.observe(document.body, { childList: true, subtree: true });
})();