google translate batch upload script

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==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)
                }
            }
        }
    })
})();