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)

目前为 2017-11-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name open all links in the new tab
  3. // @description except for the link in the same directory(the link whose web address are same before the last '/' as the current url)
  4. // @include http://*
  5. // @include https://*
  6. // @author yechenyin
  7. // @version 0.6
  8. // @namespace https://greasyfork.org/users/3586-yechenyin
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11. var exception = ['https://m.leiphone.com/page/'];
  12. function getAncestorLink(element) {
  13. while (element && element.nodeName != "A") {
  14. element = element.parentNode;
  15. }
  16. if (element.nodeName === "A" && element.href && element.href.indexOf('http') === 0)
  17. return element;
  18. }
  19.  
  20. String.prototype.matched = function(strings) {
  21. for (var i = 0; i < strings.length; i++) {
  22. if (typeof strings[i] == 'string' && this.indexOf(strings[i]) === 0)
  23. return true;
  24. else if (strings[i] instanceof RegExp && this.test(strings[i]))
  25. return true;
  26. }
  27. return false;
  28. };
  29.  
  30. document.addEventListener('click', function(e) {
  31. var link = getAncestorLink(e.target);
  32. console.log(link.innerText, link.href);
  33. if (link && link.href && !/^(\d+|Next|Prev|>|<|下一页|上一页|下一頁|上一頁|回首頁|次へ|前へ)$/.test(link.innerText) && !link.href.matched(exception)) {
  34. window.open(link.href);
  35. e.preventDefault();
  36. e.stopPropagation();
  37. return false;
  38. }
  39. });
  40. document.body.innerHTML += '<script>' + `
  41. window.addEventListener("beforeunload", function (e) {
  42. //debugger;
  43. return 'left?';
  44. });
  45. ` + '</script>';