SegmentFault real links

去除思否网页中的外链跳转,并将其替换为真实地址

  1. // ==UserScript==
  2. // @name SegmentFault real links
  3. // @name:zh-CN SegmentFault 思否真实链接
  4. // @namespace https://github.com/wxh06
  5. // @version 0.0.1
  6. // @description 去除思否网页中的外链跳转,并将其替换为真实地址
  7. // @author 汪心禾
  8. // @license MIT
  9. // @supportURL https://github.com/wxh06/segmentfault-real-links/issues
  10. // @match https://segmentfault.com/*
  11. // @icon https://static.segmentfault.com/main_site_next/df1f59ce/favicon.ico
  12. // @grant GM.xmlHttpRequest
  13. // @connect link.segmentfault.com
  14. // ==/UserScript==
  15.  
  16. "use strict";
  17.  
  18. const observer = new IntersectionObserver((entries) => {
  19. entries.forEach((entry) => {
  20. if (entry.isIntersecting) {
  21. GM.xmlHttpRequest({
  22. method: "GET",
  23. url: entry.target.getAttribute("href"),
  24. /** @param {GM.Response} response */
  25. onload(response) {
  26. let { responseXML } = response;
  27. if (!responseXML) {
  28. responseXML = new DOMParser().parseFromString(
  29. response.responseText,
  30. "text/html",
  31. );
  32. }
  33. entry.target.setAttribute(
  34. "href",
  35. responseXML.querySelector("[data-url]").getAttribute("data-url"),
  36. );
  37. observer.unobserve(entry.target);
  38. },
  39. });
  40. }
  41. });
  42. });
  43.  
  44. /** @param {Element} element */
  45. function observeAllLinks(element) {
  46. element
  47. .querySelectorAll('[href^="https://link.segmentfault.com/?enc="]')
  48. .forEach((e) => observer.observe(e));
  49. }
  50.  
  51. observeAllLinks(document.body);
  52. document.body.addEventListener("DOMNodeInserted", (event) => {
  53. if (event.target && event.target.nodeType !== Node.TEXT_NODE) {
  54. observeAllLinks(event.target);
  55. }
  56. });