SegmentFault real links

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

目前為 2023-08-18 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         SegmentFault real links
// @name:zh-CN   SegmentFault 思否真实链接
// @namespace    https://github.com/wxh06
// @version      0.0.1
// @description  去除思否网页中的外链跳转,并将其替换为真实地址
// @author       汪心禾
// @license      MIT
// @supportURL   https://github.com/wxh06/segmentfault-real-links/issues
// @match        https://segmentfault.com/*
// @icon         https://static.segmentfault.com/main_site_next/df1f59ce/favicon.ico
// @grant        GM.xmlHttpRequest
// @connect      link.segmentfault.com
// ==/UserScript==

"use strict";

const observer = new IntersectionObserver((entries) => {
  entries.forEach((entry) => {
    if (entry.isIntersecting) {
      GM.xmlHttpRequest({
        method: "GET",
        url: entry.target.getAttribute("href"),
        /** @param {GM.Response} response  */
        onload(response) {
          let { responseXML } = response;
          if (!responseXML) {
            responseXML = new DOMParser().parseFromString(
              response.responseText,
              "text/html",
            );
          }
          entry.target.setAttribute(
            "href",
            responseXML.querySelector("[data-url]").getAttribute("data-url"),
          );
          observer.unobserve(entry.target);
        },
      });
    }
  });
});

/** @param {Element} element */
function observeAllLinks(element) {
  element
    .querySelectorAll('[href^="https://link.segmentfault.com/?enc="]')
    .forEach((e) => observer.observe(e));
}

observeAllLinks(document.body);
document.body.addEventListener("DOMNodeInserted", (event) => {
  if (event.target && event.target.nodeType !== Node.TEXT_NODE) {
    observeAllLinks(event.target);
  }
});