QuickDownloader

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

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

  1. // ==UserScript==
  2. // @name QuickDownloader
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.5
  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. // @grant GM_addStyle
  22. // @require https://kit.fontawesome.com/4c29a4a2e7.js
  23. // @license MIT
  24. // ==/UserScript==
  25.  
  26. (function() {
  27. 'use strict';
  28.  
  29. // 兼容性检查和样式添加函数
  30. function addStyle(css) {
  31. if (typeof GM_addStyle !== "undefined") {
  32. GM_addStyle(css);
  33. } else {
  34. let style = document.createElement('style');
  35. style.textContent = css;
  36. document.head.appendChild(style);
  37. }
  38. }
  39.  
  40. // 添加 Font Awesome 样式
  41. addStyle(`
  42. @import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css');
  43. `);
  44.  
  45. // 创建按钮
  46. function createButton() {
  47. var button = document.createElement('button');
  48. button.innerHTML = '<i class="fas fa-download"></i>';
  49. button.title = '下载书籍'; // 添加tooltip
  50. button.style.top = '10px';
  51. button.style.right = '10px';
  52. button.style.zIndex = '9999';
  53. button.style.background = 'none';
  54. button.style.border = 'none';
  55. button.style.fontSize = '24px';
  56. button.style.color = '#0066c0'; // 修改为蓝色
  57. button.style.cursor = 'pointer';
  58. return button;
  59. }
  60.  
  61. // 从页面提取参数
  62. function extractParams() {
  63. var productTitle = document.querySelector('#productTitle')?.textContent.trim();
  64. return encodeURIComponent(productTitle)
  65. }
  66.  
  67. // 构建目标URL
  68. function buildTargetUrl(params) {
  69. return `https://zh.singlelogin.re/s/${params}?`;
  70. }
  71.  
  72. // 插入按钮到指定位置
  73. function insertButton(button) {
  74. // 这里假设我们要将按钮插入到一个 ID 为 'product-title' 的元素后面
  75. var targetElement = document.querySelector('#productTitle');
  76. if (targetElement) {
  77. targetElement.parentNode.insertBefore(button, targetElement.nextSibling);
  78. } else {
  79. console.error('Target element for button insertion not found');
  80. }
  81. }
  82.  
  83. // 主函数
  84. function main() {
  85. var button = createButton();
  86.  
  87. button.addEventListener('click', function() {
  88. var params = extractParams();
  89. var targetUrl = buildTargetUrl(params);
  90. window.open(targetUrl, '_blank');
  91. });
  92.  
  93. insertButton(button);
  94. }
  95.  
  96. // 运行主函数
  97. main();
  98. })();