QuickDownloader

根据不同的网站添加下载功能的按钮

目前为 2024-11-03 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name QuickDownloader
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 根据不同的网站添加下载功能的按钮
  6. // @match *://*.amazon.com/*
  7. // @match *://*.amazon.co.uk/*
  8. // @match *://*.amazon.de/*
  9. // @match *://*.amazon.fr/*
  10. // @match *://*.amazon.it/*
  11. // @match *://*.amazon.es/*
  12. // @match *://*.amazon.ca/*
  13. // @match *://*.amazon.co.jp/*
  14. // @match *://*.amazon.cn/*
  15. // @match *://*.amazon.in/*
  16. // @match *://*.amazon.com.br/*
  17. // @match *://*.amazon.com.mx/*
  18. // @match *://*.amazon.com.au/*
  19. // @match *://*.amazon.nl/*
  20. // @match *://*.amazon.sg/*
  21. // @match *://*.goodreads.com/*
  22. // @match *://*.douban.com/*
  23. // @grant GM_addStyle
  24. // @grant GM_getValue
  25. // @grant GM_setValue
  26. // @require https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/js/all.min.js
  27. // 添加 Font Awesome CSS
  28. // @resource fontawesome https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css
  29. // @license MIT
  30. // ==/UserScript==
  31.  
  32. (function () {
  33. "use strict";
  34.  
  35. // 兼容性层
  36. const getValue = (key, defaultValue) => {
  37. if (typeof GM_getValue === "function") {
  38. return GM_getValue(key, defaultValue);
  39. }
  40. const value = localStorage.getItem(key);
  41. return value === null ? defaultValue : value;
  42. };
  43.  
  44. const setValue = (key, value) => {
  45. if (typeof GM_setValue === "function") {
  46. GM_setValue(key, value);
  47. } else {
  48. localStorage.setItem(key, value);
  49. }
  50. };
  51.  
  52. // 检查当前URL并决定行为
  53. function checkUrlAndAct() {
  54. const currentUrl = window.location.href;
  55. if (currentUrl.includes("amazon")) {
  56. return handleAmazonDownload();
  57. } else if (currentUrl.includes("goodreads.com")) {
  58. return handleGoodreadsDownload();
  59. } else if (currentUrl.includes("douban.com")) {
  60. return handleDoubanDownload();
  61. }
  62. // 可以继续添加其他网站的处理函数
  63. }
  64.  
  65. // 处理亚马逊网站的下载
  66. function handleAmazonDownload() {
  67. const button = createButton("下载书籍", "fa-download");
  68. button.addEventListener("click", function () {
  69. const params = extractAmazonParams();
  70. const targetUrl = buildTargetUrl(params);
  71. window.open(targetUrl, "_blank");
  72. });
  73. insertButton(button);
  74. }
  75.  
  76. // 处理Goodreads网站的下载
  77. function handleGoodreadsDownload() {
  78. const button = createButton("下载书籍", "fa-download");
  79. button.addEventListener("click", function () {
  80. const bookTitle = extractGoodreadsBookTitle();
  81. const params = encodeURIComponent(bookTitle);
  82. const targetUrl = buildTargetUrl(params);
  83. window.open(targetUrl, "_blank");
  84. });
  85. insertButton(button);
  86. }
  87.  
  88. // 处理豆瓣网站的下载
  89. function handleDoubanDownload() {
  90. const button = createButton("下载书籍", "fa-download");
  91. button.addEventListener("click", function () {
  92. const bookTitle = extractDoubanBookTitle();
  93. const params = encodeURIComponent(bookTitle);
  94. const targetUrl = buildTargetUrl(params);
  95. window.open(targetUrl, "_blank");
  96. });
  97. insertButton(button);
  98. }
  99.  
  100. // 创建按钮
  101. function createButton(title, iconClass) {
  102. const button = document.createElement("button");
  103. button.innerHTML = `<i class="fas ${iconClass}"></i>`;
  104. button.title = title;
  105. button.style.display = "inline-block";
  106. button.style.marginLeft = "10px";
  107. button.style.background = "none";
  108. button.style.border = "none";
  109. button.style.fontSize = "24px";
  110. button.style.color = "#0066c0";
  111. button.style.cursor = "pointer";
  112. button.style.verticalAlign = "middle";
  113. return button;
  114. }
  115.  
  116. // 从亚马逊页面提取参数
  117. function extractAmazonParams() {
  118. const productTitle = document
  119. .querySelector("#productTitle")
  120. ?.textContent.trim();
  121. return encodeURIComponent(productTitle);
  122. }
  123.  
  124. // 从Goodreads页面提取书名
  125. function extractGoodreadsBookTitle() {
  126. const bookTitle = document.querySelector("h1")?.textContent.trim();
  127. return bookTitle || "";
  128. }
  129.  
  130. // 从豆瓣页面提取书名
  131. function extractDoubanBookTitle() {
  132. const bookTitle = document.querySelector("h1 span")?.textContent.trim();
  133. return bookTitle || "";
  134. }
  135.  
  136. // 构建目标URL
  137. function buildTargetUrl(params) {
  138. const baseUrl = getValue("targetBaseUrl", "https://zh.singlelogin.re/s/");
  139. return `${baseUrl}${params}?`;
  140. }
  141.  
  142. // 插入按钮到指定位置
  143. function insertButton(button) {
  144. let targetElement;
  145. if (window.location.href.includes("amazon")) {
  146. targetElement = document.querySelector("#productTitle");
  147. } else if (window.location.href.includes("goodreads.com")) {
  148. targetElement = document.querySelector("h1");
  149. } else if (window.location.href.includes("douban.com")) {
  150. targetElement = document.querySelector("h1 span");
  151. }
  152.  
  153. if (targetElement) {
  154. targetElement.style.display = "inline-block";
  155. targetElement.style.marginRight = "10px";
  156. targetElement.parentNode.insertBefore(button, targetElement.nextSibling);
  157. } else {
  158. console.error("未找到插入按钮的目标元素");
  159. }
  160. }
  161.  
  162. // 设置配置的函数
  163. function setConfig() {
  164. const newBaseUrl = prompt(
  165. "请输入新的基础 URL:",
  166. getValue("targetBaseUrl", "https://zh.singlelogin.re/s/")
  167. );
  168. if (newBaseUrl !== null) {
  169. setValue("targetBaseUrl", newBaseUrl);
  170. alert("基础 URL 已更新!");
  171. }
  172. }
  173.  
  174. // 主函数
  175. function main() {
  176. checkUrlAndAct();
  177.  
  178. const configButton = createButton("设置下载地址", "fa-cog");
  179. configButton.addEventListener("click", setConfig);
  180. insertButton(configButton);
  181. }
  182.  
  183. // 运行主函数
  184. main();
  185. })();

QingJ © 2025

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