-
Notifications
You must be signed in to change notification settings - Fork 2
/
translate-remove-newline.js
49 lines (42 loc) · 1.6 KB
/
translate-remove-newline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// ==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/*
// @updateURL https://raw.githubusercontent.com/yzx9/Tampermonkey/main/translate-remove-newline.js
// @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));
})();