Google - Middle Click Search

Opens search results in new tab when you middle click

当前为 2019-12-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Google - Middle Click Search
  3. // @namespace https://gf.qytechs.cn/users/649
  4. // @version 1.1.7
  5. // @description Opens search results in new tab when you middle click
  6. // @author Adrien Pyke
  7. // @include /^https?:\/\/www\.google\.[a-zA-Z]+\/?(?:\?.*)?$/
  8. // @include /^https?:\/\/www\.google\.[a-zA-Z]+\/search\/?\?.*$/
  9. // @require https://gitcdn.link/repo/fuzetsu/userscripts/b38eabf72c20fa3cf7da84ecd2cefe0d4a2116be/wait-for-elements/wait-for-elements.js
  10. // @grant GM_openInTab
  11. // ==/UserScript==
  12.  
  13. (() => {
  14. 'use strict';
  15.  
  16. const setQueryParam = function(key, value, url = location.href) {
  17. const regex = new RegExp(`([?&])${key}=.*?(&|#|$)(.*)`, 'giu');
  18. const hasValue = typeof value !== 'undefined' && value !== null && value !== '';
  19. if (regex.test(url)) {
  20. if (hasValue) {
  21. return url.replace(regex, `$1${key}=${value}$2$3`);
  22. } else {
  23. const [path, hash] = url.split('#');
  24. url = path.replace(regex, '$1$3').replace(/(&|\?)$/u, '');
  25. if (hash) url += `#${hash[1]}`;
  26. return url;
  27. }
  28. } else if (hasValue) {
  29. const separator = url.includes('?') ? '&' : '?';
  30. const [path, hash] = url.split('#');
  31. url = `${path + separator + key}=${value}`;
  32. if (hash) url += `#${hash[1]}`;
  33. return url;
  34. } else return url;
  35. };
  36.  
  37. const getUrl = function(value) {
  38. if (window.location.href.match(/^https?:\/\/www\.google\.[a-zA-Z]+\/search\/?\?.*$/u)) {
  39. return setQueryParam('q', encodeURIComponent(value));
  40. } else {
  41. return `${location.protocol}//${location.host}/search?q=${encodeURIComponent(value)}`;
  42. }
  43. };
  44.  
  45. waitForElems({
  46. sel: '#_fZl',
  47. onmatch(btn) {
  48. const input = document.querySelector('#lst-ib');
  49.  
  50. btn.onmousedown = e => {
  51. if (e.button === 1) {
  52. e.preventDefault();
  53. }
  54. };
  55.  
  56. btn.onclick = e => {
  57. if (e.button === 1 && input.value.trim()) {
  58. e.preventDefault();
  59. e.stopImmediatePropagation();
  60. const url = getUrl(input.value);
  61. GM_openInTab(url, true);
  62. return false;
  63. }
  64. };
  65.  
  66. btn.onauxclick = btn.onclick;
  67. }
  68. });
  69.  
  70. waitForElems({
  71. sel: '.sbsb_b li .sbqs_c, .sbsb_b li .sbpqs_d',
  72. onmatch(elem) {
  73. elem.onclick = e => {
  74. if (e.button === 1) {
  75. e.preventDefault();
  76. e.stopImmediatePropagation();
  77. const text = elem.classList.contains('sbpqs_d')
  78. ? elem.querySelector('span').textContent
  79. : elem.textContent;
  80. const url = getUrl(text);
  81. GM_openInTab(url, true);
  82. return false;
  83. }
  84. };
  85. elem.onauxclick = elem.onclick;
  86. }
  87. });
  88. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址