每次在掘金看文章想点链接,就像闯关打怪——先被拦截页“拦路”,再手动点击“续命”,CV工程师的键盘都要按冒烟了🔥!
🤔 问题根源:掘金的“中间商套路”
掘金偷偷给所有链接加了“中介前缀”(https://link.juejin.cn?target=),导致跳转时强制弹窗。你以为直接替换链接就能解决?Too young!
// ==UserScript==
// @name 掘金重定向链接转直链
// @namespace http://tampermonkey.net/
// @version 2025-05-20
// @description try to take over the world!
// @author You
// @match https://juejin.cn/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=juejin.cn
// @run-at document-idle
// @grant none
// ==/UserScript==
(()=> {
'use strict';
const linkKeyword = "?target=";
const aLinks = document.querySelectorAll(
`a[href*="${linkKeyword}"]:not([data-redirect-skipper])`
);
if(!aLinks.length)return;
aLinks.forEach((a) => {
a.href=a.title;
a.setAttribute("data-redirect-skipper", "true"); // 避免重复处理
console.log(a)//打印替换结果
});
})();
💣 初战翻车:脚本为啥不生效?
原计划:用脚本把a标签的href属性换成title里的真实链接,简单粗暴!
翻车现场:代码明明打印成功,页面却纹丝不动❓
真相:掘金是单页应用(SPA) ,内容像变魔术一样动态加载,DOM更新后脚本直接“懵圈”了!
🚀 终极方案:给DOM加“监控探头”
既然页面爱玩“实时更新”,咱们就用MutationObserver当“监控探头”,DOM一变立刻触发脚本!
但新问题又来了——页面疯狂更新时,脚本会像“饿狼扑食”一样高频运行,直接让浏览器卡成PPT😱……
const linkKeyword = "?target=";
function updateLinks() {
const aLinks = document.querySelectorAll(
`a[href*="${linkKeyword}"]:not([data-redirect-skipper])`
);
if(!aLinks.length)return;
aLinks.forEach((a) => {
const href = a.href;
if (a.title && a.href !== a.title) {
a.href = a.title;
a.setAttribute("data-redirect-skipper", "true"); // 避免重复处理
console.log("[Updated]", a);
}
});
}
// 初始化处理一次
updateLinks();
// 使用 MutationObserver 监听 DOM 变化
const observer = new MutationObserver(() => {
updateLinks();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
优化绝招:加个 “冷静期” (节流函数),让脚本每秒最多跑一次,既流畅又能“精准打击”!
function throttle(fn, wait) {
let lastTime = 0;
return function (...args) {
const now = Date.now();
if (now - lastTime >= wait) {
lastTime = now;
fn.apply(this, args);
}
};
}
🎯 最终效果
- 链接点击秒开无弹窗,丝滑如德芙🍫
- 脚本自动监听新内容,一劳永逸
- 代码仅需20行,小白也能轻松上手
// ==UserScript==
// @name 掘金重定向链接转直链
// @namespace http://tampermonkey.net/
// @version 2025-05-20
// @description try to take over the world!
// @author You
// @match https://juejin.cn/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=juejin.cn
// @run-at document-idle
// @grant none
// ==/UserScript==
(()=> {
'use strict';
function updateLinks() {
const linkKeyword = "?target=";
const aLinks = document.querySelectorAll(
`a[href*="${linkKeyword}"]:not([data-redirect-skipper])`
);
if(!aLinks.length)return;
aLinks.forEach((a) => {
const href = a.href;
if (a.title && a.href !== a.title) {
a.href = a.title;
a.setAttribute("data-redirect-skipper", "true"); // 避免重复处理
}
});
}
// 使用 MutationObserver 监听 DOM 变化
const throttledUpdateLinks = throttle(updateLinks, 1000);
const observer = new MutationObserver(() => {
throttledUpdateLinks();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})();
function throttle(fn, wait) {
let lastTime = 0;
return function (...args) {
const now = Date.now();
if (now - lastTime >= wait) {
lastTime = now;
fn.apply(this, args);
}
};
}
📌 总结
技术宅的快乐:用代码把繁琐操作变成“魔法按钮”!
下次遇到网页套路,别急着暴躁—— “监控探头”+“冷静期” ,分分钟教它做人!💪
✨ 从此,CV工程师的键盘寿命延长了99%……