Open Links in NEW BACKGROUND Tab

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

目前为 2025-01-14 提交的版本,查看 最新版本

  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 1.0
  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. cond = cond && (!node.href.contains("walmart.com/account")); // walmart login
  53. if ( cond ) {
  54. node.onclick = clickHandler;
  55. node.addEventListener('click', clickHandler);
  56.  
  57. }
  58. // to avoid circular loop (that I do not fully understand how it would happen)
  59. cond2 = cond && (!node.hasAttribute('processed'));
  60. // ddg right sidebar search result or image search result
  61. cond2 = cond2 && ( node.className.includes("js-about-item") || (cond && location.href.startsWith("https://duckduckgo.com") && location.href.includes("&ia=images")) );
  62. if ( cond2 ) {
  63. node.setAttribute("processed", "true");
  64. node.replaceWith(node.cloneNode(true));
  65. }
  66. });
  67. }
  68.  
  69. /*
  70. https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
  71. MouseEvent.buttons
  72. 0: No button or un-initialized
  73. 1: Primary button (usually the left button)
  74. 2: Secondary button (usually the right button)
  75. 4: Auxiliary button (usually the mouse wheel button or middle button)
  76. 8: 4th button (typically the "Browser Back" button)
  77. 16 : 5th button (typically the "Browser Forward" button)
  78.  
  79. https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
  80. KeyboardEvent.metaKey
  81. e.metaKey
  82.  
  83. https://stackoverflow.com/a/35936912/2292993
  84. e is short for event
  85. the element on which you clicked (event.target)
  86.  
  87. https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation
  88. 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().
  89. */
  90. function clickHandler(e) {
  91. if (e.button > 1)
  92. return;
  93. e.preventDefault();
  94. e.stopPropagation();
  95. e.stopImmediatePropagation();
  96. // GM_openInTab(this.href, e.button || e.ctrlKey);
  97. GM_openInTab(this.href, true); // open in background
  98. }
  99.  
  100.  
  101.  
  102.  
  103. // https://greasyfork.org/scripts/12228/code/setMutationHandler.js
  104. // ==UserScript==
  105. // @name setMutationHandler
  106. // @description MutationObserver wrapper to wait for the specified CSS selector
  107. // @namespace wOxxOm.scripts
  108. // @author wOxxOm
  109. // @grant none
  110. // @version 3.0.2
  111. // ==/UserScript==
  112.  
  113. function setMutationHandler(target, selector, handler, options) {
  114. // or setMutationHandler(selector, handler, options) {
  115. // or setMutationHandler(options) {
  116. if (typeof arguments[0] == 'string') {
  117. options = arguments[2] || {};
  118. handler = arguments[1];
  119. selector = arguments[0];
  120. target = document;
  121. } else if (arguments.length == 1 && target && typeof target.handler == 'function') {
  122. options = arguments[0];
  123. handler = options.handler;
  124. selector = options.selector;
  125. target = options.target || document;
  126. } else if (!(target instanceof Node)) {
  127. throw 'Bad params for setMutationHandler.\n' +
  128. 'A: [optional Node] target, [String] selector, [Function] handler, [optional Object] options\n' +
  129. 'B: [Object] options\n' +
  130. 'Options: target, selector, handler, processExisting, childList, attributes, characterData, subtree, attributeOldValue, characterDataOldValue, attributeFilter';
  131. } else
  132. options = options || {};
  133.  
  134. if (options.processExisting && target.querySelector(selector))
  135. handler.call(null, Array.prototype.slice.call(target.querySelectorAll(selector)));
  136. if (!options.attributes && !options.characterData && !options.childList && options.subtree === undefined)
  137. options.childList = options.subtree = true;
  138.  
  139. var cb;
  140. if (/^#[\w\d-]+$/.test(selector)) {
  141. selector = selector.substr(1);
  142. cb = MOhandlerForId;
  143. } else {
  144. cb = MOhandler;
  145. }
  146. var observer = new MutationObserver(cb);
  147. observer.observe(target, options || {subtree:true, childList:true});
  148. return observer;
  149.  
  150. function MOhandler(mutations) {
  151. if (mutations.length > 100 && !document.querySelector(selector))
  152. return;
  153. var found = [];
  154. for (var i=0, m; (m = mutations[i++]); ) {
  155. switch (m.type) {
  156. case 'childList':
  157. var nodes = m.addedNodes, nl = nodes.length;
  158. var textNodesOnly = true;
  159. for (var j=0; j < nl; j++) {
  160. var n = nodes[j];
  161. textNodesOnly &= n.nodeType == 3; // TEXT_NODE
  162. if (n.nodeType != 1) // ELEMENT_NODE
  163. continue;
  164. if (n.matches(selector))
  165. found.push(n);
  166. else if (n.querySelector(selector)) {
  167. n = n.querySelectorAll(selector);
  168. if (n.length < 1000)
  169. found.push.apply(found, n);
  170. else
  171. found = found.concat(found.slice.call(n));
  172. }
  173. }
  174. if (textNodesOnly && m.target.matches(selector))
  175. found.push(m.target);
  176. break;
  177. case 'attributes':
  178. if (m.target.matches(selector))
  179. found.push(m.target);
  180. break;
  181. case 'characterData':
  182. if (m.target.parentNode && m.target.parentNode.matches(selector))
  183. found.push(m.target.parentNode);
  184. break;
  185. }
  186. }
  187. if (!found.length)
  188. return;
  189. if (handler.call(observer, found) === false)
  190. observer.disconnect();
  191. }
  192.  
  193. function MOhandlerForId(mutations) {
  194. var el = document.getElementById(selector);
  195. if (el && target.contains(el))
  196. if (handler.call(observer, [el]) === false)
  197. observer.disconnect();
  198. }
  199. }
  200.