// ==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
// @downloadURL none
// ==/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 = '';
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 = '';
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();
})();