Replace Zhihu Links with Google Search

将知乎中的指定链接替换为Google搜索关键词的链接

  1. // ==UserScript==
  2. // @name Replace Zhihu Links with Google Search
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 将知乎中的指定链接替换为Google搜索关键词的链接
  6. // @match *://*.zhihu.com/*
  7. // @grant none
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. // 函数:替换链接为Google搜索
  15. function replaceLinks() {
  16. const links = document.querySelectorAll('a.RichContent-EntityWord[data-paste-text="true"]');
  17. links.forEach(link => {
  18. const keyword = link.textContent;
  19. if (keyword) {
  20. link.href = `https://www.google.com/search?q=${encodeURIComponent(keyword)}`;
  21. link.target = "_blank"; // 新标签页打开
  22. }
  23. });
  24. }
  25.  
  26. // 监听页面变动以实时更新
  27. const observer = new MutationObserver(() => {
  28. replaceLinks();
  29. });
  30.  
  31. // 监控整个页面
  32. observer.observe(document.body, { childList: true, subtree: true });
  33.  
  34. // 初始执行
  35. replaceLinks();
  36. })();