我在浏览帖子的时候发现,直接左键打开帖子会在原标签页打开,然后返回的到话题列表会移动,不太方便确认上次浏览到的位置。
这个脚本就是左键点击帖子然后在新标签页打开,然后浏览完成直接关闭就行了,话题列表不会受到影响。
油猴脚本
// ==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
快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。
17 条回复
排序