切换百度谷歌搜索结果

在百度中点击按钮以使用相同关键词在google搜索。在google中点击按钮以使用相同关键词在百度中搜索。

  1. // ==UserScript==
  2. // @name 切换百度谷歌搜索结果
  3. // @name BaiduGoogleResultSwitcher
  4. // @namespace https://github.com/NiaoBlush/BaiduGoogleSwitcher
  5. // @version 1.0
  6. // @description 在百度中点击按钮以使用相同关键词在google搜索。在google中点击按钮以使用相同关键词在百度中搜索。
  7. // @author NiaoBlush
  8. // @license MIT
  9. // @grant none
  10. // @include https://www.google.com/search?*
  11. // @include https://www.baidu.com/s?*
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. const switcherBtnId = "baidu-google-switcher-btn-id";
  18. const site = getCurrentSite();
  19.  
  20. let parent;
  21. let btnText;
  22. let logoUrl;
  23. if (site === "baidu") {
  24. parent = document.getElementsByClassName("s_tab_inner")[0];
  25. btnText = "Google一下";
  26. logoUrl = "https://www.google.com/favicon.ico";
  27. } else if (site === "google") {
  28. parent = document.getElementsByTagName("g-header-menu")[1].parentElement;
  29. btnText = "百度一下";
  30. logoUrl = "https://www.baidu.com/favicon.ico";
  31. }
  32.  
  33. createButton(parent, btnText, logoUrl);
  34.  
  35. /**
  36. * 创建按钮并插入
  37. * @param parent 父元素
  38. * @param btnText 按钮文本
  39. * @param logoUrl 图标url
  40. */
  41. function createButton(parent, btnText, logoUrl) {
  42. const imgSize = 16;
  43.  
  44. let className = parent.lastElementChild.className;
  45. let button = document.createElement("a");
  46. button.className = className;
  47. button.id = switcherBtnId;
  48. button.style.width = "100px"
  49. button.style.cursor = "pointer";
  50. button.onclick = () => redirect();
  51.  
  52. let img = document.createElement("img");
  53. img.src = logoUrl;
  54. img.style.height = `${imgSize}px`;
  55. img.style.width = `${imgSize}px`;
  56.  
  57. let text = document.createElement("span");
  58. text.innerText = btnText;
  59.  
  60. if (logoUrl) {
  61. button.appendChild(img);
  62. }
  63. button.appendChild(text);
  64. parent.appendChild(button);
  65. }
  66.  
  67. /**
  68. * 获取当前网站
  69. * @returns {string} baidu | google
  70. */
  71. function getCurrentSite() {
  72. if (location.hostname.indexOf("baidu") > -1) {
  73. return "baidu";
  74. } else if (location.hostname.indexOf("google") > -1) {
  75. return "google";
  76. } else {
  77. return "unknown host";
  78. }
  79. }
  80.  
  81. /**
  82. * 获取当前关键字
  83. * @returns {string} 关键字
  84. */
  85. function getKeyword() {
  86. const urlParams = new URLSearchParams(window.location.search);
  87. let keyword = "";
  88. if (site === "baidu") {
  89. keyword = urlParams.get("wd");
  90. } else if (site === "google") {
  91. keyword = urlParams.get("q");
  92. }
  93. return keyword;
  94. }
  95.  
  96. /**
  97. * 重定向
  98. */
  99. function redirect() {
  100. document.getElementById(switcherBtnId).innerText = "redirecting";
  101. const urlParams = new URLSearchParams(window.location.search);
  102. let url = "";
  103. if (site === "google") {
  104. url = `https://www.baidu.com/s?wd=${getKeyword()}`;
  105. } else if (site === "baidu") {
  106. url = `https://www.google.com/search?q=${getKeyword()}`;
  107. }
  108. if (url) {
  109. location.href = url;
  110. }
  111. }
  112.  
  113. })();

QingJ © 2025

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