Google Lens autoopen translation

https://github.com/srghma-chinese2/srghma-chinese2.github.io

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Google Lens autoopen translation
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  https://github.com/srghma-chinese2/srghma-chinese2.github.io
// @author       [email protected]
// @match        https://lens.google.com/search*
// @license MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant        none
// ==/UserScript==


function doAsynclyOnce({ id, isValid, onValid, onInvalid }) {
    let retries = 300;

    const intervalID = setInterval(_ => {
        const element = document.getElementById(id);
        const valid = isValid(element);
        console.log('trying ', id, ', element=', element, ', valid=', valid)
        if (valid) onValid(element)
        else onInvalid && onInvalid()

        retries--;
        if(retries === 0 || valid) clearInterval(intervalID);
    }, 100);
}

(function() {
    'use strict';
    console.log('asdfasdf')

    doAsynclyOnce({
        id: "translate",
        isValid: spanWithButton => !!spanWithButton,
        onValid: spanWithButton => {
            console.log(spanWithButton)
            spanWithButton.querySelector('button').click()

            doAsynclyOnce({
                id: "ucc-4",
                isValid: element => !!element,
                onInvalid: () => { spanWithButton.querySelector('button').click() },
                onValid: element => {
                    element.click()
                    setTimeout(() => window.close(), 900)
                }
            })
        }
    })
})();