爱发电自动下载

Automatically download content from afdian

  1. // ==UserScript==
  2. // @name 爱发电自动下载
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Automatically download content from afdian
  6. // @author NBXX
  7. // @match https://afdian.net/*
  8. // @grant GM_download
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. let floatButton = document.createElement("button");
  16. floatButton.textContent = "自动滚动";
  17. floatButton.style.position = "fixed";
  18. floatButton.style.bottom = "20px";
  19. floatButton.style.right = "20px";
  20. floatButton.style.zIndex = "9999";
  21. floatButton.style.padding = "10px 20px";
  22. floatButton.style.fontSize = "1rem";
  23. floatButton.style.color = "#fff";
  24. floatButton.style.background = "#007bff";
  25. floatButton.style.border = "none";
  26. floatButton.style.borderRadius = "5px";
  27. floatButton.style.cursor = "pointer";
  28.  
  29. document.body.appendChild(floatButton);
  30.  
  31. let autoScrolling = false;
  32. let lastXHRTime = Date.now();
  33.  
  34. floatButton.addEventListener('click', function() {
  35. if (!autoScrolling) {
  36.  
  37. this.textContent = "停止滚动";
  38. autoScrolling = true;
  39. autoScroll();
  40. } else {
  41. this.textContent = "自动滚动";
  42. autoScrolling = false;
  43. }
  44. });
  45.  
  46. function autoScroll() {
  47. if (autoScrolling) {
  48. window.scrollTo(0, document.body.scrollHeight);
  49.  
  50. // 检查是否有新的XHR请求
  51. if (Date.now() - lastXHRTime < 3000) { // 3秒内没有新的XHR请求
  52. setTimeout(autoScroll, 1000); //
  53. } else {
  54. floatButton.textContent = "自动滚动";
  55. autoScrolling = false;
  56. }
  57. }
  58. }
  59.  
  60. // 监听XHR请求并处理数据
  61. function detectXHR() {
  62. const originalOpen = XMLHttpRequest.prototype.open;
  63. XMLHttpRequest.prototype.open = function() {
  64. this.addEventListener('load', function() {
  65. if (this.responseURL.includes("https://afdian.net/api/post/get-list?")) {
  66. lastXHRTime = Date.now();
  67. try {
  68. const responseData = JSON.parse(this.responseText);
  69. handleResponseData(responseData);
  70. } catch (e) {
  71. console.error("Error parsing response data: ", e);
  72. }
  73. }
  74. });
  75. originalOpen.apply(this, arguments);
  76. };
  77. }
  78.  
  79. function handleResponseData(data) {
  80. const list = data["data"]["list"];
  81. list.forEach((item) => {
  82. if (item["has_right_errMsg"] == null) {
  83. const postId = item["post_id"];
  84. const title = item["title"];
  85. const cate = item["cate"];
  86. console.log(`Downloading: ${postId}, ${title}`);
  87. if (cate === "audio") {
  88. downloadContent(item["audio"], `${title}.mp3`);
  89. } else if (cate === "pic") {
  90. const pics = item["pics"];
  91. pics.forEach((picUrl, index) => {
  92. downloadContent(picUrl, `${postId}_${index + 1}.jpg`);
  93. });
  94. }
  95. }
  96. });
  97. }
  98.  
  99. function downloadContent(url, filename) {
  100. GM_download(url, filename);
  101. }
  102. // 初始化XHR监听
  103. detectXHR();
  104. })();

QingJ © 2025

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