谷歌翻译自动去除换行

谷歌翻译自动去除没用的换行符,基于hustcc谷歌翻译辅助v0.2修改

目前为 2024-05-14 提交的版本。查看 最新版本

// ==UserScript==
// @name         谷歌翻译自动去除换行
// @version      0.2
// @license      MIT
// @description  谷歌翻译自动去除没用的换行符,基于hustcc谷歌翻译辅助v0.2修改
// @author       yinwaii
// @match        *://translate.google.com/*
// @match        *://translate.google.cn/*
// @connect      translate.google.com
// @grant        unsafeWindow
// @namespace    http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';
    if (window.location.host !== 'translate.google.com' && window.location.host !== 'translate.google.cn') return;

    // 满宽屏!
    const root = document.querySelector('body > div.frame > div.page.tlid-homepage.homepage.translate-text');
    if (root) root.style.maxWidth = '96%';

    let timer = 0;

    function loop() {
        const ele = document.querySelector("textarea");
        if (ele) {
            let t = ele.value;
            if (t) {
                let r = t.replace(/([^\.])\n/g,"$1 ").replace(/\x02/g,"");
                if (r !== t) {
                    console.log(r);
                    ele.value = r;
                    let inputEvent = new Event('input', { bubbles: true });
                    ele.dispatchEvent(inputEvent);
                }
            }
        }
        // timer
        clearTimeout(timer);
        timer = setTimeout(() => {
            loop();
        }, 1000);
    }

    timer = setTimeout(() => {
        loop();
    }, 1000);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址