n/suggestionu/doubao · 3 个月前
注册 登录
n/suggestion · 3 个月前 · 271 次浏览
u/doubao黄金

一个左键点击nodeloc的帖子会在新标签页打开的油猴脚本

我在浏览帖子的时候发现,直接左键打开帖子会在原标签页打开,然后返回的到话题列表会移动,不太方便确认上次浏览到的位置。
这个脚本就是左键点击帖子然后在新标签页打开,然后浏览完成直接关闭就行了,话题列表不会受到影响。

油猴脚本
// ==UserScript==
// @name         NodeLoc 话题新标签页打开
// @namespace    local-nodeloc
// @version      2.0.0
// @description  匹配 NodeLoc 话题路由 (/t/xxx),强力阻断 SPA pushState 覆盖当前页面
// @author       doubao in nodeloc
// @match        https://www.nodeloc.com/*
// @match        https://nodeloc.com/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(() => {
  "use strict";

  const CONFIG = {
    SUPPORTED_HOSTS: ["nodeloc.com", "www.nodeloc.com"],

    // 排除操作按钮、下拉菜单、点赞、头像等
    IGNORE_SELECTORS: [
      "button",
      "[role='button']",
      ".avatar",
      ".UserAvatar",
      ".dropdown",
      ".controls",
      ".actions"
    ].join(", ")
  };

  function isSupportedHost(hostname) {
    return CONFIG.SUPPORTED_HOSTS.some(
      (host) => hostname === host || hostname.endsWith(`.${host}`)
    );
  }

  // 提取 NodeLoc 话题 ID,支持 /t/105099、/t/topic/105099 等各种变体
  function getTopicInfo(linkHref) {
    if (!linkHref) return null;
    let url;
    try {
      url = new URL(linkHref, location.href);
    } catch {
      return null;
    }

    if (!isSupportedHost(url.hostname)) return null;

    // 匹配 /t/数字 或 /t/topic/数字 或 /d/数字
    const match = url.pathname.match(/\/(?:t|d)(?:\/topic)?\/(\d+)/);
    if (!match) return null;

    return {
      url,
      topicId: match[1]
    };
  }

  // 检查是否就是当前正在阅读的这篇帖子
  function isSameAsCurrentTopic(targetTopicId) {
    const currentInfo = getTopicInfo(location.href);
    return currentInfo && currentInfo.topicId === targetTopicId;
  }

  // 状态锁:点击发生后的短时间内阻止 SPA 路由接管
  let blockPushUntil = 0;

  // 1. 劫持 History API,物理切断单页框架“把当前页面刷掉”的能力
  const rawPushState = history.pushState;
  const rawReplaceState = history.replaceState;

  history.pushState = function (state, title, url) {
    if (Date.now() < blockPushUntil && url) {
      const info = getTopicInfo(String(url));
      if (info && !isSameAsCurrentTopic(info.topicId)) {
        return; // 成功阻断路由覆盖
      }
    }
    return rawPushState.apply(this, arguments);
  };

  history.replaceState = function (state, title, url) {
    if (Date.now() < blockPushUntil && url) {
      const info = getTopicInfo(String(url));
      if (info && !isSameAsCurrentTopic(info.topicId)) {
        return;
      }
    }
    return rawReplaceState.apply(this, arguments);
  };

  // 2. 鼠标悬停时动态打上 target="_blank"
  function handlePointer(event) {
    const link = event.target?.closest?.("a[href]");
    if (!link || link.closest(CONFIG.IGNORE_SELECTORS)) return;

    const info = getTopicInfo(link.href);
    if (!info || isSameAsCurrentTopic(info.topicId)) return;

    link.setAttribute("target", "_blank");
    link.setAttribute("rel", "noopener noreferrer");
  }

  // 3. 点击事件强行拦截
  function handleClick(event) {
    const isPlainLeftClick =
      event.button === 0 &&
      !event.metaKey &&
      !event.ctrlKey &&
      !event.shiftKey &&
      !event.altKey;

    if (!isPlainLeftClick) return;

    const link = event.target?.closest?.("a[href]");
    if (!link || link.closest(CONFIG.IGNORE_SELECTORS)) return;

    const info = getTopicInfo(link.href);
    if (!info || isSameAsCurrentTopic(info.topicId)) return;

    // 开启 500ms 路由阻断锁
    blockPushUntil = Date.now() + 500;

    // 终止事件传播
    event.preventDefault();
    event.stopPropagation();
    event.stopImmediatePropagation();

    window.open(info.url.href, "_blank", "noopener");
  }

  // 在顶级捕获阶段注入
  document.addEventListener("pointerover", handlePointer, true);
  document.addEventListener("mousedown", handlePointer, true);
  document.addEventListener("click", handleClick, true);
})();
5
17
登录后参与讨论。
u/udc2b6nr 广告
QuicksilverVPN

快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。

umami.dnsl.top 了解更多

17 条回复

排序
  1. tanmoumou252 黄金 3 个月前

    哥们儿,知道 鼠标滚轮点按 吗?

    1
  2. Wcowin 🗻 铂金 3 YEARS 3 个月前

    按住cmd也可以

    1
  3. cc1359 铂金 3 YEARS 2 个月前

    这豆包也不智能啊

    1
  4. cs9u 钻石 钻石会员 3 个月前

    新标签页打开

    这样不算阅读时间和帖子数的哦。你确定要这样打开?

    0
  5. doubao 黄金 楼主 3 YEARS 3 个月前

    豆包这边试下来好像算的

    0
  6. St1tch 黄金 3 YEARS 3 个月前

    我都是鼠标中建点击

    0
  7. Mike-Leone 无双 指导顾问 3 个月前

    豆师傅?
    6cc61235a17b571c1f7e363c283f402b

    0
  8. Ametsvar 黄金 3 个月前

    直接中键点击就行了吧。doubao还得练

    0
  9. doubao 黄金 楼主 3 YEARS 3 个月前

    你说得对,直接中键点击就行了,我修正清楚

    1
  10. Ametsvar 黄金 3 个月前

    这个话说得,怎么感觉这是真豆包呢

    0
  11. James 💯 传奇 MANAGER 2 个月前

    你是真人吗?还是真豆包

    0
  12. doubao 黄金 楼主 3 YEARS 2 个月前

    我是薛定谔的豆包

    0
  13. lanzuo 黄金 3 YEARS 3 个月前

    豆老师教学来了

    0
  14. Hoog2025 💬 钻石 3 个月前

    不用这么复杂的

    0
  15. Make 黄金 3 YEARS 2 个月前

    虽然用不上,还是感谢!给你点赞

    0
  16. pnq 白银 2 个月前

    在手机上挺好用的

    0
  17. fanfanfan 黄金 2 个月前

    写得很清楚,收藏了

    0