open all links in the new tab

except for the link in the same directory(the link whose web address are same before the last '/' as the current url)

Version vom 11.11.2017. Aktuellste Version

// ==UserScript==
// @name           open all links in the new tab
// @description    except for the link in the same directory(the link whose web address are same before the last '/' as the current url)
// @include        http://*
// @include        https://*
// @author         yechenyin
// @version        0.8
// @namespace 	   https://greasyfork.org/users/3586-yechenyin
// @grant       	 GM_openInTab
// ==/UserScript==
(function() {
  "use strict";

  var exception = ['https://m.leiphone.com/page/'];
  function getAncestorLink(element) {
    while (element && element.nodeName != "A") {
      element = element.parentNode;
    }
    if (element.nodeName && element.nodeName === "A" && element.href && element.href.indexOf('http') === 0)
      return element;
  }

  String.prototype.matched = function(strings) {
    for (var i = 0; i < strings.length; i++) {
      if (typeof strings[i] == 'string' && this.indexOf(strings[i]) === 0)
        return true;
      else if (strings[i] instanceof RegExp && this.test(strings[i]))
        return true;
    }
    return false;
  };


  var setLinkAction = function(node) {
    if (node && node.nodeName && node.nodeName === "A" && node.href && node.href.indexOf('http') === 0)
      node.addEventListener('click', function(e) {
        if (this && this.href && !/^(\d+|Next|Prev|>|<|下一页|上一页|下一頁|上一頁|回首頁|次へ|前へ)$/.test(this.innerText) && !this.href.matched(exception)) {
          console.log(this, e.target);
          e.preventDefault();
          e.stopPropagation();
          window.open(this.href);
        } else if (this && this.href && this.href.indexOf('http') === 0) {
          e.preventDefault();
          e.stopPropagation();
          location.href = this.href;
        }
      });

  };

  for (var i = 0; i < document.getElementsByTagName("A").length; i++) {
    setLinkAction(document.getElementsByTagName("A")[i]);
  }
  var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  if (MutationObserver) {
    var observer = new MutationObserver(function(records) {
      records.map(function(record) {
        setLinkAction(record.target);
        for (var i = 0; i < record.target.getElementsByTagName("A").length; i++) {
          setLinkAction(record.target.getElementsByTagName("A")[i]);
        }
      });
    });
    observer.observe(document.body, {
      childList: true,
      subtree: true
    });
  }


})();