Douban无缝转发

自动加载豆瓣帖子中的特定格式转发内容,从当前页开始加载后续页面内容,判断最后一页

  1. // ==UserScript==
  2. // @name Douban无缝转发
  3. // @namespace https://www.douban.com/
  4. // @version 0.1
  5. // @description 自动加载豆瓣帖子中的特定格式转发内容,从当前页开始加载后续页面内容,判断最后一页
  6. // @match https://www.douban.com/group/topic/*?type=rec*
  7. // @grant GM_xmlhttpRequest
  8. // ==/UserScript==
  9.  
  10. (function () {
  11. 'use strict';
  12.  
  13. let isLoading = false;
  14. let hasNextPage = true;
  15.  
  16. // 获取帖子 ID
  17. const topicId = window.location.pathname.match(/\/topic\/(\d+)\//)[1];
  18.  
  19. // 确定起始页码
  20. const urlParams = new URLSearchParams(window.location.search);
  21. let page = urlParams.has('start') ? parseInt(urlParams.get('start')) / 30 + 1 : 1; // 从当前页的下一页开始
  22.  
  23. // 插入符合条件的转发内容
  24. function addRepostsToPage(reposts) {
  25. const container = document.querySelector('.list.topic-rec-list ul');
  26. reposts.forEach(repost => {
  27. const hasRequiredElements = repost.querySelector('.pic') &&
  28. repost.querySelector('.content') &&
  29. repost.querySelector('.pubtime');
  30. if (hasRequiredElements) {
  31. container.appendChild(repost.cloneNode(true));
  32. }
  33. });
  34. }
  35.  
  36. // 加载下一页
  37. function loadMoreReposts() {
  38. if (isLoading || !hasNextPage) return;
  39. isLoading = true;
  40.  
  41. // 构建 URL
  42. const url = `https://www.douban.com/group/topic/${topicId}/?type=rec&start=${page * 30}`;
  43.  
  44. GM_xmlhttpRequest({
  45. method: 'GET',
  46. url: url,
  47. onload: function (response) {
  48. const parser = new DOMParser();
  49. const doc = parser.parseFromString(response.responseText, 'text/html');
  50. const reposts = doc.querySelectorAll('.list.topic-rec-list ul > li');
  51.  
  52. if (reposts.length === 0) {
  53. hasNextPage = false;
  54. return;
  55. }
  56.  
  57. addRepostsToPage(reposts);
  58.  
  59. // 检查是否还有下一页
  60. const nextButton = doc.querySelector('.paginator > span.next > a');
  61. if (!nextButton) {
  62. hasNextPage = false;
  63. }
  64.  
  65. isLoading = false;
  66. page++; // 增加页数,准备加载下一页
  67. },
  68. onerror: function () {
  69. isLoading = false;
  70. }
  71. });
  72. }
  73.  
  74. // 监听滚动事件,滚动到底部时加载更多
  75. window.addEventListener('scroll', function () {
  76. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 200) {
  77. loadMoreReposts();
  78. }
  79. });
  80.  
  81. // 初始不加载,等用户滚动到底部时再加载下一页
  82. })();

QingJ © 2025

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