移动端搜索引擎切换

移动端搜索引擎切换,必应,谷歌,百度

当前为 2023-10-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Mobile search engineer switcher
  3. // @name:zh-CN 移动端搜索引擎切换
  4. // @namespace http://tampermonkey.net/
  5. // @version 2023.10.28
  6. // @description mobile search engineer switcher
  7. // @description:zh-CN 移动端搜索引擎切换,必应,谷歌,百度
  8. // @author Andy Yuen
  9. // @include https://*.bing.*/search*
  10. // @include https://*.google.*/search*
  11. // @include https://*.baidu.*/*wd*
  12. // @include https://*.baidu.*/*word*
  13. // @icon https://www.google.com/s2/favicons?sz=64&domain=bing.com
  14. // @license MIT
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20.  
  21. console.log('mobile search engineer switcher')
  22.  
  23. if (!/Mobi|Android|iPhone/i.test(navigator.userAgent)) return;
  24.  
  25. // Disable the scroll to top functionality
  26. if (location.host.includes('bing.com')) {
  27. window.addEventListener('focus', function () {
  28. window.scrollTo = function (x, y) {
  29. if (y !== 0) {
  30. window.scrollTo.originalFunc(x, y);
  31. }
  32. };
  33. window.scrollTo.originalFunc = window.scrollTo;
  34. });
  35. }
  36.  
  37. // search engineer
  38. let selector, mapCallback;
  39. if (location.host.includes('bing.com')) {
  40. selector = '[role="navigation"] ul';
  41. mapCallback = ([name, nameCn, url]) => {
  42. let element = document.createElement('li');
  43. element.className = 'injection-mses';
  44. element.innerHTML = `<a>${nameCn}</a>`;
  45. element.onclick = () => {
  46. location.href = 'https://' + url + new URLSearchParams(location.search).get('q');
  47. };
  48. return element;
  49. };
  50. } else if (location.host.includes('google.com')) {
  51. selector = '#hdtb-msb';
  52. mapCallback = ([name, nameCn, url]) => {
  53. let element = document.createElement('li');
  54. element.className = 'injection-mses hdtb-mitem';
  55. element.innerHTML = `<a>${nameCn}</a>`;
  56. element.onclick = () => {
  57. location.href = 'https://' + url + new URLSearchParams(location.search).get('q');
  58. };
  59. return element;
  60. };
  61. } else if (location.host.includes('baidu.com')) {
  62. selector = '.se-tab-lists';
  63. mapCallback = ([name, nameCn, url]) => {
  64. let element = document.createElement('a');
  65. element.className = 'injection-mses se-tabitem';
  66. element.innerHTML = `<span>${nameCn}</span>`;
  67. element.onclick = () => {
  68. const params = new URLSearchParams(location.search);
  69. location.href = 'https://' + url + (params.get('wd') || params.get('word'));
  70. };
  71. return element;
  72. };
  73. }
  74.  
  75. function inject() {
  76. if (document.querySelector(selector) && !document.querySelector('.injection-mses')) {
  77. document.querySelector(selector).prepend(...[
  78. ['google', '谷歌', 'google.com/search?q='],
  79. ['bing', '必应', 'bing.com/search?q='],
  80. ['baidu', '百度', 'baidu.com/s?wd=']
  81. ].filter(([name]) => !location.host.includes(name)).map(mapCallback));
  82. } else {
  83. setTimeout(inject, 10);
  84. }
  85. }
  86.  
  87. inject();
  88. })();