google translate batch upload script

for those who need translate text files which can be identified directly by browser(.xml .txt)

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         google translate batch upload script
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description for those who need translate text files which can be identified  directly by browser(.xml .txt)
// @author       bridge
// @match        https://translate.google.cn/*
// @require      https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js
// ==/UserScript==

(function () {
    'use strict';
    //传参
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
        }
        return (false);
    }
    var index = getQueryVariable('myquery')
    if (index !== false) {
        var e = document.createEvent("UIEvent");
        e.initUIEvent("input", true, true);
        document.querySelector('textarea').value = localStorage.getItem(index)
        localStorage.removeItem(index)
        document.querySelector('textarea').dispatchEvent(e);
    } else {
        localStorage.clear()
    }
    let cmp = `<input type="file" class = 'multiSelect' multiple="multiple"></br>`
    document.querySelector('body').insertAdjacentHTML('beforeEnd', cmp)
    $('.multiSelect').css('position', 'fixed')
    $('.multiSelect').css('top', '50%')
    $('.multiSelect').css('width', '70px')
    $('.multiSelect').css('z-index', '100')
    $('.multiSelect').on('input propertychange', function () {
        let files = $('.multiSelect')[0].files
        for (let i = 0; i < files.length; i++) {
            const r = new FileReader()
            r.readAsText(files[i])
            r.onloadend = function () {
                if (r.readyState === 2) {
                    localStorage.setItem(i, r.result.replaceAll(/<.*?>/g, '').replaceAll('\\n\\n', '').replaceAll('\\\'', '\''))
                    window.open('https://translate.google.cn/?myquery=' + i, i)
                }
            }
        }
    })
})();