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-23 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name External link newtaber
  3. // @namespace almaceleste
  4. // @version 0.2.1
  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. // @icon64 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-start
  18. // @require https://openuserjs.org/src/libs/sizzle/GM_config.js
  19. // @grant GM_getValue
  20. // @grant GM_setValue
  21. // @grant GM_registerMenuCommand
  22. // @grant GM_openInTab
  23.  
  24. // @match http*://*/*
  25. // ==/UserScript==
  26.  
  27. // ==OpenUserJS==
  28. // @author almaceleste
  29. // ==/OpenUserJS==
  30.  
  31. const windowcss = '#allnewtaberCfg {background-color: lightblue;} #allnewtaberCfg .reset_holder {float: left; position: relative; bottom: -1em;} #allnewtaberCfg .saveclose_buttons {margin: .7em;}';
  32. const iframecss = 'height: 30.1em; width: 30em; border: 1px solid; border-radius: 3px; position: fixed; z-index: 999;';
  33.  
  34. var host = window.location.hostname;
  35. var flat = host.replace(/\..*/, '');
  36. var root = host.replace(/^[^.]*\./, '');
  37. var child = '*.' + host;
  38. var next = '*.' + root;
  39.  
  40. GM_registerMenuCommand('External link newtaber Settings', opencfg);
  41.  
  42. function opencfg()
  43. {
  44. GM_config.open();
  45. allnewtaberCfg.style = iframecss;
  46. }
  47.  
  48. GM_config.init(
  49. {
  50. id: 'allnewtaberCfg',
  51. title: 'External link newtaber',
  52. fields:
  53. {
  54. level:
  55. {
  56. section: ['Exclusions', 'Exclude these domains (do not open in new tab)'],
  57. label: 'do not exclude parent and neighbor sites if parent site is a root domain like .com',
  58. labelPos: 'right',
  59. type: 'checkbox',
  60. default: true,
  61. },
  62. root:
  63. {
  64. label: 'parent site links (' + root + ')',
  65. labelPos: 'right',
  66. type: 'checkbox',
  67. default: true,
  68. },
  69. next:
  70. {
  71. label: 'neighbor site links (' + next + ')',
  72. labelPos: 'right',
  73. type: 'checkbox',
  74. default: true,
  75. },
  76. host:
  77. {
  78. label: 'this site links (' + host + ')',
  79. labelPos: 'right',
  80. type: 'checkbox',
  81. default: true,
  82. },
  83. child:
  84. {
  85. label: 'child site links (' + child + ')',
  86. labelPos: 'right',
  87. type: 'checkbox',
  88. default: true,
  89. },
  90. background:
  91. {
  92. section: ['Options', 'Other options'],
  93. label: 'open new tab in background',
  94. labelPos: 'right',
  95. type: 'checkbox',
  96. default: false,
  97. },
  98. insert:
  99. {
  100. label: 'insert new tab next to the current instead of the right end',
  101. labelPos: 'right',
  102. type: 'checkbox',
  103. default: true,
  104. },
  105. setParent:
  106. {
  107. label: 'return to the current tab after new tab closed',
  108. labelPos: 'right',
  109. type: 'checkbox',
  110. default: true,
  111. },
  112. },
  113. css: windowcss,
  114. events:
  115. {
  116. save: function() {
  117. GM_config.close();
  118. }
  119. },
  120. });
  121.  
  122. (function() {
  123. 'use strict';
  124.  
  125. var empty = new RegExp('^$');
  126. var patternroot = empty;
  127. var patternhost = empty;
  128. host = host.replace(/\./g, '\\\.');
  129. root = root.replace(/\./g, '\\\.');
  130. var background = GM_config.get('background');
  131. var options = {active: !GM_config.get('background'), insert: GM_config.get('insert'), setParent: GM_config.get('setParent')};
  132.  
  133. if (GM_config.get('root')){patternroot = new RegExp('^' + root + '$');} // abc.x => ^abc\.x$
  134. if (GM_config.get('next')){
  135. if (GM_config.get('root')){
  136. patternroot = new RegExp('[^(' + flat + '\.)]?' + root + '$'); // abc.x + *.abc.x => [^(w\.)]?abc\.x$
  137. }
  138. else {patternroot = new RegExp('[^(' + flat + ')]?\.' + root + '$');} // *.abc.x => [^(w)]?\.abc\.x$
  139. }
  140. if (GM_config.get('level') && root.search(/\..+\./) == -1){patternroot = empty;}
  141. if (GM_config.get('host')){patternhost = new RegExp('^' + host + '$');} // w.abc.x => ^w\.abc\.x$
  142. if (GM_config.get('child')){
  143. if (GM_config.get('host')){
  144. patternhost = new RegExp('(.+\.)?' + host + '$'); // w.abc.x + *.w.abc.x => (.+\.)?w\.abc\.x$
  145. }
  146. else {patternhost = new RegExp('.+\.' + host + '$');} // *.w.abc.x => .+\.w\.abc\.x$
  147. }
  148.  
  149. window.onload = function(){
  150. var anchors= document.getElementsByTagName('a');
  151.  
  152. for (var i = 0; i < anchors.length; i++) {
  153. var a = anchors[i];
  154. // console.log('href', a.href, a);
  155. var target = a.host;
  156. if (a.hasAttribute('href')){
  157. if (target && !empty.test(target)){
  158. if (!patternroot.test(target) && !patternhost.test(target)){
  159. // a.target = '_blank';
  160. a.addEventListener('click', newtaber);
  161. }
  162. }
  163. }
  164. }
  165. };
  166.  
  167. function newtaber(e){
  168. e.preventDefault();
  169. e.stopPropagation();
  170. GM_openInTab(this.href, options);
  171. }
  172. })();