External link newtaber

opens external links in a new tab on all sites (now can work with dynamic link lists, such as search results)

目前為 2020-05-25 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name External link newtaber
  3. // @namespace almaceleste
  4. // @version 0.3.0
  5. // @description opens external links in a new tab on all sites (now can work with dynamic link lists, such as search results)
  6. // @description:ru открывает внешние ссылки в новой вкладке на всех сайтах (теперь должно работать с динамическими списками ссылок, такими как результаты поисковых запросов)
  7. // @author (ɔ) Paola Captanovska
  8. // @license AGPL-3.0-or-later; 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-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. // @grant GM_openInTab
  23.  
  24. // @match http*://*/*
  25. // ==/UserScript==
  26.  
  27. // ==OpenUserJS==
  28. // @author almaceleste
  29. // ==/OpenUserJS==
  30.  
  31. const configId = 'allnewtaberCfg';
  32. const windowcss = `
  33. #${configId} {
  34. background-color: darkslategray;
  35. color: whitesmoke;
  36. }
  37. #${configId} a,
  38. #${configId} button,
  39. #${configId} input,
  40. #${configId} select,
  41. #${configId} select option,
  42. #${configId} .section_desc {
  43. color: whitesmoke !important;
  44. }
  45. #${configId} a,
  46. #${configId} button,
  47. #${configId} input,
  48. #${configId} .section_desc {
  49. font-size: .8em !important;
  50. }
  51. #${configId} button,
  52. #${configId} select,
  53. #${configId} select option,
  54. #${configId} .section_desc {
  55. background-color: #333;
  56. border: 1px solid #222;
  57. }
  58. #${configId} button{
  59. height: 1.65em !important;
  60. }
  61. #${configId}_header {
  62. font-size: 1.3em !important;
  63. }
  64. #${configId}.section_header {
  65. background-color: #454545;
  66. border: 1px solid #222;
  67. font-size: 1em !important;
  68. }
  69. #${configId} .field_label {
  70. font-size: .7em !important;
  71. }
  72. #${configId}_buttons_holder {
  73. position: fixed;
  74. width: 97%;
  75. bottom: 0;
  76. }
  77. #${configId} .reset_holder {
  78. float: left;
  79. position: relative;
  80. bottom: -1em;
  81. }
  82. #${configId} .saveclose_buttons {
  83. margin: .7em;
  84. }
  85. #${configId}_field_support {
  86. background: none !important;
  87. border: none;
  88. cursor: pointer;
  89. padding: 0 !important;
  90. text-decoration: underline;
  91. }
  92. #${configId}_field_support:hover,
  93. #${configId}_resetLink:hover {
  94. filter: drop-shadow(0 0 1px dodgerblue);
  95. }
  96. `;
  97. const iframecss = `
  98. height: 28em;
  99. width: 30em;
  100. border: 1px solid;
  101. border-radius: 3px;
  102. position: fixed;
  103. z-index: 999;
  104. `;
  105.  
  106. var host = window.location.hostname;
  107. var flat = host.replace(/\..*/, '');
  108. var root = host.replace(/^[^.]*\./, '');
  109. var child = '*.' + host;
  110. var next = '*.' + root;
  111.  
  112. GM_registerMenuCommand(`${GM_info.script.name} Settings`, () => {
  113. GM_config.open();
  114. GM_config.frame.style = iframecss;
  115. });
  116.  
  117. GM_config.init({
  118. id: `${configId}`,
  119. title: `${GM_info.script.name} ${GM_info.script.version}`,
  120. fields: {
  121. level: {
  122. section: ['', 'Exclude these domains (do not open in new tab)'],
  123. label: 'do not exclude parent and neighbor sites if parent site is a root domain like .com',
  124. labelPos: 'right',
  125. type: 'checkbox',
  126. default: true,
  127. },
  128. root: {
  129. label: 'parent site links (' + root + ')',
  130. labelPos: 'right',
  131. type: 'checkbox',
  132. default: true,
  133. },
  134. next: {
  135. label: 'neighbor site links (' + next + ')',
  136. labelPos: 'right',
  137. type: 'checkbox',
  138. default: true,
  139. },
  140. host: {
  141. label: 'this site links (' + host + ')',
  142. labelPos: 'right',
  143. type: 'checkbox',
  144. default: true,
  145. },
  146. child: {
  147. label: 'child site links (' + child + ')',
  148. labelPos: 'right',
  149. type: 'checkbox',
  150. default: true,
  151. },
  152. background: {
  153. section: ['', 'Other options'],
  154. label: 'open new tab in background',
  155. labelPos: 'right',
  156. type: 'checkbox',
  157. default: false,
  158. },
  159. insert: {
  160. label: 'insert new tab next to the current instead of the right end',
  161. labelPos: 'right',
  162. type: 'checkbox',
  163. default: true,
  164. },
  165. setParent: {
  166. label: 'return to the current tab after new tab closed',
  167. labelPos: 'right',
  168. type: 'checkbox',
  169. default: true,
  170. },
  171. support: {
  172. section: ['', 'Support'],
  173. label: 'almaceleste.github.io',
  174. title: 'more info on almaceleste.github.io',
  175. type: 'button',
  176. click: () => {
  177. GM_openInTab('https://almaceleste.github.io', {
  178. active: true,
  179. insert: true,
  180. setParent: true
  181. });
  182. }
  183. },
  184. },
  185. css: windowcss,
  186. events: {
  187. save: function() {
  188. GM_config.close();
  189. }
  190. },
  191. });
  192.  
  193. (function() {
  194. 'use strict';
  195.  
  196. const empty = new RegExp('^$');
  197. var patternroot = empty;
  198. var patternhost = empty;
  199. host = host.replace(/\./g, '\\\.');
  200. root = root.replace(/\./g, '\\\.');
  201. const background = GM_config.get('background');
  202. const insert = GM_config.get('insert');
  203. const setParent = GM_config.get('setParent');
  204. const options = {active: !background, insert: insert, setParent: setParent};
  205.  
  206. if (GM_config.get('root')){patternroot = new RegExp('^' + root + '$');} // abc.x => ^abc\.x$
  207. if (GM_config.get('next')){
  208. if (GM_config.get('root')){
  209. patternroot = new RegExp('[^(' + flat + '\.)]?' + root + '$'); // abc.x + *.abc.x => [^(w\.)]?abc\.x$
  210. }
  211. else {patternroot = new RegExp('[^(' + flat + ')]?\.' + root + '$');} // *.abc.x => [^(w)]?\.abc\.x$
  212. }
  213. if (GM_config.get('level') && root.search(/\..+\./) == -1){patternroot = empty;}
  214. if (GM_config.get('host')){patternhost = new RegExp('^' + host + '$');} // w.abc.x => ^w\.abc\.x$
  215. if (GM_config.get('child')){
  216. if (GM_config.get('host')){
  217. patternhost = new RegExp('(.+\.)?' + host + '$'); // w.abc.x + *.w.abc.x => (.+\.)?w\.abc\.x$
  218. }
  219. else {patternhost = new RegExp('.+\.' + host + '$');} // *.w.abc.x => .+\.w\.abc\.x$
  220. }
  221.  
  222. window.onload = function(){
  223. var anchors = document.getElementsByTagName('a');
  224. for (var i = 0; i < anchors.length; i++) {
  225. var a = anchors[i];
  226. var target = a.host;
  227. if (a.hasAttribute('href')){
  228. if (target && !empty.test(target)){
  229. if (!patternroot.test(target) && !patternhost.test(target)){
  230. a.addEventListener('click', newtaber);
  231. }
  232. }
  233. }
  234. }
  235. }
  236.  
  237. function newtaber(e){
  238. e.preventDefault();
  239. e.stopPropagation();
  240. GM_openInTab(this.href, options);
  241. }
  242. })();