知乎专栏外链自动跳转

专为知乎专栏优化的外链自动跳转,支持最新页面结构

  1. // ==UserScript==
  2. // @name 知乎专栏外链自动跳转
  3. // @namespace https://github.com/leiqichn
  4. // @version 3.1
  5. // @description 专为知乎专栏优化的外链自动跳转,支持最新页面结构
  6. // @author Lei Qi
  7. // @match https://zhuanlan.zhihu.com/*
  8. // @grant none
  9. // @license MIT
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 深度解码URL(处理知乎新的多层编码)
  17. const decodeZhihuUrl = (encodedUrl) => {
  18. try {
  19. let decoded = encodedUrl;
  20. // 检测是否包含多重编码特征
  21. while(/%[0-9A-Fa-f]{2}/.test(decoded)) {
  22. decoded = decodeURIComponent(decoded);
  23. }
  24. // 处理可能的Base64编码(新发现的特征)
  25. if(decoded.startsWith('http%3A') && decoded.length > 100){
  26. const b64Match = decoded.match(/b64=(.*?)(&|$)/);
  27. if(b64Match){
  28. return atob(b64Match[1]);
  29. }
  30. }
  31. return decoded;
  32. } catch(e) {
  33. console.warn('URL解码失败:', e);
  34. return encodedUrl;
  35. }
  36. };
  37.  
  38. // 强化链接处理
  39. const enhanceLinks = () => {
  40. document.querySelectorAll('a').forEach(link => {
  41. try {
  42. const urlObj = new URL(link.href);
  43.  
  44. // 匹配知乎专栏专用跳转模式
  45. const isZhihuRedirect =
  46. urlObj.hostname === 'link.zhihu.com' ||
  47. urlObj.pathname === '/platform-api/redirect';
  48.  
  49. if(isZhihuRedirect && urlObj.searchParams.has('target')){
  50. // 处理多层编码的target参数
  51. const rawTarget = urlObj.searchParams.get('target');
  52. const finalUrl = decodeZhihuUrl(rawTarget);
  53.  
  54. // 创建新链接并保留重要属性
  55. const newLink = link.cloneNode(true);
  56. newLink.href = finalUrl;
  57.  
  58. // 清除知乎的事件监听(重要!)
  59. newLink.replaceWith(...newLink.childNodes);
  60. link.parentNode.replaceChild(newLink, link);
  61. }
  62. } catch(e) {
  63. // 忽略无效链接
  64. }
  65. });
  66. };
  67.  
  68. // 使用高性能MutationObserver
  69. const observer = new MutationObserver(mutations => {
  70. if(!mutations) return;
  71. for(const mutation of mutations) {
  72. if(mutation.type === 'childList') {
  73. enhanceLinks();
  74. }
  75. }
  76. });
  77.  
  78. // 劫持所有点击事件(优先级最高)
  79. document.addEventListener('click', e => {
  80. const link = e.target.closest('a');
  81. if(link) {
  82. const href = link.href;
  83. // 匹配知乎专栏外链特征
  84. if(href && /(link\.zhihu\.com|redirect\/target)/.test(href)) {
  85. e.stopImmediatePropagation();
  86. e.preventDefault();
  87. window.location.href = decodeZhihuUrl(
  88. new URL(href).searchParams.get('target') || href
  89. );
  90. return false;
  91. }
  92. }
  93. }, true); // 捕获阶段执行
  94.  
  95. // 初始化设置
  96. observer.observe(document, {
  97. subtree: true,
  98. childList: true,
  99. attributes: false,
  100. characterData: false
  101. });
  102.  
  103. // 即时处理现有内容
  104. enhanceLinks();
  105.  
  106. // 防御知乎动态脚本(每500ms检查一次)
  107. setInterval(() => {
  108. // 清除知乎的跳转弹窗
  109. const modals = document.querySelectorAll('.Modal-wrapper, .SecurityModal');
  110. modals.forEach(modal => modal.remove());
  111.  
  112. // 覆盖弹窗相关方法
  113. window.alert = function(){};
  114. window.confirm = function(){ return true; };
  115. }, 500);
  116.  
  117. // 伪装成正常流量
  118. Object.defineProperty(navigator, 'userAgent', {
  119. value: navigator.userAgent.replace(/Tampermonkey|HeadlessChrome/, ''),
  120. configurable: false,
  121. enumerable: true,
  122. writable: false
  123. });
  124. })();

QingJ © 2025

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