Open Links in NEW BACKGROUND Tab

Open links (esp. from different domains) in NEW BACKGROUND tab with normal left click

当前为 2023-04-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Open Links in NEW BACKGROUND Tab
  3. // @author Jerry
  4. // @description Open links (esp. from different domains) in NEW BACKGROUND tab with normal left click
  5. // @grant GM_openInTab
  6. // @include http*://*
  7. // @namespace https://greasyfork.org/users/28298
  8. // @run-at document-start
  9. // @version 0.9
  10. // @license GNU GPLv3
  11. // ==/UserScript==
  12.  
  13.  
  14. // source:
  15. // https://greasyfork.org/en/scripts/20694-all-links-open-all-in-new-background-tab/code
  16. // https://greasyfork.org/en/scripts/12367-open-links-in-new-tab/code
  17.  
  18. attachHandler([].slice.call(document.getElementsByTagName('a')));
  19.  
  20. setMutationHandler(document, 'a', function(nodes) {
  21. attachHandler(nodes);
  22. return true;
  23. });
  24.  
  25.  
  26. // Not use anymore, has been fixed by cond2 below
  27. // https://stackoverflow.com/a/26269087/2292993
  28. // ugly fix duckduckgo rightside bar search results
  29. // // document.addEventListener ("DOMContentLoaded", FnReady);
  30. // window.addEventListener ("load", FnReady);
  31. // function FnReady () {
  32. // if (location.href.startsWith('https://duckduckgo.com')) {
  33. // var nodes = document.getElementsByTagName('a');
  34. // for (var i=0; i<nodes.length; i++) {
  35. // var node = nodes[i];
  36. // if (node.className.includes("js-about-item")) {
  37. // node.replaceWith(node.cloneNode(true));
  38. // }
  39. // }
  40. // }
  41. // }
  42.  
  43. function attachHandler(nodes) {
  44. nodes.forEach(function(node) {
  45. // href will be auto expanded with window.location.origin, such as href="#" --> "https://example.com/#"
  46. var cond = (node.target != '_blank') && (node.href) && (!node.href.includes("javascript:")) && (!node.href.startsWith(window.location.origin));
  47. cond = cond && (node.className!="search-engine-a"); // search-engine-a: more compatible with Search Engine Switcher script
  48. cond = cond && (!node.className.includes("s-topbar--item")); // stackoverflow
  49. cond = cond && (!location.href.startsWith("https://app.raindrop.io"));
  50. cond = cond && (!location.href.startsWith("https://portal.discover.com"));
  51. cond = cond && (!node.href.startsWith("https://accounts.google.com")); // gdrive
  52. if ( cond ) {
  53. node.onclick = clickHandler;
  54. node.addEventListener('click', clickHandler);
  55.  
  56. }
  57. // to avoid circular loop (that I do not fully understand how it would happen)
  58. cond2 = cond && (!node.hasAttribute('processed'));
  59. // ddg right sidebar search result or image search result
  60. cond2 = cond2 && ( node.className.includes("js-about-item") || (cond && location.href.startsWith("https://duckduckgo.com") && location.href.includes("&ia=images")) );
  61. if ( cond2 ) {
  62. node.setAttribute("processed", "true");
  63. node.replaceWith(node.cloneNode(true));
  64. }
  65. });
  66. }
  67.  
  68. /*
  69. https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
  70. MouseEvent.buttons
  71. 0: No button or un-initialized
  72. 1: Primary button (usually the left button)
  73. 2: Secondary button (usually the right button)
  74. 4: Auxiliary button (usually the mouse wheel button or middle button)
  75. 8: 4th button (typically the "Browser Back" button)
  76. 16 : 5th button (typically the "Browser Forward" button)
  77.  
  78. https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
  79. KeyboardEvent.metaKey
  80. e.metaKey
  81.  
  82. https://stackoverflow.com/a/35936912/2292993
  83. e is short for event
  84. the element on which you clicked (event.target)
  85.  
  86. https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation
  87. The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases. It does not, however, prevent any default behaviors from occurring; for instance, clicks on links are still processed. If you want to stop those behaviors, see the preventDefault() method. It also does not prevent immediate propagation to other event-handlers. If you want to stop those, see stopImmediatePropagation().
  88. */
  89. function clickHandler(e) {
  90. if (e.button > 1)
  91. return;
  92. e.preventDefault();
  93. e.stopPropagation();
  94. e.stopImmediatePropagation();
  95. // GM_openInTab(this.href, e.button || e.ctrlKey);
  96. GM_openInTab(this.href, true); // open in background
  97. }
  98.  
  99.  
  100.  
  101.  
  102. // https://greasyfork.org/scripts/12228/code/setMutationHandler.js
  103. // ==UserScript==
  104. // @name setMutationHandler
  105. // @description MutationObserver wrapper to wait for the specified CSS selector
  106. // @namespace wOxxOm.scripts
  107. // @author wOxxOm
  108. // @grant none
  109. // @version 3.0.2
  110. // ==/UserScript==
  111.  
  112. function setMutationHandler(target, selector, handler, options) {
  113. // or setMutationHandler(selector, handler, options) {
  114. // or setMutationHandler(options) {
  115. if (typeof arguments[0] == 'string') {
  116. options = arguments[2] || {};
  117. handler = arguments[1];
  118. selector = arguments[0];
  119. target = document;
  120. } else if (arguments.length == 1 && target && typeof target.handler == 'function') {
  121. options = arguments[0];
  122. handler = options.handler;
  123. selector = options.selector;
  124. target = options.target || document;
  125. } else if (!(target instanceof Node)) {
  126. throw 'Bad params for setMutationHandler.\n' +
  127. 'A: [optional Node] target, [String] selector, [Function] handler, [optional Object] options\n' +
  128. 'B: [Object] options\n' +
  129. 'Options: target, selector, handler, processExisting, childList, attributes, characterData, subtree, attributeOldValue, characterDataOldValue, attributeFilter';
  130. } else
  131. options = options || {};
  132.  
  133. if (options.processExisting && target.querySelector(selector))
  134. handler.call(null, Array.prototype.slice.call(target.querySelectorAll(selector)));
  135. if (!options.attributes && !options.characterData && !options.childList && options.subtree === undefined)
  136. options.childList = options.subtree = true;
  137.  
  138. var cb;
  139. if (/^#[\w\d-]+$/.test(selector)) {
  140. selector = selector.substr(1);
  141. cb = MOhandlerForId;
  142. } else {
  143. cb = MOhandler;
  144. }
  145. var observer = new MutationObserver(cb);
  146. observer.observe(target, options || {subtree:true, childList:true});
  147. return observer;
  148.  
  149. function MOhandler(mutations) {
  150. if (mutations.length > 100 && !document.querySelector(selector))
  151. return;
  152. var found = [];
  153. for (var i=0, m; (m = mutations[i++]); ) {
  154. switch (m.type) {
  155. case 'childList':
  156. var nodes = m.addedNodes, nl = nodes.length;
  157. var textNodesOnly = true;
  158. for (var j=0; j < nl; j++) {
  159. var n = nodes[j];
  160. textNodesOnly &= n.nodeType == 3; // TEXT_NODE
  161. if (n.nodeType != 1) // ELEMENT_NODE
  162. continue;
  163. if (n.matches(selector))
  164. found.push(n);
  165. else if (n.querySelector(selector)) {
  166. n = n.querySelectorAll(selector);
  167. if (n.length < 1000)
  168. found.push.apply(found, n);
  169. else
  170. found = found.concat(found.slice.call(n));
  171. }
  172. }
  173. if (textNodesOnly && m.target.matches(selector))
  174. found.push(m.target);
  175. break;
  176. case 'attributes':
  177. if (m.target.matches(selector))
  178. found.push(m.target);
  179. break;
  180. case 'characterData':
  181. if (m.target.parentNode && m.target.parentNode.matches(selector))
  182. found.push(m.target.parentNode);
  183. break;
  184. }
  185. }
  186. if (!found.length)
  187. return;
  188. if (handler.call(observer, found) === false)
  189. observer.disconnect();
  190. }
  191.  
  192. function MOhandlerForId(mutations) {
  193. var el = document.getElementById(selector);
  194. if (el && target.contains(el))
  195. if (handler.call(observer, [el]) === false)
  196. observer.disconnect();
  197. }
  198. }
  199.