remove_redirect

remove redirects in zhihu.com

Verze ze dne 04. 02. 2025. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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;
            }
          }
        }
      }
    });
  });

})();