Greasy Fork is available in English.

External link newtaber

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

სკრიპტის ინსტალაცია?
ავტორის შემოთავაზებული სკრიპტი

შეიძლება მოგეწონოს StackExchange link newtaber.

სკრიპტის ინსტალაცია
  1. // ==UserScript==
  2. // @name External link newtaber
  3. // @namespace almaceleste
  4. // @version 0.3.4
  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 (ɔ) almaceleste (https://almaceleste.github.io)
  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://greatest.deepsurf.us/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. // @require https://openuserjs.org/src/libs/sizzle/GM_config.js
  18. // @grant GM_getValue
  19. // @grant GM_setValue
  20. // @grant GM_registerMenuCommand
  21. // @grant GM_openInTab
  22. // @grant GM_getResourceText
  23.  
  24. // @resource css https://github.com/almaceleste/userscripts/raw/master/css/default.css
  25.  
  26. // @match http*://*/*
  27. // ==/UserScript==
  28.  
  29. // ==OpenUserJS==
  30. // @author almaceleste
  31. // ==/OpenUserJS==
  32.  
  33. // script global variables
  34. let host = window.location.hostname;
  35. const root = '*.' + host.split('.').pop();
  36. const flat = host.replace(/\..*/, '');
  37. let parent = host.replace(/^[^.]*\./, '');
  38. const child = '*.' + host;
  39. const neighbor = '*.' + parent;
  40.  
  41. // config settings
  42. const configId = 'allnewtaberCfg';
  43. const iconUrl = GM_info.script.icon64;
  44. const pattern = {};
  45. pattern[`#${configId}`] = /#configId/g;
  46. pattern[`${iconUrl}`] = /iconUrl/g;
  47.  
  48. let css = GM_getResourceText('css');
  49. Object.keys(pattern).forEach((key) => {
  50. css = css.replace(pattern[key], key);
  51. });
  52. const windowcss = css;
  53. const iframecss = `
  54. height: 375px;
  55. width: 435px;
  56. border: 1px solid;
  57. border-radius: 3px;
  58. position: fixed;
  59. z-index: 9999;
  60. `;
  61.  
  62. GM_registerMenuCommand(`${GM_info.script.name} Settings`, () => {
  63. GM_config.open();
  64. GM_config.frame.style = iframecss;
  65. });
  66.  
  67. GM_config.init({
  68. id: `${configId}`,
  69. title: `${GM_info.script.name} ${GM_info.script.version}`,
  70. fields: {
  71. rootzone: {
  72. section: ['', 'Exclude these domains (do not open in new tab)'],
  73. label: `parent and neighbor sites if parent site is a root domain (${root})`,
  74. labelPos: 'right',
  75. type: 'checkbox',
  76. default: false,
  77. },
  78. parent: {
  79. label: `parent site links (${parent})`,
  80. labelPos: 'right',
  81. type: 'checkbox',
  82. default: true,
  83. },
  84. neighbor: {
  85. label: `neighbor site links (${neighbor})`,
  86. labelPos: 'right',
  87. type: 'checkbox',
  88. default: true,
  89. },
  90. host: {
  91. label: `this site links (${host})`,
  92. labelPos: 'right',
  93. type: 'checkbox',
  94. default: true,
  95. },
  96. child: {
  97. label: `child site links (${child})`,
  98. labelPos: 'right',
  99. type: 'checkbox',
  100. default: true,
  101. },
  102. background: {
  103. section: ['', 'Other options'],
  104. label: 'open new tab in background',
  105. labelPos: 'right',
  106. type: 'checkbox',
  107. default: false,
  108. },
  109. insert: {
  110. label: 'insert new tab next to the current instead of the right end',
  111. labelPos: 'right',
  112. type: 'checkbox',
  113. default: true,
  114. },
  115. setParent: {
  116. label: 'return to the current tab after new tab closed',
  117. labelPos: 'right',
  118. type: 'checkbox',
  119. default: true,
  120. },
  121. support: {
  122. section: ['', 'Support'],
  123. label: 'almaceleste.github.io',
  124. title: 'more info on almaceleste.github.io',
  125. type: 'button',
  126. click: () => {
  127. GM_openInTab('https://almaceleste.github.io', {
  128. active: true,
  129. insert: true,
  130. setParent: true
  131. });
  132. }
  133. },
  134. },
  135. css: windowcss,
  136. events: {
  137. save: function() {
  138. GM_config.close();
  139. }
  140. },
  141. });
  142.  
  143. // script code
  144. (function() {
  145. 'use strict';
  146.  
  147. const empty = new RegExp('^$');
  148. let patternparent = empty;
  149. let patternhost = empty;
  150. host = host.replace(/\./g, '\\\.');
  151. parent = parent.replace(/\./g, '\\\.');
  152. const background = GM_config.get('background');
  153. const insert = GM_config.get('insert');
  154. const setParent = GM_config.get('setParent');
  155. const options = {active: !background, insert: insert, setParent: setParent};
  156.  
  157. if (GM_config.get('parent')){patternparent = new RegExp(`^${parent}$`);} // abc.x => ^abc\.x$
  158. if (GM_config.get('neighbor')){
  159. if (GM_config.get('parent')){
  160. patternparent = new RegExp(`[^(${flat}\.)]?${parent}$`); // abc.x + *.abc.x => [^(w\.)]?abc\.x$
  161. }
  162. else {patternparent = new RegExp(`[^(${flat})]?\.${parent}$`);} // *.abc.x => [^(w)]?\.abc\.x$
  163. }
  164. if (!GM_config.get('rootzone') && parent.search(/\..+\./) == -1){patternparent = empty;}
  165. if (GM_config.get('host')){patternhost = new RegExp(`^${host}$`);} // w.abc.x => ^w\.abc\.x$
  166. if (GM_config.get('child')){
  167. if (GM_config.get('host')){
  168. patternhost = new RegExp(`(.+\.)?${host}$`); // w.abc.x + *.w.abc.x => (.+\.)?w\.abc\.x$
  169. }
  170. else {patternhost = new RegExp(`.+\.${host}$`);} // *.w.abc.x => .+\.w\.abc\.x$
  171. }
  172.  
  173. window.onload = function(){
  174. const anchors = document.getElementsByTagName('a');
  175. for (let i = 0; i < anchors.length; i++) {
  176. const a = anchors[i];
  177. const target = a.host;
  178. if (a.hasAttribute('href')){
  179. if (target && !empty.test(target)){
  180. if (!patternparent.test(target) && !patternhost.test(target)){
  181. a.addEventListener('click', newtaber);
  182. }
  183. }
  184. }
  185. }
  186. }
  187.  
  188. function newtaber(e){
  189. e.preventDefault();
  190. e.stopPropagation();
  191. GM_openInTab(this.href, options);
  192. }
  193. })();