-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpapago-paster.js
61 lines (51 loc) · 2.27 KB
/
papago-paster.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
50
51
52
53
54
55
56
57
58
59
60
61
// ==UserScript==
// @name Auto Paste Clipboard to Papago (Japanese Only)
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Continuously check the clipboard and paste Japanese text into the Papago textarea input box, mimicking user pasting actions.
// @match https://papago.naver.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
let lastClipboardText = ''; // Tracks the last clipboard text
// Function to check if text contains Japanese characters
function isJapanese(text) {
// Regex to match Japanese characters (Hiragana, Katakana, Kanji)
const japaneseRegex = /[\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}]|[\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF]/u;
return japaneseRegex.test(text);
}
// Function to simulate user paste action into the textarea
function pasteIntoTextarea(text) {
const textArea = document.querySelector('textarea#txtSource');
if (textArea) {
// Clear the textarea before pasting
textArea.focus();
textArea.select();
document.execCommand('delete');
// Set the new value
textArea.value = text;
// Dispatch input and change events to notify the page
textArea.dispatchEvent(new Event('input', { bubbles: true }));
textArea.dispatchEvent(new Event('change', { bubbles: true }));
// Trigger a custom event if necessary
const event = new Event('input', { bubbles: true, cancelable: true });
textArea.dispatchEvent(event);
}
}
// Function to check the clipboard and paste if necessary
function processClipboard() {
navigator.clipboard.readText().then(clipText => {
if (clipText && clipText !== lastClipboardText && isJapanese(clipText)) {
// Update the last clipboard text
lastClipboardText = clipText;
// Paste the clipboard text into the textarea
pasteIntoTextarea(clipText);
}
}).catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
}
// Check clipboard content every second
setInterval(processClipboard, 1000);
})();