-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathKamite One-Click OCR.user.js
More file actions
80 lines (69 loc) · 1.99 KB
/
Kamite One-Click OCR.user.js
File metadata and controls
80 lines (69 loc) · 1.99 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
// ==UserScript==
// @name Kamite One-Click OCR
// @version 0.1.1
// @description Send OCR requests to Kamite with a single mouse click
// @icon https://raw.githubusercontent.com/fauu/Kamite/master/src/web/res/favicon/favicon-192.png
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
"use strict";
// !!! CONFIG
const KAMITE_ENDPOINT_BASE = "http://localhost:4110"
const MOUSE_BUTTON = 1; // 1 = Middle, 2 = Right
const LONG_CLICK_DELAY_MS = 375;
// !!!
const CMD_ENDPOINT = `${KAMITE_ENDPOINT_BASE}/cmd`;
main();
function main() {
let longClickTimeout;
let wasLongClick = false;
function handleMousedown(event) {
if (event.button !== MOUSE_BUTTON) {
return;
}
if (longClickTimeout) {
clearTimeout(longClickTimeout);
}
longClickTimeout = setTimeout(() => {
wasLongClick = true;
sendKamiteCMD("ocr/manual-block");
}, LONG_CLICK_DELAY_MS);
}
function handleMouseup(event) {
if (event.button !== MOUSE_BUTTON) {
return;
}
if (longClickTimeout) {
clearTimeout(longClickTimeout);
}
if (wasLongClick) {
wasLongClick = false;
return;
}
sendKamiteCMD("ocr/auto-block", '{"mode":"instant"}');
}
function sendKamiteCMD(name, data) {
GM_xmlhttpRequest({
method: "POST",
url: `${CMD_ENDPOINT}/${name}`,
data: data,
headers: {
"Content-Type": "application/json"
},
onload: function(res) {
if (res.status !== 200) {
console.error("Got unexpected status code");
}
},
onerror: function() {
console.error("Request error");
}
});
}
document.addEventListener("mousedown", handleMousedown);
document.addEventListener("mouseup", handleMouseup);
if (MOUSE_BUTTON === 2) {
document.addEventListener("contextmenu", e => e.preventDefault(), false);
}
}
})();