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
|
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
|
-
+
-
-
-
-
+
+
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
+
-
-
-
|
// Background
/* jshint esversion:6, -W097 */
/* jslint esversion:6 */
/* global GrammalectePanel, oGrammalecte, helpers, showError, Worker, chrome, console */
/* global helpers, showError, Worker, chrome, console */
"use strict";
function showError (e) {
console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
}
// Chrome don’t follow the W3C specification:
// https://browserext.github.io/browserext/
let bChrome = false;
if (typeof(browser) !== "object") {
var browser = chrome;
bChrome = true;
}
const oWorkerHandler = {
xGCEWorker: null,
/*
Worker (separate thread to avoid freezing Firefox)
*/
let xGCEWorker = new Worker("gce_worker.js");
nLastTimeWorkerResponse: 0, // milliseconds since 1970-01-01
oTask: {},
start: function () {
this.xGCEWorker = new Worker("gce_worker.js");
xGCEWorker.onmessage = function (e) {
// https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
try {
let {sActionDone, result, dInfo, bEnd, bError} = e.data;
if (bError) {
console.log(result);
console.log(dInfo);
return;
}
switch (sActionDone) {
case "init":
storeGCOptions(result);
break;
case "parse":
case "parseAndSpellcheck":
case "parseAndSpellcheck1":
case "parseFull":
case "getListOfTokens":
case "getSpellSuggestions":
case "getVerb":
// send result to content script
if (typeof(dInfo.iReturnPort) === "number") {
let xPort = dConnx.get(dInfo.iReturnPort);
xPort.postMessage(e.data);
} else {
console.log("[background] don’t know where to send results");
console.log(e.data);
}
break;
case "textToTest":
case "fullTests":
// send result to panel
browser.runtime.sendMessage(e.data);
break;
case "getOptions":
case "getDefaultOptions":
case "resetOptions":
// send result to panel
storeGCOptions(result);
browser.runtime.sendMessage(e.data);
break;
case "setOptions":
case "setOption":
storeGCOptions(result);
break;
case "setDictionary":
case "setDictionaryOnOff":
//console.log("[background] " + sActionDone + ": " + result);
break;
default:
console.log("[background] Unknown command: " + sActionDone);
console.log(e.data);
}
}
catch (error) {
showError(error);
console.log(e.data);
}
};
this.xGCEWorker.onmessage = function (e) {
// Messages received from the Worker
// https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
try {
this.nLastTimeWorkerResponse = Date.now();
let {sActionDone, result, dInfo, bEnd, bError} = e.data;
if (bError) {
console.log(result);
console.log(dInfo);
return;
}
switch (sActionDone) {
case "init":
storeGCOptions(result);
break;
case "parse":
case "parseAndSpellcheck":
case "parseAndSpellcheck1":
case "parseFull":
case "getListOfTokens":
case "getSpellSuggestions":
case "getVerb":
// send result to content script
if (typeof(dInfo.iReturnPort) === "number") {
let xPort = dConnx.get(dInfo.iReturnPort);
xPort.postMessage(e.data);
} else {
console.log("[background] don’t know where to send results");
console.log(e.data);
}
break;
case "textToTest":
case "fullTests":
// send result to panel
browser.runtime.sendMessage(e.data);
break;
case "getOptions":
case "getDefaultOptions":
case "resetOptions":
// send result to panel
storeGCOptions(result);
browser.runtime.sendMessage(e.data);
break;
case "setOptions":
case "setOption":
storeGCOptions(result);
break;
case "setDictionary":
case "setDictionaryOnOff":
//console.log("[background] " + sActionDone + ": " + result);
break;
default:
console.log("[background] Unknown command: " + sActionDone);
console.log(e.data);
}
}
catch (error) {
showError(error);
console.log(e.data);
}
};
},
getTimeSinceLastResponse: function () {
// result in seconds
return Math.floor((Date.now() - this.nLastTimeWorkerResponse) / 1000);
},
restart: function () {
if (this.getTimeSinceLastResponse() <= 5) {
return false;
}
if (this.xGCEWorker) {
this.xGCEWorker.terminate();
}
this.start();
oInitHandler.initGrammarChecker();
return true;
},
addTask: function () {
//
},
closeTask: function () {
//
}
}
const oInitHandler = {
function initUIOptions (dSavedOptions) {
if (!dSavedOptions.hasOwnProperty("ui_options")) {
browser.storage.local.set({"ui_options": {
textarea: true,
editablenode: true
}});
}
}
initUIOptions: function () {
if (bChrome) {
browser.storage.local.get("ui_options", this._initUIOptions);
return;
}
browser.storage.local.get("ui_options").then(this._initUIOptions, showError);
},
initGrammarChecker: function () {
if (bChrome) {
browser.storage.local.get("gc_options", this._initGrammarChecker);
browser.storage.local.get("personal_dictionary", this._setSpellingDictionaries);
browser.storage.local.get("community_dictionary", this._setSpellingDictionaries);
browser.storage.local.get("oPersonalDictionary", this._setSpellingDictionaries); // deprecated
browser.storage.local.get("sc_options", this._initSCOptions);
return;
}
browser.storage.local.get("gc_options").then(this._initGrammarChecker, showError);
browser.storage.local.get("personal_dictionary").then(this._setSpellingDictionaries, showError);
browser.storage.local.get("community_dictionary").then(this._setSpellingDictionaries, showError);
browser.storage.local.get("oPersonalDictionary").then(this._setSpellingDictionaries, showError); // deprecated
browser.storage.local.get("sc_options").then(this._initSCOptions, showError);
},
_initUIOptions: function (oSavedOptions) {
if (!oSavedOptions.hasOwnProperty("ui_options")) {
browser.storage.local.set({"ui_options": {
textarea: true,
editablenode: true
}});
}
},
function initGrammarChecker (dSavedOptions) {
try {
let dOptions = (dSavedOptions.hasOwnProperty("gc_options")) ? dSavedOptions.gc_options : null;
if (dOptions !== null && Object.getOwnPropertyNames(dOptions).length == 0) {
console.log("# Error: the saved options was an empty object.");
dOptions = null;
}
xGCEWorker.postMessage({
sCommand: "init",
dParam: {sExtensionPath: browser.extension.getURL(""), dOptions: dOptions, sContext: "Firefox"},
dInfo: {}
});
}
catch (e) {
console.log("initGrammarChecker failed");
showError(e);
}
}
_initGrammarChecker: function (oSavedOptions) {
try {
let dOptions = (oSavedOptions.hasOwnProperty("gc_options")) ? oSavedOptions.gc_options : null;
if (dOptions !== null && Object.getOwnPropertyNames(dOptions).length == 0) {
console.log("# Error: the saved options was an empty object.");
dOptions = null;
}
oWorkerHandler.xGCEWorker.postMessage({
sCommand: "init",
dParam: {sExtensionPath: browser.extension.getURL(""), dOptions: dOptions, sContext: "Firefox"},
dInfo: {}
});
}
catch (e) {
console.log("initGrammarChecker failed");
showError(e);
}
},
function setDictionaryOnOff (sDictionary, bActivate) {
xGCEWorker.postMessage({
sCommand: "setDictionaryOnOff",
dParam: { sDictionary: sDictionary, bActivate: bActivate },
dInfo: {}
_setSpellingDictionaries: function (oData) {
if (oData.hasOwnProperty("oPersonalDictionary")) {
// deprecated (to be removed in 2020)
console.log("personal dictionary migration");
browser.storage.local.set({ "personal_dictionary": oData["oPersonalDictionary"] });
oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionary", dParam: { sDictionary: "personal", oDict: oData["oPersonalDictionary"] }, dInfo: {} });
browser.storage.local.remove("oPersonalDictionary");
}
if (oData.hasOwnProperty("community_dictionary")) {
oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionary", dParam: { sDictionary: "community", oDict: oData["community_dictionary"] }, dInfo: {} });
}
if (oData.hasOwnProperty("personal_dictionary")) {
oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionary", dParam: { sDictionary: "personal", oDict: oData["personal_dictionary"] }, dInfo: {} });
});
}
}
},
function initSCOptions (oData) {
if (!oData.hasOwnProperty("sc_options")) {
browser.storage.local.set({"sc_options": {
extended: true,
community: true,
personal: true
}});
setDictionaryOnOff("community", true);
setDictionaryOnOff("personal", true);
} else {
setDictionaryOnOff("community", oData.sc_options["community"]);
setDictionaryOnOff("personal", oData.sc_options["personal"]);
}
}
_initSCOptions: function (oData) {
if (!oData.hasOwnProperty("sc_options")) {
browser.storage.local.set({"sc_options": {
extended: true,
community: true,
personal: true
}});
oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionaryOnOff", dParam: { sDictionary: "community", bActivate: true }, dInfo: {} });
oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionaryOnOff", dParam: { sDictionary: "personal", bActivate: true }, dInfo: {} });
} else {
oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionaryOnOff", dParam: { sDictionary: "community", bActivate: oData.sc_options["community"] }, dInfo: {} });
oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionaryOnOff", dParam: { sDictionary: "personal", bActivate: oData.sc_options["personal"] }, dInfo: {} });
}
}
}
function setDictionary (sDictionary, oDictionary) {
xGCEWorker.postMessage({
sCommand: "setDictionary",
dParam: { sDictionary: sDictionary, oDict: oDictionary },
dInfo: {}
});
}
// start the Worker for the GC
function setSpellingDictionaries (oData) {
if (oData.hasOwnProperty("oPersonalDictionary")) {
// deprecated (to be removed in 2020)
console.log("personal dictionary migration");
browser.storage.local.set({ "personal_dictionary": oData["oPersonalDictionary"] });
setDictionary("personal", oData["oPersonalDictionary"]);
browser.storage.local.remove("oPersonalDictionary");
}
oWorkerHandler.start();
if (oData.hasOwnProperty("personal_dictionary")) {
setDictionary("personal", oData["personal_dictionary"]);
}
if (oData.hasOwnProperty("community_dictionary")) {
setDictionary("community", oData["community_dictionary"]);
}
}
function init () {
// init the options stuff and start the GC
oInitHandler.initUIOptions();
if (bChrome) {
browser.storage.local.get("gc_options", initGrammarChecker);
oInitHandler.initGrammarChecker();
browser.storage.local.get("ui_options", initUIOptions);
browser.storage.local.get("personal_dictionary", setSpellingDictionaries);
browser.storage.local.get("community_dictionary", setSpellingDictionaries);
browser.storage.local.get("oPersonalDictionary", setSpellingDictionaries); // deprecated
browser.storage.local.get("sc_options", initSCOptions);
return;
}
browser.storage.local.get("gc_options").then(initGrammarChecker, showError);
browser.storage.local.get("ui_options").then(initUIOptions, showError);
browser.storage.local.get("personal_dictionary").then(setSpellingDictionaries, showError);
browser.storage.local.get("community_dictionary").then(setSpellingDictionaries, showError);
browser.storage.local.get("oPersonalDictionary").then(setSpellingDictionaries, showError); // deprecated
browser.storage.local.get("sc_options").then(initSCOptions, showError);
}
// When the extension is installed or updated
init();
browser.runtime.onInstalled.addListener(function (oDetails) {
// launched at installation or update
// https://developer.mozilla.org/fr/Add-ons/WebExtensions/API/runtime/onInstalled
if (oDetails.reason == "update" || oDetails.reason == "installed") {
// todo
//browser.tabs.create({url: "http://grammalecte.net"});
}
|