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.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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()
        }
    }
})