remove_redirect

remove redirects in zhihu.com

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey 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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         remove_redirect
// @namespace    https://github.com/mikelxk/remove_redirect
// @version      0.1.0
// @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();
  const ZHIHU_PATTERNS = [
    "https://link.zhihu.com/?target=",
    "http://link.zhihu.com/?target="
  ];
  function debounce(fn, delay) {
    let timeoutId;
    return (...args) => {
      clearTimeout(timeoutId);
      timeoutId = setTimeout(() => fn(...args), delay);
    };
  }
  function updateLinkHref(link) {
    try {
      const href = link.href;
      if (!ZHIHU_PATTERNS.some((pattern) => href.includes(pattern))) {
        return null;
      }
      const urlParams = new URLSearchParams(href.split("?")[1]);
      const newLink = urlParams.get("target");
      if (!newLink) return null;
      const decodedLink = decodeURIComponent(newLink);
      link.href = decodedLink;
      return decodedLink;
    } catch (error) {
      console.error("Error updating link:", error);
      return null;
    }
  }
  function updateAllLinks() {
    const links = document.getElementsByTagName("a");
    Array.from(links).forEach((link) => updateLinkHref(link));
  }
  const debouncedObserverCallback = debounce((mutations) => {
    mutations.forEach((mutation) => {
      if (mutation.type === "childList") {
        mutation.addedNodes.forEach((node) => {
          if (node instanceof HTMLElement) {
            if (node instanceof HTMLAnchorElement) {
              updateLinkHref(node);
            } else {
              const links = node.getElementsByTagName("a");
              Array.from(links).forEach((link) => updateLinkHref(link));
            }
          }
        });
      }
    });
  }, 100);
  document.addEventListener("DOMContentLoaded", updateAllLinks);
  const observer = new MutationObserver(debouncedObserverCallback);
  observer.observe(document.body, {
    childList: true,
    subtree: true
  });
  window.addEventListener("unload", () => {
    observer.disconnect();
  });
  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;
            }
          }
        }
      }
    });
  });

})();