翻译移除换行符

移除谷歌翻译框中的换行符,尤其适用于PDF复制文字

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         翻译移除换行符
// @namespace    https://github.com/yzx9/
// @version      0.0.5
// @description  移除谷歌翻译框中的换行符,尤其适用于PDF复制文字
// @author       [email protected]
// @match        https://translate.google.cn/*
// @match        https://translate.google.com/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const validLineReg = [/^\s*$/, /\.\s*$/, /。\s*$/]
    const removeNewline = text => text
        .split('\n')
        .reduce((arr, val) => {
            arr[arr.length-1] += val + ' '
            if (validLineReg.some(r => r.test(val))) arr.push('')
            return arr
        }, [''])
        .join('\n')

    const listener = () => {
        const state = window.location.search
            .substring(1)
            .split("&")
            .map(a => a.split('='))
            .reduce((q, [k, ...v]) => ({ ...q, [k]: v.join('=') }), {})

        if (!state.text) return

        const text = removeNewline(decodeURIComponent(state.text))
        if (text === state.text) return

        const newState = { ...state, text }
        const query = Reflect.ownKeys(newState)
            .map(k => [k, encodeURIComponent(newState[k])])
            .map(([k, v]) => `${k}=${v}`)
            .join('&')
        const href = window.location.href.split('?')[0]
        window.location.href = `${href}?${query}`
    }

    window.addEventListener('paste', () => setTimeout(listener, 5));
})();