Open in Goodreads

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

目前为 2024-10-31 提交的版本。查看 最新版本

// ==UserScript==
// @name         Open in Goodreads
// @namespace    open-in-goodreads
// @version      2.3
// @description  Adds a button to Amazon book pages to redirect to Goodreads page based on ASIN/ISBN
// @match        https://*.amazon.*/*
// @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 });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址