-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocorrect.user.js
More file actions
170 lines (156 loc) · 6.05 KB
/
Copy pathautocorrect.user.js
File metadata and controls
170 lines (156 loc) · 6.05 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// ==UserScript==
// @name VK Autocorrect
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author SPRAVEDLIVO
// @match https://vk.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=vk.com
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @connect github.com
// @connect githubusercontent.com
// ==/UserScript==
(async function() {
'use strict';
const TRESHOLD = 0.5
const TRESHOLD_RU = 0.5
const s1 = new Array(
"й","ц","у","к","е","н","г","ш","щ","з","х","ъ",
"ф","ы","в","а","п","р","о","л","д","ж","э",
"я","ч","с","м","и","т","ь","б","ю"
)
const s2 = new Array(
"q","w","e","r","t","y","u","i","o","p","[","]",
"a","s","d","f","g","h","j","k","l",";","'",
"z","x","c","v","b","n","m",",","."
)
const en2ru = {};
const ru2en = {};
s2.forEach((key, i) => en2ru[key] = s1[i]);
s1.forEach((key, i) => ru2en[key] = s2[i]);
let xhr = GM_xmlhttpRequest
GM_xmlhttpRequest = function (details) {
return new Promise((resolve, reject) => {
if (typeof details === 'string') {
details = { 'url': details }
}
xhr({
...details,
onload: function (request) {
resolve(request)
return
}
})
})
}
let request = GM_xmlhttpRequest
let json_request = async (details) => {
return JSON.parse((await request(details)).responseText)
}
class Repository {
constructor(name, use_branch) {
this.name = name
this.use_branch = use_branch
}
async get_file_contents(path) {
return (await request(`https://raw.githubusercontent.com/${this.name}/${this.use_branch}/${path}`)).responseText
}
async get_latest_commit() {
let json = await json_request(`https://api.github.com/repos/${this.name}/branches`)
return json.find(it => it.name == this.use_branch)["commit"]["sha"]
}
}
function translit(text, mode) {
let answer = ''
let ch, got
let converter = mode == "en2ru" ? en2ru : ru2en
for (var i = 0; i < text.length; ++i ) {
ch = text[i]
got = converter[ch]
if (got === undefined){
answer += ch;
} else {
answer += got;
}
}
return answer
}
let r = new Repository("dwyl/english-words", "master")
let ru = new Repository("BydloCoding/VkAutocorrect", "main")
let hash = await r.get_latest_commit()
let hash_ru = await r.get_latest_commit()
let saved = GM_getValue("dict_commit_en", "")
let saved_ru = GM_getValue("dict_commit_ru", "")
if (hash != saved) {
console.log("updating dictionary...")
let content = await r.get_file_contents("words_alpha.txt")
GM_setValue("dict_commit_en", hash)
content = content.replace(/[\r]+/g, "").split("\n").map(it => it.toLowerCase())
GM_setValue("content_en", JSON.stringify(content))
}
if (hash_ru != saved_ru) {
console.log("updating dictionary...")
let content = await ru.get_file_contents("russian.txt")
GM_setValue("dict_commit_ru", hash_ru)
content = content.replace(/[\r]+/g, "").split("\n").map(it => it.toLowerCase())
GM_setValue("content_ru", JSON.stringify(content))
}
let words = new Set(JSON.parse(GM_getValue("content_en", "[]")))
let words_ru = new Set(JSON.parse(GM_getValue("content_ru", "[]")))
let english = /[a-z .,\[\]';]+$/g
setInterval(() => {
let messages = Array.from(document.querySelectorAll(".im-mess._im_mess .im-mess--text")).filter(it => !it.hasAttribute("vkac")).reverse().slice(0, 10)
messages.forEach(message => {
let children = Array.from(message.childNodes)
let text = ""
let replace_mode = ""
if (children.length == 0) {text = message.textContent; replace_mode = "oneline"}
else {
replace_mode = "lines"
for (let child of children) {
if (child.nodeName == "#text") {
text += child.nodeValue
}
else if (child.nodeValue == "BR") {
text += "\n"
}
else {
break
}
}
}
let text_words = text.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/gm,"").replace(/\n*/gm, " ").replace(/\s{2,}/g," ").replace("\d*", "").split(" ") || []
text_words = text_words.filter(it => it != '')
// en2ru
let translitirate = []
for (let word of text_words) {
let lowered = word.toLowerCase()
if (english.test(lowered)) {
if (!words.has(lowered)) {
translitirate.push(lowered)
}
}
}
let ratio = translitirate.length / (text_words.length || 1)
if (ratio >= TRESHOLD) {
translitirate = translitirate.map(it => words_ru.has(translit(it, "en2ru")))
let ratio2 = translitirate.filter(Boolean).length / (translitirate.length || 1)
if (ratio2 >= TRESHOLD_RU) {
if (replace_mode == "oneline") {
message.textContent = translit(message.textContent, "en2ru")
}
else {
for (let child of children) {
if (child.nodeName == "#text") {
child.nodeValue = translit(child.nodeValue, "en2ru")
}
}
}
}
}
message.setAttribute("vkac", "")
})
}, 2000)
})();