移动端搜索引擎切换

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

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