-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyandex-paster.js
60 lines (50 loc) · 2.23 KB
/
yandex-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
// ==UserScript==
// @name Auto Paste Clipboard to Yandex Translate (Japanese Only)
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Continuously check the clipboard and paste Japanese text into the Yandex Translate contenteditable div, mimicking user pasting actions.
// @match https://translate.yandex.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 paste text into the contenteditable div
function pasteIntoContentEditable(text) {
const editableDiv = document.querySelector('div#fakeArea');
if (editableDiv) {
// Focus on the div
editableDiv.focus();
// Clear the existing content
editableDiv.innerHTML = '';
// Insert the new text
editableDiv.innerText = text;
// Dispatch input and change events to notify the page
const inputEvent = new Event('input', { bubbles: true });
const changeEvent = new Event('change', { bubbles: true });
editableDiv.dispatchEvent(inputEvent);
editableDiv.dispatchEvent(changeEvent);
}
}
// 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 contenteditable div
pasteIntoContentEditable(clipText);
}
}).catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
}
// Check clipboard content every second
setInterval(processClipboard, 1000);
})();