QuickDownloader

添加一个图标按钮来打开特定网页,带有当前页面的参数

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         QuickDownloader
// @namespace    http://tampermonkey.net/
// @version      0.7
// @description  添加一个图标按钮来打开特定网页,带有当前页面的参数
// @match        *://*.amazon.com/*
// @match        *://*.amazon.co.uk/*
// @match        *://*.amazon.de/*
// @match        *://*.amazon.fr/*
// @match        *://*.amazon.it/*
// @match        *://*.amazon.es/*
// @match        *://*.amazon.ca/*
// @match        *://*.amazon.co.jp/*
// @match        *://*.amazon.cn/*
// @match        *://*.amazon.in/*
// @match        *://*.amazon.com.br/*
// @match        *://*.amazon.com.mx/*
// @match        *://*.amazon.com.au/*
// @match        *://*.amazon.nl/*
// @match        *://*.amazon.sg/*
// @grant        GM_addStyle
// @grant        GM_getValue
// @grant        GM_setValue
// @require      https://kit.fontawesome.com/4c29a4a2e7.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const defaultUrl = 'https://zh.singlelogin.re/s/';
    // 兼容性层
    const getValue = (key, defaultValue) => {
        if (typeof GM_getValue === 'function') {
            return GM_getValue(key, defaultValue);
        }
        const value = localStorage.getItem(key);
        return value === null ? defaultValue : value;
    };

    const setValue = (key, value) => {
        if (typeof GM_setValue === 'function') {
            GM_setValue(key, value);
        } else {
            localStorage.setItem(key, value);
        }
    };

    // 兼容性检查和样式添加函数
    function addStyle(css) {
        if (typeof GM_addStyle !== "undefined") {
            GM_addStyle(css);
        } else {
            let style = document.createElement('style');
            style.textContent = css;
            document.head.appendChild(style);
        }
    }

    // 添加 Font Awesome 样式
    addStyle(`
        @import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css');
    `);

    // 创建按钮
    function createButton() {
        var button = document.createElement('button');
        button.innerHTML = '<i class="fas fa-download"></i>';
        button.title = '下载书籍'; // 添加tooltip
        button.style.top = '10px';
        button.style.right = '10px';
        button.style.zIndex = '9999';
        button.style.background = 'none';
        button.style.border = 'none';
        button.style.fontSize = '24px';
        button.style.color = '#0066c0'; // 修改为蓝色
        button.style.cursor = 'pointer';
        return button;
    }

    // 从页面提取参数
    function extractParams() {
        var productTitle = document.querySelector('#productTitle')?.textContent.trim();
        return encodeURIComponent(productTitle)
    }

    // 构建目标URL
    function buildTargetUrl(params) {
        const baseUrl = getValue('targetBaseUrl', defaultUrl);
        return `${baseUrl}/s/${params}?`;
    }

    // 设置配置的函数
    function setConfig() {
        const newBaseUrl = prompt("请输入书籍下载网址的基础 URL:", getValue('targetBaseUrl', defaultUrl));
        if (newBaseUrl !== null) {
            setValue('targetBaseUrl', newBaseUrl);
            alert("基础 URL 已更新!");
        }
    }

    // 创建配置按钮
    function createConfigButton() {
        var button = document.createElement('button');
        button.innerHTML = '<i class="fas fa-cog"></i>';
        button.title = '设置下载地址';
        button.style.display = 'inline-block';
        button.style.marginLeft = '10px';
        button.style.background = 'none';
        button.style.border = 'none';
        button.style.fontSize = '24px';
        button.style.color = '#0066c0';
        button.style.cursor = 'pointer';
        button.style.verticalAlign = 'middle';
        button.addEventListener('click', setConfig);
        return button;
    }

    // 插入按钮到指定位置
    function insertButton(button) {
        // 这里假设我们要将按钮插入到一个 ID 为 'product-title' 的元素后面
        var targetElement = document.querySelector('#productTitle');
        if (targetElement) {
            targetElement.parentNode.insertBefore(button, targetElement.nextSibling);
        } else {
            console.error('Target element for button insertion not found');
        }
    }

    // 主函数
    function main() {
        var button = createButton();
        var configButton = createConfigButton();

        button.addEventListener('click', function() {
            var params = extractParams();
            var targetUrl = buildTargetUrl(params);
            window.open(targetUrl, '_blank');
        });

        insertButton(button);
        insertButton(configButton);
    }

    // 运行主函数
    main();
})();