-
Notifications
You must be signed in to change notification settings - Fork 8
/
livemining.js
225 lines (197 loc) · 6.05 KB
/
livemining.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
function reset_fields()
{
document.getElementById("containfields").innerHTML = "";
}
function add_option(input, text, selected=false)
{
let option = document.createElement("option");
option.value = text;
option.innerText = text;
option.selected = selected;
input.appendChild(option);
}
function add_options(input, preselect="")
{
let list = [
"nothing",
"found_spelling",
"readings",
"definitions",
"definitions_raw",
"found_text",
"context",
"context_left",
"context_right",
"audio_anki",
"audio_kanji",
"audio_reading",
"jmdict_id",
"url",
"page_title",
"time_unix",
"time_local",
"readings_raw",
"spellings_raw",
];
for (let ident of list)
{
add_option(input, ident, (ident == preselect));
}
}
function get_fields()
{
let mappings = new Map();
for(let element of document.querySelectorAll("#containfields > div"))
{
console.log(element);
mappings.set(element.querySelector("label").innerText, element.querySelector("select").value);
}
return mappings;
}
function add_field(fieldname, preselect="")
{
let div = document.createElement("div");
let input = document.createElement("select");
input.id = fieldname;
add_options(input, preselect);
let label = document.createElement("label");
label.for = fieldname;
label.innerText = fieldname;
div.appendChild(input);
div.appendChild(label);
document.getElementById("containfields").appendChild(div);
}
var __host = "http://127.0.0.1:8765";
function saveConfig()
{
let mappings = new Array();
for(let element of document.querySelectorAll("#containfields > div"))
{
console.log(element);
mappings.push([element.querySelector("label").innerText, element.querySelector("select").value]);
}
console.log(mappings);
let setstuff =
{
"livemining_deckname": document.querySelector("#deckname").value,
"livemining_modelname": document.querySelector("#modelname").value,
"livemining_fields": mappings,
};
console.log(setstuff);
browser.storage.local.set(setstuff);
}
function loadConfig()
{
let promise = browser.storage.local.get(["livemining_deckname", "livemining_modelname", "livemining_fields"]);
promise.then((storage) =>
{
if(storage["livemining_deckname"] !== undefined)
document.querySelector("#deckname").value = storage["livemining_deckname"];
if(storage["livemining_modelname"] !== undefined)
document.querySelector("#modelname").value = storage["livemining_modelname"];
reset_fields();
if(storage["livemining_fields"] !== undefined)
for(let pair of storage["livemining_fields"])
add_field(pair[0], pair[1]);
}, (e) => {console.log(e);});
}
function buildpage()
{
document.getElementById("autofields").onclick = (e) =>
{
let request = JSON.parse(`
{"action":"modelFieldNames",
"version":6,
"params": {"modelName":"Basic"}
}`);
request["params"]["modelName"] = document.getElementById("modelname").value;
let json = JSON.stringify(request);
console.log(json);
let xhr = new XMLHttpRequest();
xhr.open("POST", __host, true);
//xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.addEventListener('load', () =>
{
let response = JSON.parse(xhr.responseText);
if (response.result)
{
let old_fields = get_fields();
reset_fields();
for(let fieldname of response.result)
{
let old = old_fields.get(fieldname);
if(old !== undefined)
add_field(fieldname, old);
else
add_field(fieldname);
}
}
else
{
errormessage(response.error);
}
});
xhr.addEventListener('error', () =>
{
errormessage("AnkiConnect messaging failed: unspecified error (Anki is probably not open)");
});
xhr.addEventListener('timeout', () =>
{
errormessage("AnkiConnect messaging failed: timed out");
});
xhr.send(json);
e.preventDefault();
};
document.getElementById("applyfields").onclick = saveConfig;
loadConfig();
document.getElementById("loadfields").onclick = loadConfig;
document.getElementById("upgrade").onclick = (e) =>
{
let json = `
{"action":"upgrade",
"version":6
}`;
let xhr = new XMLHttpRequest();
xhr.open("POST", __host, true);
xhr.send(json);
xhr.addEventListener('load', () =>
{
let response = JSON.parse(xhr.responseText);
if (!response.result)
{
errormessage(response.error);
}
});
xhr.addEventListener('error', () =>
{
errormessage("AnkiConnect messaging failed: unspecified error (Anki is probably not open)");
});
xhr.addEventListener('timeout', () =>
{
errormessage("AnkiConnect messaging failed: timed out");
});
e.preventDefault();
};
}
if (document.readyState == "complete")
{
buildpage();
}
else
{
document.addEventListener("DOMContentLoaded", buildpage);
}
function errormessage(text)
{
if(!text) return;
let mydiv = document.createElement("div");
mydiv.style = "background-color: #111; color: #CCC; font-family: Arial, sans-serif; font-size: 13px; width: 300px; border: 3px double red; position: fixed; right: 25px; top: 25px; z-index: 1000000000000000000000; padding: 5px; border-radius: 3px;"
mydiv.textContent = text;
document.body.appendChild(mydiv);
function delete_later()
{
mydiv.remove();
}
setTimeout(delete_later, 3000);
}