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.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google Translate Icon Auto Click

// @namespace    http://tampermonkey.net/
// @version      0.1
// @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.

// @author       You
// @match        http://*/*
// @match        https://*/*

// @require      https://cdn.jsdelivr.net/npm/[email protected]/build/alertify.min.js
// @resource     alertify.min.css  https://cdn.jsdelivr.net/npm/[email protected]/build/css/alertify.min.css
// @resource     default.min.css  https://cdn.jsdelivr.net/npm/[email protected]/build/css/themes/default.min.css
// @grant        GM_addStyle
// @grant        GM_getResourceText

// @require      https://unpkg.com/[email protected]/dist/url-parse.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js


// @run-at       document-idle
// @noframes

// @license mit
// ==/UserScript==


function waitForElm(selector) { // wait for element
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }

        const observer = new MutationObserver(mutations => {
            if (document.querySelector(selector)) {
                resolve(document.querySelector(selector));
                observer.disconnect();
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}

function getSelectionText() {

    var text = "";
    if(window.location.hostname == "docs.google.com")
	{
    	var googleDocument = googleDocsUtil.getGoogleDocument();
    	text = googleDocument.selectedText;
	}
    else
    {
    	if (window.getSelection) {
            text = window.getSelection().toString();
        } else if (document.selection && document.selection.type != "Control") {
            text = document.selection.createRange().text;
        }
    }

    return text.trim();
}

// alt + double click to auto click google translate icon

document.addEventListener('dblclick', async function(e){
    var searchWord = getSelectionText()
    if(searchWord != "")
    {
        if (e.altKey)
        {
            await waitForElm('#gtx-trans');
            document.querySelector('#gtx-trans').click()
        }
    }
})