Google - Middle Click Search

Opens search results in new tab when you middle click

目前为 2018-03-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Google - Middle Click Search
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.1
  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://cdn.rawgit.com/fuzetsu/userscripts/477063e939b9658b64d2f91878da20a7f831d98b/wait-for-elements/wait-for-elements.js
  10. // @grant GM_openInTab
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const setQueryParameter = function(key, value, url) {
  17. if (!url) url = window.location.href;
  18. let re = new RegExp('([?&])' + key + '=.*?(&|#|$)(.*)', 'gi'),
  19. hash;
  20.  
  21. if (re.test(url)) {
  22. if (typeof value !== 'undefined' && value !== null) return url.replace(re, '$1' + key + '=' + value + '$2$3');
  23. else {
  24. hash = url.split('#');
  25. url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
  26. if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
  27. return url;
  28. }
  29. } else if (typeof value !== 'undefined' && value !== null) {
  30. let separator = url.indexOf('?') !== -1 ? '&' : '?';
  31. hash = url.split('#');
  32. url = hash[0] + separator + key + '=' + value;
  33. if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
  34. return url;
  35. } else return url;
  36. };
  37.  
  38. const getUrl = function(value) {
  39. if (window.location.href.match(/^https?:\/\/www\.google\.[a-zA-Z]+\/search\/?\?.*$/)) {
  40. return setQueryParameter('q', encodeURIComponent(value));
  41. } else {
  42. return location.protocol + '//' + location.host + '/search?q=' + encodeURIComponent(value);
  43. }
  44. };
  45.  
  46. waitForElems({
  47. sel: '#_fZl',
  48. onmatch(btn) {
  49. let input = document.querySelector('#lst-ib');
  50.  
  51. btn.onmousedown = e => {
  52. if (e.button === 1) {
  53. e.preventDefault();
  54. }
  55. };
  56.  
  57. btn.onclick = e => {
  58. if (e.button === 1 && input.value.trim()) {
  59. e.preventDefault();
  60. e.stopImmediatePropagation();
  61. let url = getUrl(input.value);
  62. GM_openInTab(url, true);
  63. return false;
  64. }
  65. };
  66.  
  67. btn.onauxclick = btn.onclick;
  68. }
  69. });
  70.  
  71. waitForElems({
  72. sel: '.sbsb_b li .sbqs_c, .sbsb_b li .sbpqs_d',
  73. onmatch(elem) {
  74. elem.onclick = e => {
  75. if (e.button === 1) {
  76. e.preventDefault();
  77. e.stopImmediatePropagation();
  78. let text = elem.classList.contains('sbpqs_d') ? elem.querySelector('span').textContent : elem.textContent;
  79. let url = getUrl(text);
  80. GM_openInTab(url, true);
  81. return false;
  82. }
  83. };
  84. elem.onauxclick = elem.onclick;
  85. }
  86. });
  87. }());