Open in Goodreads

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

目前為 2024-10-29 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Open in Goodreads
// @namespace    open-in-goodreads
// @version      2.0
// @description  Adds a button to Amazon book pages to redirect to Goodreads page based on ASIN/ISBN
// @author       SirGryphin
// @match        *://*.amazon.*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    function redirectToGoodreads() {
        // Attempt to find ASIN in various locations
        const asin = document.querySelector('[data-asin]')?.getAttribute('data-asin') ||
                     document.getElementById('ASIN')?.value ||
                     document.querySelector('input[name="ASIN"]')?.value ||
                     document.querySelector('input[name="ASIN.0"]')?.value;

        if (!asin) {
            alert("No ASIN or ISBN Found.");
            return;
        }

        // Construct Goodreads URL based on ASIN/ISBN
        const isNumeric = /^\d+$/.test(asin);
        const goodreadsURL = isNumeric
            ? `http://www.goodreads.com/review/isbn/${asin}`
            : `https://www.goodreads.com/book/isbn?isbn=${asin}`;

        window.open(goodreadsURL, '_blank');
    }

    function addButton() {
        // Check if the button already exists
        if (document.getElementById('OpenGoodreadsButton')) return;

        // Try to locate a reliable insert position
        const targetDivs = [
            'imageBlock_feature_div',
            'imageBlockNew_feature_div',
            'booksImageBlock_feature_div'
        ];
        const insertTarget = targetDivs.map(id => document.getElementById(id)).find(Boolean);

        // Create and style the button
        const button = document.createElement('button');
        button.id = 'OpenGoodreadsButton';
        button.innerText = 'Open in Goodreads';
        button.style.cssText = `
            margin-top: 10px;
            margin-bottom: 10px;
            display: block;
            margin-left: auto;
            margin-right: auto;
            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;

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

        // Insert button or fallback to document body
        if (insertTarget) {
            insertTarget.parentNode.insertBefore(centerDiv, insertTarget.nextSibling);
        } else {
            console.warn("Could not find specific insert target, appending to body.");
            document.body.appendChild(centerDiv);
        }
        console.log("Goodreads button added successfully.");

        // Disconnect observer once the button is added
        if (observer) observer.disconnect();
    }

    // Set up the MutationObserver
    const observer = new MutationObserver(() => {
        if (!document.getElementById('OpenGoodreadsButton')) {
            addButton(observer);  // Pass the observer to disconnect it inside addButton
        }
    });

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

    // Initial attempt to add the button
    addButton(observer);
})();

QingJ © 2025

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