Ecosia + Google

Adds Google Button to Ecosia.org + a customizable Shortcut (default "G")

  1. // ==UserScript==
  2. // @name Ecosia + Google
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.11
  5. // @description Adds Google Button to Ecosia.org + a customizable Shortcut (default "G")
  6. // @author You
  7. // @match https://www.ecosia.org/*
  8. // @icon https://www.google.com/s2/favicons?domain=ecosia.org
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // >>>>>>>>>>>> SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  16.  
  17. const GOOGLE_SHORTCUT = "G"
  18.  
  19. // <<<<<<<<<<<< SETTINGS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  20.  
  21. const shortcut = {
  22. 'all_shortcuts':{},//All the shortcuts are stored in this array
  23. 'add': function(shortcut_combination,callback,opt) {
  24. //Provide a set of default options
  25. var default_options = {
  26. 'type':'keydown',
  27. 'propagate':false,
  28. 'disable_in_input':false,
  29. 'target':document,
  30. 'keycode':false
  31. }
  32. if(!opt) opt = default_options;
  33. else {
  34. for(var dfo in default_options) {
  35. if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
  36. }
  37. }
  38.  
  39. var ele = opt.target;
  40. if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
  41. var ths = this;
  42. shortcut_combination = shortcut_combination.toLowerCase();
  43.  
  44. //The function to be called at keypress
  45. var func = function(e) {
  46. e = e || window.event;
  47.  
  48. if(opt['disable_in_input']) { //Don't enable shortcut keys in Input, Textarea fields
  49. var element;
  50. if(e.target) element=e.target;
  51. else if(e.srcElement) element=e.srcElement;
  52. if(element.nodeType==3) element=element.parentNode;
  53.  
  54. if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
  55. }
  56.  
  57. //Find Which key is pressed
  58. var code;
  59. if (e.keyCode) code = e.keyCode;
  60. else if (e.which) code = e.which;
  61. var character = String.fromCharCode(code).toLowerCase();
  62.  
  63. if(code == 188) character=","; //If the user presses , when the type is onkeydown
  64. if(code == 190) character="."; //If the user presses , when the type is onkeydown
  65.  
  66. var keys = shortcut_combination.split("+");
  67. //Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
  68. var kp = 0;
  69.  
  70. //Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
  71. var shift_nums = {
  72. "`":"~",
  73. "1":"!",
  74. "2":"@",
  75. "3":"#",
  76. "4":"$",
  77. "5":"%",
  78. "6":"^",
  79. "7":"&",
  80. "8":"*",
  81. "9":"(",
  82. "0":")",
  83. "-":"_",
  84. "=":"+",
  85. ";":":",
  86. "'":"\"",
  87. ",":"<",
  88. ".":">",
  89. "/":"?",
  90. "\\":"|"
  91. }
  92. //Special Keys - and their codes
  93. var special_keys = {
  94. 'esc':27,
  95. 'escape':27,
  96. 'tab':9,
  97. 'space':32,
  98. 'return':13,
  99. 'enter':13,
  100. 'backspace':8,
  101.  
  102. 'scrolllock':145,
  103. 'scroll_lock':145,
  104. 'scroll':145,
  105. 'capslock':20,
  106. 'caps_lock':20,
  107. 'caps':20,
  108. 'numlock':144,
  109. 'num_lock':144,
  110. 'num':144,
  111.  
  112. 'pause':19,
  113. 'break':19,
  114.  
  115. 'insert':45,
  116. 'home':36,
  117. 'delete':46,
  118. 'end':35,
  119.  
  120. 'pageup':33,
  121. 'page_up':33,
  122. 'pu':33,
  123.  
  124. 'pagedown':34,
  125. 'page_down':34,
  126. 'pd':34,
  127.  
  128. 'left':37,
  129. 'up':38,
  130. 'right':39,
  131. 'down':40,
  132.  
  133. 'f1':112,
  134. 'f2':113,
  135. 'f3':114,
  136. 'f4':115,
  137. 'f5':116,
  138. 'f6':117,
  139. 'f7':118,
  140. 'f8':119,
  141. 'f9':120,
  142. 'f10':121,
  143. 'f11':122,
  144. 'f12':123
  145. }
  146.  
  147. var modifiers = {
  148. shift: { wanted:false, pressed:false},
  149. ctrl : { wanted:false, pressed:false},
  150. alt : { wanted:false, pressed:false},
  151. meta : { wanted:false, pressed:false} //Meta is Mac specific
  152. };
  153.  
  154. if(e.ctrlKey) modifiers.ctrl.pressed = true;
  155. if(e.shiftKey) modifiers.shift.pressed = true;
  156. if(e.altKey) modifiers.alt.pressed = true;
  157. if(e.metaKey) modifiers.meta.pressed = true;
  158.  
  159. var k;
  160. for(var i=0; k=keys[i],i<keys.length; i++) {
  161. //Modifiers
  162. if(k == 'ctrl' || k == 'control') {
  163. kp++;
  164. modifiers.ctrl.wanted = true;
  165.  
  166. } else if(k == 'shift') {
  167. kp++;
  168. modifiers.shift.wanted = true;
  169.  
  170. } else if(k == 'alt') {
  171. kp++;
  172. modifiers.alt.wanted = true;
  173. } else if(k == 'meta') {
  174. kp++;
  175. modifiers.meta.wanted = true;
  176. } else if(k.length > 1) { //If it is a special key
  177. if(special_keys[k] == code) kp++;
  178.  
  179. } else if(opt['keycode']) {
  180. if(opt['keycode'] == code) kp++;
  181.  
  182. } else { //The special keys did not match
  183. if(character == k) kp++;
  184. else {
  185. if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug created by using lowercase
  186. character = shift_nums[character];
  187. if(character == k) kp++;
  188. }
  189. }
  190. }
  191. }
  192.  
  193. if(kp == keys.length &&
  194. modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
  195. modifiers.shift.pressed == modifiers.shift.wanted &&
  196. modifiers.alt.pressed == modifiers.alt.wanted &&
  197. modifiers.meta.pressed == modifiers.meta.wanted) {
  198. callback(e);
  199.  
  200. if(!opt['propagate']) { //Stop the event
  201. //e.cancelBubble is supported by IE - this will kill the bubbling process.
  202. e.cancelBubble = true;
  203. e.returnValue = false;
  204.  
  205. //e.stopPropagation works in Firefox.
  206. if (e.stopPropagation) {
  207. e.stopPropagation();
  208. e.preventDefault();
  209. }
  210. return false;
  211. }
  212. }
  213. }
  214. this.all_shortcuts[shortcut_combination] = {
  215. 'callback':func,
  216. 'target':ele,
  217. 'event': opt['type']
  218. };
  219. //Attach the function with the event
  220. if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
  221. else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
  222. else ele['on'+opt['type']] = func;
  223. },
  224.  
  225. //Remove the shortcut - just specify the shortcut and I will remove the binding
  226. 'remove':function(shortcut_combination) {
  227. shortcut_combination = shortcut_combination.toLowerCase();
  228. var binding = this.all_shortcuts[shortcut_combination];
  229. delete(this.all_shortcuts[shortcut_combination])
  230. if(!binding) return;
  231. var type = binding['event'];
  232. var ele = binding['target'];
  233. var callback = binding['callback'];
  234.  
  235. if(ele.detachEvent) ele.detachEvent('on'+type, callback);
  236. else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
  237. else ele['on'+type] = false;
  238. }
  239. }
  240.  
  241. const googleCont = document.createElement("div");
  242. googleCont.style = "display: flex; align-items: center; justify-content: center; width: 4px; height: 70px; margin-bottom: -20px;"
  243. const google = document.createElement("a");
  244. google.id = "google"
  245. googleCont.appendChild(google);
  246. google.innerHTML = `
  247. <svg viewBox="0 0 24 24" width="24" height="24" xmlns="http://www.w3.org/2000/svg">
  248. <g transform="matrix(1, 0, 0, 1, 27.009001, -39.238998)">
  249. <path fill="#4285F4" d="M -3.264 51.509 C -3.264 50.719 -3.334 49.969 -3.454 49.239 L -14.754 49.239 L -14.754 53.749 L -8.284 53.749 C -8.574 55.229 -9.424 56.479 -10.684 57.329 L -10.684 60.329 L -6.824 60.329 C -4.564 58.239 -3.264 55.159 -3.264 51.509 Z"/>
  250. <path fill="#34A853" d="M -14.754 63.239 C -11.514 63.239 -8.804 62.159 -6.824 60.329 L -10.684 57.329 C -11.764 58.049 -13.134 58.489 -14.754 58.489 C -17.884 58.489 -20.534 56.379 -21.484 53.529 L -25.464 53.529 L -25.464 56.619 C -23.494 60.539 -19.444 63.239 -14.754 63.239 Z"/>
  251. <path fill="#FBBC05" d="M -21.484 53.529 C -21.734 52.809 -21.864 52.039 -21.864 51.239 C -21.864 50.439 -21.724 49.669 -21.484 48.949 L -21.484 45.859 L -25.464 45.859 C -26.284 47.479 -26.754 49.299 -26.754 51.239 C -26.754 53.179 -26.284 54.999 -25.464 56.619 L -21.484 53.529 Z"/>
  252. <path fill="#EA4335" d="M -14.754 43.989 C -12.984 43.989 -11.404 44.599 -10.154 45.789 L -6.734 42.369 C -8.804 40.429 -11.514 39.239 -14.754 39.239 C -19.444 39.239 -23.494 41.939 -25.464 45.859 L -21.484 48.949 C -20.534 46.099 -17.884 43.989 -14.754 43.989 Z"/>
  253. </g>
  254. </svg>
  255. `
  256. google.href = "https://google.de/search"+window.location.search
  257. document.getElementsByClassName("row")[0].insertBefore(googleCont,document.getElementsByClassName("row")[0].children[2])
  258.  
  259. shortcut.add(GOOGLE_SHORTCUT,function() {google.click()},
  260. {
  261. type: "keydown",
  262. 'disable_in_input': true
  263. });
  264. })();

QingJ © 2025

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