Alternative search engines 2

Adds search on other sites for google, bing, yandex, duckduckgo

目前为 2024-06-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Alternative search engines 2
  3. // @description Adds search on other sites for google, bing, yandex, duckduckgo
  4. // @namespace 2k1dmg@userscript
  5. // @license GPL version 3 or any later version; http://www.gnu.org/licenses/gpl.html
  6. // @version 0.3.1
  7. // @grant none
  8. // @noframes
  9. // @match *://yandex.com/*
  10. // @match *://yandex.ru/*
  11. // @match *://ya.ru/*
  12. // @match *://www.google.com/*
  13. // @match *://www.google.ru/*
  14. // @match *://www.bing.com/*
  15. // @match *://duckduckgo.com/*
  16. // ==/UserScript==
  17.  
  18. // 2024-06-25
  19.  
  20. (function() {
  21. 'use strict';
  22.  
  23. var SEARCH_ON = '• ';
  24. var SEARCH_END = ' •';
  25. var LINK_BOX_ID = 'oeid-box';
  26. var ENGINES_SEPARATOR = ' - ';
  27. var POSITION = 'left';
  28.  
  29. var ENGINES = [
  30. ['Yandex', 'https://yandex.ru/yandsearch?text='],
  31. ['Ya', 'https://ya.ru/yandsearch?text='],
  32. ['Google', 'https://www.google.com/search?q='],
  33. ['Bing', 'https://www.bing.com/search?q='],
  34. ['DuckDuckGo', 'https://duckduckgo.com/?q=']
  35. ];
  36.  
  37. var PLACEHOLDER_SELECTORS = [
  38. '.content__left', // yandex
  39. '.content__left', // ya
  40. '#center_col',/*'#result-stats',*/ // google
  41. '.sb_count', // bing
  42. '.results--main' // duckduckgo
  43. ].join(',');
  44.  
  45. var INPUT_FIELD_SELECTORS = [
  46. '.HeaderForm-Input', // yandex
  47. '.HeaderForm-Input', // ya
  48. 'textarea.gLFyf', // google
  49. '#sb_form_q', // bing
  50. '#search_form_input' // duckduckgo
  51. ].join(',');
  52.  
  53. function addCSSStyle() {
  54. var cssStyle = document.createElement('style');
  55. cssStyle.type = 'text/css';
  56. cssStyle.textContent = [
  57. '#' + LINK_BOX_ID + ' {',
  58. ' display: inline-block;',
  59. ' padding-right: 10px;',
  60. ' padding-bottom: 10px;',
  61. ' color: rgb(115, 115, 115);' ,
  62. ' font-family: Verdana,sans-serif;',
  63. ' font-size: 9pt;',
  64. ' text-align: ' + POSITION + ';',
  65. ' z-index: 10000;',
  66. '}',
  67. '#' + LINK_BOX_ID + ' > a {',
  68. ' text-decoration: none;',
  69. '}'
  70. ].join('\n');
  71. document.head.appendChild(cssStyle);
  72. }
  73.  
  74. function createLinkBox() {
  75. var domain = document.domain.split('.').splice(-2, 2)[0];
  76. var fragment = document.createDocumentFragment();
  77. var divNode = document.createElement('div');
  78. divNode.id = LINK_BOX_ID;
  79. fragment.appendChild(divNode);
  80.  
  81. divNode.appendChild(document.createTextNode(SEARCH_ON));
  82.  
  83. ENGINES.forEach(function(engine) {
  84. if(engine[0].toLowerCase() == domain) {
  85. return;
  86. }
  87. var node = document.createElement('a');
  88. node.target = '_blank';
  89. node.href = engine[1];
  90. node.textContent = engine[0];
  91. divNode.appendChild(node);
  92. divNode.appendChild(document.createTextNode(ENGINES_SEPARATOR));
  93. });
  94.  
  95. divNode.lastChild.textContent = SEARCH_END;
  96. return fragment;
  97. }
  98.  
  99. function linkBoxMouseOver(event) {
  100. var aHref = event.target;
  101. if(aHref.nodeName.toLowerCase() != 'a') {
  102. return;
  103. }
  104.  
  105. var engineSource;
  106. ENGINES.forEach(function(engine) {
  107. if(engine[0] == aHref.textContent) {
  108. engineSource = engine[1];
  109. return;
  110. }
  111. });
  112.  
  113. var engineURL;
  114. var engineParam = '';
  115. if(Array.isArray(engineSource)) {
  116. engineParam = engineSource[1];
  117. engineURL = engineSource[0];
  118. }
  119. else if(typeof engineSource == 'string') {
  120. engineURL = engineSource;
  121. }
  122. else {
  123. return;
  124. }
  125. var searchText = document.querySelector(INPUT_FIELD_SELECTORS);
  126. if(engineURL && searchText && searchText.value.length > 0) {
  127. aHref.href = engineURL + encodeURIComponent(searchText.value) + engineParam;
  128. }
  129. }
  130.  
  131. function linkBoxMouseOut(event) {
  132. var aHref = event.target;
  133. if(aHref.nodeName.toLowerCase() != 'a') {
  134. return;
  135. }
  136. ENGINES.forEach(function(engine) {
  137. if(engine[0] == aHref.textContent) {
  138. aHref.href = engine[1];
  139. return;
  140. }
  141. });
  142. }
  143.  
  144. if(document.getElementById(LINK_BOX_ID)) {
  145. return;
  146. }
  147. var results = document.querySelector(PLACEHOLDER_SELECTORS);
  148. if(!results) {
  149. return;
  150. }
  151. addCSSStyle();
  152. var fragment = createLinkBox();
  153. results.insertBefore(fragment, results.firstChild);
  154.  
  155. var linkBox = document.querySelector('#'+LINK_BOX_ID);
  156.  
  157. linkBox.addEventListener('mouseover', linkBoxMouseOver);
  158. linkBox.addEventListener('mouseout', linkBoxMouseOut);
  159.  
  160. })();