e-hentai-download.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // ==UserScript==
  2. // @name 图片下载器
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description 尝试在页面顶部添加一个按钮,点击后下载页面所有图片
  6. // @author Jack
  7. // @match *://*/*
  8. // @grant GM_download
  9. // @grant GM_addStyle
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.5.0/jszip.min.js
  11. // ==/UserScript==
  12. (function () {
  13. 'use strict';
  14. // 添加样式以确保按钮在页面加载时可见
  15. GM_addStyle(`
  16. .download-pic-button {
  17. position: fixed;
  18. top: 5%; /* 默认位置 */
  19. left: 50%;
  20. transform: translateX(-50%);
  21. z-index: 10000;
  22. border: none;
  23. background-color: transparent; /* 背景颜色设置为透明 */
  24. color: rgb(113, 100, 156); /* 字体颜色设置为指定的 RGB 值 */
  25. cursor: pointer;
  26. font-size: 15px;
  27. padding: 0;
  28. }
  29. `);
  30. // 创建按钮并添加到页面顶部的中间
  31. var downloadButton = document.createElement("button");
  32. downloadButton.textContent = "New Btn";
  33. downloadButton.className = 'download-pic-button';
  34. document.body.appendChild(downloadButton);
  35. // 检查特定元素是否存在,并设置按钮位置
  36. function checkElementAndSetButtonPosition() {
  37. var targetElement = document.querySelector("body > div.o_action_manager > div > div.o_control_panel > div.o_cp_bottom > div.o_cp_bottom_left > div > div > button.btn.btn-secondary.fa.fa-download.o_list_export_xlsx");
  38. if (targetElement) {
  39. // 如果元素存在,获取元素的位置和尺寸
  40. var targetRect = targetElement.getBoundingClientRect();
  41. // 设置按钮的 top 值,使其与目标元素顶部对齐
  42. downloadButton.style.top = `${targetRect.top + window.scrollY}px`;
  43. // 设置按钮的 left 值,使其在目标元素右侧偏移量为 targetElementWidth + offset
  44. var targetElementWidth = targetRect.width;
  45. var offset = 10; // 你可以根据需要调整这个偏移量
  46. downloadButton.style.left = `${targetRect.right + offset}px`; // 偏移量在目标元素右侧
  47. downloadButton.style.transform = 'none';
  48. } else {
  49. // 如果元素不存在,恢复按钮的默认位置
  50. downloadButton.style.top = '5%';
  51. downloadButton.style.left = '50%';
  52. downloadButton.style.transform = 'translateX(-50%)';
  53. }
  54. }
  55. // 初始检查
  56. checkElementAndSetButtonPosition();
  57. // 监听滚动事件,以便在滚动时更新按钮位置
  58. window.addEventListener('scroll', function () {
  59. checkElementAndSetButtonPosition();
  60. });
  61. // 监听 DOM 变化,以便在元素被添加到页面时更新按钮位置
  62. var observer = new MutationObserver(function (mutations) {
  63. mutations.forEach(function (mutation) {
  64. if (mutation.type === 'childList') {
  65. checkElementAndSetButtonPosition();
  66. }
  67. });
  68. });
  69. observer.observe(document.body, { childList: true, subtree: true });
  70. // 点击按钮时执行的函数
  71. // 点击按钮时执行的函数
  72. downloadButton.onclick = function () {
  73. var images = document.querySelectorAll('img'); // 获取页面上所有的<img>元素
  74. var zip = new JSZip();
  75. console.log(images);
  76. images.forEach(function (img) {
  77. var src = img.src; // 获取图片的src属性
  78. var filename = img.alt || 'image'; // 使用alt属性作为文件名,如果没有alt则默认为'image'
  79. // 获取文件扩展名,如果没有扩展名则默认为 'jpg'
  80. var extensionMatch = src.match(/\.([^.\/\?]+)$/); // 正则表达式匹配文件扩展名
  81. var extension = extensionMatch ? extensionMatch[1].toLowerCase() : 'jpg';
  82. // 检查扩展名是否是图片格式
  83. var validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'bmp', 'webp'];
  84. if (validExtensions.includes(extension)) {
  85. // 异步获取图片数据
  86. fetch(src)
  87. .then(response => {
  88. if (response.ok) return response.blob();
  89. throw new Error('Network response was not ok.');
  90. })
  91. .then(blob => {
  92. // 将blob添加到zip文件中
  93. zip.file(filename + '.' + extension, blob);
  94. })
  95. .catch(error => {
  96. console.error('There was a problem with the fetch operation:', error);
  97. });
  98. } else {
  99. // 如果不是图片格式,跳过这个文件
  100. console.log('Skipped non-image file:', src);
  101. }
  102. });
  103. // 当所有图片都添加到zip中后,生成并下载zip文件
  104. Promise.all(zip.files).then(() => {
  105. zip.generateAsync({ type: 'blob' }).then(function (content) {
  106. var filename = 'images_' + new Date().toISOString() + '.zip'; // 使用时间戳生成唯一的文件名
  107. var eleLink = document.createElement('a');
  108. eleLink.download = filename;
  109. eleLink.style.display = 'none';
  110. eleLink.href = URL.createObjectURL(content);
  111. document.body.appendChild(eleLink);
  112. eleLink.click();
  113. document.body.removeChild(eleLink);
  114. });
  115. });
  116. };
  117. })();