搜索引擎快捷跳转

在百度和谷歌搜索结果页的搜索按钮旁边添加隐藏的切换按钮,鼠标悬停在搜索按钮上时显示,方便在两个搜索引擎之间快速切换。

  1. // ==UserScript==
  2. // @name 搜索引擎快捷跳转
  3. // @namespace https://gf.qytechs.cn/users/812563
  4. // @icon https://www.google.com/favicon.ico
  5. // @version 1.0.2
  6. // @description 在百度和谷歌搜索结果页的搜索按钮旁边添加隐藏的切换按钮,鼠标悬停在搜索按钮上时显示,方便在两个搜索引擎之间快速切换。
  7. // @author chilinha
  8. // @match *://www.baidu.com/s?*
  9. // @match *://www.google.com/search?*
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. const baiduButtonSelector = '#su';
  16. const googleButtonSelector = '.HZVG1b.Tg7LZd';
  17. let searchButton = document.querySelector(baiduButtonSelector) || document.querySelector(googleButtonSelector);
  18. if (!searchButton) return;
  19. let q = '';
  20. let input = document.createElement('input');
  21. input.style.display = 'none';
  22. input.style.position = 'absolute';
  23. input.style.marginLeft = '10px';
  24. let hideTimeout;
  25. searchButton.addEventListener('mouseenter', function(event) {
  26. const rect = searchButton.getBoundingClientRect();
  27. const x = event.clientX - rect.left;
  28. if (window.location.hostname === 'www.google.com') {
  29. if (rect.width - x <= 44) {
  30. setupSwitchButton('https://www.baidu.com/s?wd=', '百度一下', '#4e6ef2');
  31. }
  32. } else if (window.location.hostname === 'www.baidu.com') {
  33. setupSwitchButton('https://www.google.com/search?q=', 'Google', '#4285f4');
  34. }
  35. });
  36. function setupSwitchButton(baseUrl, buttonText, backgroundColor) {
  37. let searchInput = document.querySelector(window.location.hostname === 'www.baidu.com' ? 'input[name="wd"]' : 'input[name="q"]');
  38. if (searchInput) {
  39. let searchQuery = searchInput.value;
  40. q = baseUrl + encodeURIComponent(searchQuery);
  41. input.setAttribute('value', buttonText);
  42. input.setAttribute('type', 'button');
  43. input.style.color = 'white';
  44. input.style.background = backgroundColor;
  45. input.style.width = '110px';
  46. input.style.height = searchButton.offsetHeight + 'px';
  47. input.style.border = '1px solid';
  48. input.style.borderRadius = '30px';
  49. input.style.cursor = 'pointer';
  50. input.style.display = 'inline';
  51. }
  52. input.style.top = searchButton.offsetTop + 'px';
  53. input.style.left = (searchButton.offsetLeft + searchButton.offsetWidth) + 'px';
  54. clearTimeout(hideTimeout);
  55. }
  56. searchButton.addEventListener('mouseleave', function() {
  57. hideTimeout = setTimeout(() => {
  58. input.style.display = 'none';
  59. }, 500);
  60. });
  61. input.addEventListener('mouseenter', function() {
  62. clearTimeout(hideTimeout);
  63. });
  64. input.addEventListener('mouseleave', function() {
  65. hideTimeout = setTimeout(() => {
  66. input.style.display = 'none';
  67. }, 500);
  68. });
  69. input.addEventListener('click', function() {
  70. window.open(q);
  71. });
  72. searchButton.parentNode.appendChild(input);
  73. })();

QingJ © 2025

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