Automatic URL Decoder

Декодирует все найденные на странице ссылки, похожие на "%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%BC%D0%B8%D1%80", в удобочитаемое "привет мир"

Stan na 05-04-2018. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name Automatic URL Decoder
// @namespace https://github.com/T1mL3arn
// @description:ru Декодирует все найденные на странице ссылки, похожие на "%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%BC%D0%B8%D1%80", в удобочитаемое "привет мир"
// @description:en It decodes all cyrillic links on current page.
// @match *://*/*
// @version 1.0
// @run-at document-end
// @license GPLv3
// @supportURL https://github.com/T1mL3arn/Automatic-URL-Decoder/issues
// @homepageURL https://github.com/T1mL3arn/Automatic-URL-Decoder
// @description Декодирует все найденные на странице ссылки, похожие на "%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%BC%D0%B8%D1%80", в удобочитаемое "привет мир"
// ==/UserScript==

let linkEreg = /(https|http|ftp|file):.+?(?=\s|$)/gi;
let linkEregLocal = /(https|http|ftp|file):.+?(?=\s|$)/i;
let obsOptions = { childList: true, subtree: true };

let underlineStyle = new Map([["border-bottom","2px solid currentColor"], ["margin-bottom", "-2px"]]);
// set one of style from above to enable 
// custom style for fixed links
let changedLinkStyle = null;

let obs = new MutationObserver((changes, obs) => {
  changes.forEach((change) => change.addedNodes.forEach((node) => fixLinks(node)) );
});
obs.observe(document.body, obsOptions);

function fixLinks(node) {
  if (node.nodeType === 3) {
    let content = node.textContent;
    if (content && content != '') {
      if (linkEregLocal.test(content)) {
        if (changedLinkStyle)
          replaceAndStyleLink(node);
        else {
          let decoded = content.replace(linkEreg, decodeURIComponent);
          if(decoded.length!=content.length)
            node.textContent = decoded;
        }
      }
    }
  } else if (node.childNodes && node.childNodes.length > 0) {
    node.childNodes.forEach(fixLinks);
  }
}

function replaceAndStyleLink(node) {
  let match;
  let offset = 0;
  let sibling = node;
  let content = node.textContent;
  obs.disconnect();
  while ((match = linkEreg.exec(content)) != null) {
    let fullMatch = match[0];
    let decoded = decodeURIComponent(fullMatch);
    if (decoded.length != fullMatch.length) {
      let span = document.createElement('span');

      changedLinkStyle.forEach((val, key) => span.style.setProperty(key, val));
      
      let range = document.createRange();
      range.setStart(sibling, linkEreg.lastIndex - match[0].length);
      range.setEnd(sibling, linkEreg.lastIndex);
      range.surroundContents(span);
      
      sibling = getNextTextSibling(span);
      content = sibling.textContent;
      span.textContent = decoded;
      linkEreg.lastIndex = 0;

      if (sibling == null)
        break;
    }
  }
  obs.observe(document.body, obsOptions);
}

function getNextTextSibling(node) {
  let next = node.nextSibling;
  while (next != null) {
    if (next.nodeType == 3)
      return next;
    else
      next = node.nextSibling;
  }
  return null;
}

fixLinks(document.body);