remove_redirect

remove redirects in zhihu.com

Versione datata 04/02/2025. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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

})();