Open all links in the new tab WITH exceptions

except for the turn page link whose inner text is 'next', 'previous' and other page numbers etc.

目前为 2020-07-18 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Open all links in the new tab WITH exceptions
  3. // @description except for the turn page link whose inner text is 'next', 'previous' and other page numbers etc.
  4. // @include http://*
  5. // @include https://*
  6. // @exclude *.wikipedia.org*
  7. // @exclude *gf.qytechs.cn*
  8. // @exclude *ezgif.com*
  9. // @exclude *armorgames.com*
  10. // @exclude */#
  11. //
  12. // @author yechenyin, me:) (kliop)
  13. // @version 1.0.0
  14. // @namespace https://gf.qytechs.cn/users/3586-yechenyin
  15. // @grant GM_openInTab
  16. // ==/UserScript==
  17. (function() {
  18. "use strict";
  19.  
  20. var exception = [/^https:\/\/www\.google\.\w+\/search/, 'https://m.leiphone.com/page/'];
  21. function getAncestorLink(element) {
  22. while (element && element.nodeName != "A") {
  23. element = element.parentNode;
  24. }
  25. if (element && element.nodeName && element.nodeName === "A" && element.href && element.href.indexOf('http') === 0)
  26. return element;
  27. }
  28.  
  29. String.prototype.matched = function(strings) {
  30. for (var i = 0; i < strings.length; i++) {
  31. if (typeof strings[i] == 'string' && this.indexOf(strings[i]) === 0)
  32. return true;
  33. else if (strings[i] instanceof RegExp && strings[i].test(this))
  34. return true;
  35. }
  36. return false;
  37. };
  38.  
  39. if (location.href.indexOf('https://tech.sina.cn') === 0) {
  40.  
  41. var setLinkAction = function(node) {
  42. if (node && !node.hasAttribute('setted') && node.nodeName && node.nodeName === "A" && node.href && node.href.indexOf('http') === 0) {
  43. node.setAttribute('setted', '');
  44. node.addEventListener('click', function(e) {
  45. e.preventDefault();
  46. e.stopPropagation();
  47. if (this && this.href && !this.href.matched(exception) && !/^(\d+|Next|Prev|>|<|下一页|上一页|下一頁|上一頁|回首頁|次へ|前へ)$/.test(this.innerText)) {
  48. console.log(this, e.target);
  49. window.open(this.href);
  50. } else {
  51. location.href = this.href;
  52. }
  53. });
  54. }
  55. };
  56.  
  57. for (var i = 0; i < document.getElementsByTagName("A").length; i++) {
  58. setLinkAction(document.getElementsByTagName("A")[i]);
  59. }
  60. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  61. if (MutationObserver) {
  62. var observer = new MutationObserver(function(records) {
  63. records.map(function(record) {
  64. setLinkAction(record.target);
  65. for (var i = 0; i < record.target.getElementsByTagName("A").length; i++) {
  66. setLinkAction(record.target.getElementsByTagName("A")[i]);
  67. }
  68. });
  69. });
  70. observer.observe(document.body, {
  71. childList: true,
  72. subtree: true
  73. });
  74. }
  75. } else {
  76. document.addEventListener('click', function(e) {
  77. var link = getAncestorLink(e.target);
  78. console.log(link.innerText);
  79. if (link && link.href && !link.href.matched(exception) && link.innerText && !/^(\d+|Next|Next >|page|Page|Sort|Filter|Prev|< Prev|Last|Random|1|5|10|[>|]|[|<]|>|<|>>|<<|下一页|上一页|下一|上一|首|次へ|前へ)$/.test(link.innerText))
  80. link.target = '_blank';
  81. });
  82. }
  83.  
  84.  
  85.  
  86. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址