remove_redirect

remove redirects in zhihu.com

Від 04.02.2025. Дивіться остання версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         remove_redirect
// @namespace    https://github.com/mikelxk/remove_redirect
// @version      0.0.5
// @author       mikelxk
// @description  remove redirects in zhihu.com
// @license      MIT
// @icon         https://vitejs.dev/logo.svg
// @match        *://*.zhihu.com/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  const updatedLinks = /* @__PURE__ */ new WeakSet();
  function updateLinkHref(link) {
    if (link.href.includes("https://link.zhihu.com/?target=") || link.href.includes("http://link.zhihu.com/?target=")) {
      const urlParams = new URLSearchParams(link.href.split("?")[1]);
      const newLink = urlParams.get("target");
      if (newLink) {
        link.href = decodeURIComponent(newLink);
        return link.href;
      }
    }
    return null;
  }
  function updateAllLinks() {
    const links = document.querySelectorAll("a");
    links.forEach((link) => updateLinkHref(link));
  }
  updateAllLinks();
  const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      if (mutation.type === "childList") {
        mutation.addedNodes.forEach((node) => {
          if (node.nodeType === Node.ELEMENT_NODE) {
            const element = node;
            if (element.tagName === "A") {
              updateLinkHref(element);
            } else {
              const links = element.querySelectorAll("a");
              links.forEach((link) => updateLinkHref(link));
            }
          }
        });
      }
    });
  });
  observer.observe(document.body, {
    childList: true,
    subtree: true
  });
  const listenedEvents = ["click", "mouseover"];
  listenedEvents.forEach((eventName) => {
    document.addEventListener(eventName, (event) => {
      const target = event.target;
      if (target.tagName === "A") {
        const link = target;
        if (!updatedLinks.has(link)) {
          const newHref = updateLinkHref(link);
          if (newHref) {
            updatedLinks.add(link);
            if (eventName === "click") {
              event.preventDefault();
              window.location.href = newHref;
            }
          }
        }
      }
    });
  });

})();