挖坟

要实现在网页切换后仍能运行的挖坟脚本,通常需要将脚本集成到浏览器的扩展程序中,这样可以确保脚本在页面切换后继续运行。以下是一种基本的方法,使用 Chrome 浏览器的扩展程序来实现:

使用 Chrome 扩展程序

  1. 创建 Chrome 扩展程序

    • 创建一个新的文件夹,例如 fossil-script-extension
    • 在该文件夹下创建以下文件:

      • manifest.json:用于描述扩展程序的配置信息。
      • contentScript.js:用于编写实际的挖坟脚本。
  2. 编辑 manifest.json 文件

    • manifest.json 文件用于配置扩展程序的基本信息和权限。示例内容如下:

      {
          "manifest_version": 2,
          "name": "Fossil Script",
          "version": "1.0",
          "description": "A script for digging up old posts on linux.do",
          "permissions": [
              "activeTab",
              "storage",
              "https://linux.do/"
          ],
          "content_scripts": [
              {
                  "matches": ["https://linux.do/*"],
                  "js": ["contentScript.js"]
              }
          ],
          "browser_action": {
              "default_popup": "popup.html",
              "default_icon": {
                  "16": "images/icon16.png",
                  "48": "images/icon48.png",
                  "128": "images/icon128.png"
              }
          },
          "icons": {
              "16": "images/icon16.png",
              "48": "images/icon48.png",
              "128": "images/icon128.png"
          }
      }
      
      • permissions 字段指定了扩展程序需要的权限,这里包括访问 https://linux.do/ 网站。
      • content_scripts 字段指定了在哪些网站(https://linux.do/*)上运行脚本,并指定了要运行的脚本文件 contentScript.js
  3. 编辑 contentScript.js 文件

    • contentScript.js 文件包含实际的挖坟脚本逻辑,这里使用与之前示例类似的代码。

      async function fetchPostData(topicNumber) {
          let url = `https://linux.do/t/topic/${topicNumber}`;
          let response = await fetch(url);
          let html = await response.text();
          let parser = new DOMParser();
          let doc = parser.parseFromString(html, 'text/html');
      
          let postAuthor = doc.querySelector('.post-author').textContent.trim();
          let postTimeStr = doc.querySelector('.post-time').getAttribute('datetime');
          let postTime = new Date(postTimeStr);
      
          return { postAuthor, postTime };
      }
      
      async function runFossilScript() {
          let topicNumber = 1;
          let latestPostTime = null;
      
          while (true) {
              try {
                  let { postAuthor, postTime } = await fetchPostData(topicNumber);
      
                  // 检查发帖人是否为 "neo",如果是则忽略该话题
                  if (postAuthor === 'neo') {
                      topicNumber++;
                      continue;
                  }
      
                  // 检查帖子时间是否早于6月
                  if (postTime < new Date('2023-06-01')) {
                      // 更新最晚帖子时间
                      if (latestPostTime === null || postTime > latestPostTime) {
                          latestPostTime = postTime;
                      }
                  }
      
                  topicNumber++;
              } catch (error) {
                  console.error(`Error fetching data for topic ${topicNumber}:`, error);
                  topicNumber++;  // 继续下一个话题
              }
          }
      
          // 检查是否存在晚于6月的帖子
          if (latestPostTime === null || latestPostTime < new Date('2023-06-01')) {
              let month = new Date().getMonth() + 1;  // 获取当前月份
              console.log(`${month}月的帖子了,挖坟`);
          } else {
              console.log('找到晚于6月的帖子,停止挖坟');
          }
      }
      
      runFossilScript();
      
  4. 加载扩展程序到 Chrome

    • 打开 Chrome 浏览器,进入扩展程序管理页面(chrome://extensions/)。
    • 在页面右上角打开开发者模式。
    • 点击“加载已解压的扩展程序”,选择之前创建的 fossil-script-extension 文件夹。
  5. 运行和测试

    • 打开 linux.do 论坛的页面,确保扩展程序已启用。
    • 打开开发者工具的 Console 标签,观察挖坟脚本的输出和运行情况。

注意事项:

  • 页面切换后的持续运行: 使用扩展程序可以确保脚本在页面切换后仍能持续运行,而不会受到单个页面的限制。
  • 网络请求和权限: 确保在 manifest.json 中正确配置了所需的权限,例如 https://linux.do/ 的访问权限。
  • 错误处理和调试: 使用开发者工具的 Console 标签来查看脚本的输出和可能的错误信息,以便调试和改进脚本逻辑。

通过以上步骤,您可以创建一个能在 Chrome 浏览器中持续挖坟的扩展程序,适用于访问 linux.do 论坛的场景。