Google Translate Icon Auto Click

Adds a feature that adds a shortcut key that auto-clicks the Google Translate extension icon on Chrome browsers for the quick and better language learning experience.

  1. // ==UserScript==
  2. // @name Google Translate Icon Auto Click
  3.  
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1
  6. // @description Adds a feature that adds a shortcut key that auto-clicks the Google Translate extension icon on Chrome browsers for the quick and better language learning experience.
  7.  
  8. // @author You
  9. // @match http://*/*
  10. // @match https://*/*
  11.  
  12. // @require https://cdn.jsdelivr.net/npm/alertifyjs@1.11.1/build/alertify.min.js
  13. // @resource alertify.min.css https://cdn.jsdelivr.net/npm/alertifyjs@1.11.1/build/css/alertify.min.css
  14. // @resource default.min.css https://cdn.jsdelivr.net/npm/alertifyjs@1.11.1/build/css/themes/default.min.css
  15. // @grant GM_addStyle
  16. // @grant GM_getResourceText
  17.  
  18. // @require https://unpkg.com/url-parse@1.5.1/dist/url-parse.js
  19. // @require https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js
  20. // @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js
  21.  
  22.  
  23. // @run-at document-idle
  24. // @noframes
  25.  
  26. // @license mit
  27. // ==/UserScript==
  28.  
  29.  
  30. function waitForElm(selector) { // wait for element
  31. return new Promise(resolve => {
  32. if (document.querySelector(selector)) {
  33. return resolve(document.querySelector(selector));
  34. }
  35.  
  36. const observer = new MutationObserver(mutations => {
  37. if (document.querySelector(selector)) {
  38. resolve(document.querySelector(selector));
  39. observer.disconnect();
  40. }
  41. });
  42.  
  43. observer.observe(document.body, {
  44. childList: true,
  45. subtree: true
  46. });
  47. });
  48. }
  49.  
  50. function getSelectionText() {
  51.  
  52. var text = "";
  53. if(window.location.hostname == "docs.google.com")
  54. {
  55. var googleDocument = googleDocsUtil.getGoogleDocument();
  56. text = googleDocument.selectedText;
  57. }
  58. else
  59. {
  60. if (window.getSelection) {
  61. text = window.getSelection().toString();
  62. } else if (document.selection && document.selection.type != "Control") {
  63. text = document.selection.createRange().text;
  64. }
  65. }
  66.  
  67. return text.trim();
  68. }
  69.  
  70. // alt + double click to auto click google translate icon
  71.  
  72. document.addEventListener('dblclick', async function(e){
  73. var searchWord = getSelectionText()
  74. if(searchWord != "")
  75. {
  76. if (e.altKey)
  77. {
  78. await waitForElm('#gtx-trans');
  79. document.querySelector('#gtx-trans').click()
  80. }
  81. }
  82. })

QingJ © 2025

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