Disqus - No link/href redirection

Remove link analysis and redirection in links in disqus comments.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Disqus - No link/href redirection
// @namespace    http://xa.universe/
// @version      0.2.2
// @description  Remove link analysis and redirection in links in disqus comments.
// @author       XA
// @match        https://disqus.com/*/comments/*
// @grant        none
// ==/UserScript==

// Copyright (C) XA, IX 2017.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
// * Neither the name of XA nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

(function() {
  'use strict';
  const cRedirector = 'https://www.google.de/url?url=URL';
  const cMetaRefresh = 'data:text/html;charset=utf-8,%3Chtml%3E%3Chead%3E%3Cmeta%20http-equiv%3D%22refresh%22%20content%3D%221%3B%20url%3DURL%22%20%2F%3E%3C%2Fhead%3E%3Cbody%3E%3C%2Fbody%3E%3C%2Fhtml%3E';
                       // encodeURIComponent('<html><head><meta http-equiv="refresh" content="1; url=URL" /></head><body></body></html>')

  const DEBUG = 0;
  const USEREDIRECTOR = false;



  function l (...args) {
    if (!DEBUG) return;
    console.log(...args);
  }


  function processHrefAsync (ePosting) {
    return new Promise(
      (resolve, reject) => {
        l("++++++++ [processHrefAsync]:", ePosting);
        let eHrefs = ePosting.querySelectorAll(".post-message a[rel~=nofollow]");
        if (!eHrefs) reject();
        for (let eHref of eHrefs) {
          l("<<<<<<<<", eHref.href);

          let mat = eHref.href.match(/^https?:\/\/disq\.us\/url\?url=(.+)/);
          if (mat === null) continue;
          let url = mat[1];
          if (!url) continue;

          url = decodeURIComponent(url);
          mat = url.match(/^(.+):.+?$/); // crop suffix ":SoMeCoDe&cuid=1234"
          if (mat !== null) {
            url = mat[1];
          }
          if (!url) continue;

          if (USEREDIRECTOR) {
            url = USEREDIRECTOR.replace("URL", url);
          } else {
            url = decodeURIComponent(url);
          }
          l(">>>>>>>>", url);
          eHref.href = url;
          if (!eHref.rel) {
            eHref.rel = "noreferrer";
          } else {
            eHref.rel += " noreferrer";
          }
        }
        resolve();
      }
    );
  }


  function observeAddingPosts (eRoot) {
    let observer = new MutationObserver(
      (mutationObjs, observer) => {
        l(mutationObjs);
        for (let mobj of mutationObjs) {
          if (mobj.type != 'childList' || !mobj.addedNodes || mobj.addedNodes.length === 0) return;
          for (let elt of mobj.addedNodes) {
            if (elt.id.substr(0,5) === 'post-') {
              processHrefAsync(elt);
            }
          }
        }
      } // Observer callback
    );

    observer.observe(eRoot, {childList: true, subtree: true});
  } // observeAddingPosts


  let observer = observeAddingPosts(document.body);
  //document.addEventListener("unload", (ev) => { l("********** Obeserver UNLOADED."); observer.disconnect(); });


})();