External link newtaber

this code opens external links in new tab on all sites (at the moment does not support dynamic lists of links such as search results)

目前为 2018-04-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name External link newtaber
  3. // @namespace almaceleste
  4. // @version 0.1.2
  5. // @description this code opens external links in new tab on all sites (at the moment does not support dynamic lists of links such as search results)
  6. // @description:ru этот код открывает внешние ссылки в новой вкладке на всех сайтах (в данный момент не поддерживает динамические списки ссылок, такие как результаты поимковых запросов)
  7. // @author (ɔ) Paola Captanovska
  8. // @license AGPL-3.0; http://www.gnu.org/licenses/agpl.txt
  9. // @icon https://cdn1.iconfinder.com/data/icons/feather-2/24/external-link-32.png
  10. // @icon https://cdn1.iconfinder.com/data/icons/feather-2/24/external-link-128.png
  11.  
  12. // @homepageURL https://greasyfork.org/en/users/174037-almaceleste
  13. // @homepageURL https://openuserjs.org/users/almaceleste
  14. // @homepageURL https://github.com/almaceleste/userscripts
  15. // @supportURL https://github.com/almaceleste/userscripts/issues
  16.  
  17. // @run-at document.end
  18. // @require https://openuserjs.org/src/libs/sizzle/GM_config.js
  19. // @grant GM_getValue
  20. // @grant GM_setValue
  21. // @grant GM_registerMenuCommand
  22.  
  23. // @match http*://*/*
  24. // ==/UserScript==
  25.  
  26. // ==OpenUserJS==
  27. // @author almaceleste
  28. // ==/OpenUserJS==
  29.  
  30. const windowcss = '#allnewtaberCfg {background-color: lightblue;} #allnewtaberCfg .reset_holder {float: left; position: relative; bottom: -1em;} #allnewtaberCfg .saveclose_buttons {margin: .7em;}';
  31. const iframecss = 'height: 17.3em; width: 30em; border: 1px solid; border-radius: 3px; position: fixed; z-index: 999;';
  32.  
  33. var host = window.location.hostname;
  34. var flat = host.replace(/\..*/, '');
  35. var root = host.replace(/^[^.]*\./, '');
  36. var child = '*.' + host;
  37. var next = '*.' + root;
  38.  
  39. GM_registerMenuCommand('External link newtaber Settings', opencfg);
  40.  
  41. function opencfg()
  42. {
  43. GM_config.open();
  44. allnewtaberCfg.style = iframecss;
  45. }
  46.  
  47. GM_config.init(
  48. {
  49. id: 'allnewtaberCfg',
  50. title: 'External link newtaber',
  51. fields:
  52. {
  53. level:
  54. {
  55. section: ['Exclusions', 'Exclude these domains (do not open in new tab)'],
  56. label: 'do not exclude parent and neighbor sites if parent site is a root domain like .com',
  57. labelPos: 'right',
  58. type: 'checkbox',
  59. default: true,
  60. },
  61. root:
  62. {
  63. label: 'parent site links (' + root + ')',
  64. labelPos: 'right',
  65. type: 'checkbox',
  66. default: true,
  67. },
  68. next:
  69. {
  70. label: 'neighbor site links (' + next + ')',
  71. labelPos: 'right',
  72. type: 'checkbox',
  73. default: true,
  74. },
  75. host:
  76. {
  77. label: 'this site links (' + host + ')',
  78. labelPos: 'right',
  79. type: 'checkbox',
  80. default: true,
  81. },
  82. child:
  83. {
  84. label: 'child site links (' + child + ')',
  85. labelPos: 'right',
  86. type: 'checkbox',
  87. default: true,
  88. },
  89. },
  90. css: windowcss,
  91. events:
  92. {
  93. save: function() {
  94. GM_config.close();
  95. }
  96. },
  97. });
  98.  
  99. (function() {
  100. 'use strict';
  101.  
  102. var empty = new RegExp('^$');
  103. var patternroot = empty;
  104. var patternhost = empty;
  105. host = host.replace(/\./g, '\\\.');
  106. root = root.replace(/\./g, '\\\.');
  107.  
  108. if (GM_config.get('root')){patternroot = new RegExp('^' + root + '$');} // abc.x => ^abc\.x$
  109. if (GM_config.get('next')){
  110. if (GM_config.get('root')){
  111. patternroot = new RegExp('[^(' + flat + '\.)]?' + root + '$'); // abc.x + *.abc.x => [^(w\.)]?abc\.x$
  112. }
  113. else {patternroot = new RegExp('[^(' + flat + ')]?\.' + root + '$');} // *.abc.x => [^(w)]?\.abc\.x$
  114. }
  115. if (GM_config.get('level') && root.search(/\..+\./) == -1){patternroot = empty;}
  116. if (GM_config.get('host')){patternhost = new RegExp('^' + host + '$');} // w.abc.x => ^w\.abc\.x$
  117. if (GM_config.get('child')){
  118. if (GM_config.get('host')){
  119. patternhost = new RegExp('(.+\.)?' + host + '$'); // w.abc.x + *.w.abc.x => (.+\.)?w\.abc\.x$
  120. }
  121. else {patternhost = new RegExp('.+\.' + host + '$');} // *.w.abc.x => .+\.w\.abc\.x$
  122. }
  123.  
  124. window.onload = function(){
  125. var anchors= document.getElementsByTagName('a');
  126.  
  127. for (var i = 0; i < anchors.length; i++) {
  128. var target = anchors[i].host;
  129. if (target && target !== ''){
  130. if (!patternroot.test(target) && !patternhost.test(target)){
  131. anchors[i].target = '_blank';
  132. }
  133. }
  134. }
  135. };
  136. })();