18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
// console can’t display error objects from content scripts
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;
}
/*
function loadImage (sContainerClass, sImagePath) {
let xRequest = new XMLHttpRequest();
xRequest.open('GET', browser.extension.getURL("")+sImagePath, false);
xRequest.responseType = "arraybuffer";
xRequest.send();
|
>
>
>
>
>
>
>
|
|
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
|
// console can’t display error objects from content scripts
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;
let bThunderbird = false;
if (typeof(browser) !== "object") {
var browser = chrome;
bChrome = true;
}
if (typeof(messenger) === "object" || browser.hasOwnProperty("composeAction")) {
// JS sucks again.
// In Thunderbird, <browser> exists in content-scripts, but not <messenger>
// <browner> has property <composeAction> but is undefined...
bThunderbird = true;
//console.log("[Grammalecte] Thunderbird...");
}
/*
function loadImage (sContainerClass, sImagePath) {
let xRequest = new XMLHttpRequest();
xRequest.open('GET', browser.extension.getURL("")+sImagePath, false);
xRequest.responseType = "arraybuffer";
xRequest.send();
|
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
const oGrammalecteBackgroundPort = {
bConnected: false,
xConnect: browser.runtime.connect({name: "content-script port"}),
start: function () {
//console.log("[Grammalecte] background port: start.");
this.listen();
this.listen2();
//this.ping();
},
restart: function () {
console.log("[Grammalecte] try to reconnect to the background.")
|
|
|
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
const oGrammalecteBackgroundPort = {
bConnected: false,
xConnect: browser.runtime.connect({name: "content-script port"}),
start: function () {
console.log("[Grammalecte] background port: start.");
this.listen();
this.listen2();
//this.ping();
},
restart: function () {
console.log("[Grammalecte] try to reconnect to the background.")
|
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
if (document.activeElement.tagName == "IFRAME") {
//console.log(document.activeElement.id); frameId given by result is different than frame.id
oGrammalecte.startGCPanel(document.activeElement);
} else {
oGrammalecte.showMessage("Erreur. Le cadre sur lequel vous avez cliqué n’a pas pu être identifié. Sélectionnez le texte à corriger et relancez le correcteur via le menu contextuel.");
}
break;
default:
console.log("[Grammalecte] Content-script. Unknown command: ", sActionDone);
}
}.bind(this));
},
/*
|
>
>
>
>
>
>
>
|
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
if (document.activeElement.tagName == "IFRAME") {
//console.log(document.activeElement.id); frameId given by result is different than frame.id
oGrammalecte.startGCPanel(document.activeElement);
} else {
oGrammalecte.showMessage("Erreur. Le cadre sur lequel vous avez cliqué n’a pas pu être identifié. Sélectionnez le texte à corriger et relancez le correcteur via le menu contextuel.");
}
break;
/*
composeAction
(Thunderbird only)
*/
case "grammar_checker_compose_window":
oGrammalecte.startGCPanel("__ThunderbirdComposeWindow__");
break;
default:
console.log("[Grammalecte] Content-script. Unknown command: ", sActionDone);
}
}.bind(this));
},
/*
|
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
oGrammalecteBackgroundPort.start();
/*
Callable API for the webpage.
*/
document.addEventListener("GrammalecteCall", function (xEvent) {
// GrammalecteCall events are dispatched by functions in the API script
// The script is loaded below.
try {
let oCommand = JSON.parse(xEvent.detail);
switch (oCommand.sCommand) {
case "openPanelForNode":
if (oCommand.sNodeId && document.getElementById(oCommand.sNodeId)) {
oGrammalecte.startGCPanel(document.getElementById(oCommand.sNodeId));
}
break;
case "openPanelForText":
if (oCommand.sText) {
if (oCommand.sText && oCommand.sNodeId && document.getElementById(oCommand.sNodeId)) {
oGrammalecte.startGCPanel(oCommand.sText, document.getElementById(oCommand.sNodeId));
}
else {
oGrammalecte.startGCPanel(oCommand.sText);
}
}
break;
case "parseNode":
if (oCommand.sNodeId && document.getElementById(oCommand.sNodeId)) {
let xNode = document.getElementById(oCommand.sNodeId);
if (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT") {
oGrammalecteBackgroundPort.parseAndSpellcheck(xNode.value, oCommand.sNodeId);
}
else if (xNode.tagName == "IFRAME") {
oGrammalecteBackgroundPort.parseAndSpellcheck(xNode.contentWindow.document.body.innerText, oCommand.sNodeId);
}
else {
oGrammalecteBackgroundPort.parseAndSpellcheck(xNode.innerText, oCommand.sNodeId);
}
}
break;
case "parseText":
if (oCommand.sText && oCommand.sNodeId) {
oGrammalecteBackgroundPort.parseAndSpellcheck(oCommand.sText, oCommand.sNodeId);
}
break;
case "getSpellSuggestions":
if (oCommand.sWord && oCommand.sDestination) {
oGrammalecteBackgroundPort.getSpellSuggestions(oCommand.sWord, oCommand.sDestination, oCommand.sErrorId);
}
break;
default:
console.log("[Grammalecte] Event: Unknown command", oCommand.sCommand);
}
}
catch (e) {
showError(e);
}
});
// The API script must be injected this way to be callable by the page
let xScriptGrammalecteAPI = document.createElement("script");
xScriptGrammalecteAPI.src = browser.extension.getURL("content_scripts/api.js");
document.documentElement.appendChild(xScriptGrammalecteAPI);
/*
Note:
Initialization starts when the background is connected.
See: oGrammalecteBackgroundPort.listen() -> case "init"
*/
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
oGrammalecteBackgroundPort.start();
/*
Callable API for the webpage.
(Not for Thunderbird)
*/
if (!bThunderbird) {
document.addEventListener("GrammalecteCall", function (xEvent) {
// GrammalecteCall events are dispatched by functions in the API script
// The script is loaded below.
try {
let oCommand = JSON.parse(xEvent.detail);
switch (oCommand.sCommand) {
case "openPanelForNode":
if (oCommand.sNodeId && document.getElementById(oCommand.sNodeId)) {
oGrammalecte.startGCPanel(document.getElementById(oCommand.sNodeId));
}
break;
case "openPanelForText":
if (oCommand.sText) {
if (oCommand.sText && oCommand.sNodeId && document.getElementById(oCommand.sNodeId)) {
oGrammalecte.startGCPanel(oCommand.sText, document.getElementById(oCommand.sNodeId));
}
else {
oGrammalecte.startGCPanel(oCommand.sText);
}
}
break;
case "parseNode":
if (oCommand.sNodeId && document.getElementById(oCommand.sNodeId)) {
let xNode = document.getElementById(oCommand.sNodeId);
if (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT") {
oGrammalecteBackgroundPort.parseAndSpellcheck(xNode.value, oCommand.sNodeId);
}
else if (xNode.tagName == "IFRAME") {
oGrammalecteBackgroundPort.parseAndSpellcheck(xNode.contentWindow.document.body.innerText, oCommand.sNodeId);
}
else {
oGrammalecteBackgroundPort.parseAndSpellcheck(xNode.innerText, oCommand.sNodeId);
}
}
break;
case "parseText":
if (oCommand.sText && oCommand.sNodeId) {
oGrammalecteBackgroundPort.parseAndSpellcheck(oCommand.sText, oCommand.sNodeId);
}
break;
case "getSpellSuggestions":
if (oCommand.sWord && oCommand.sDestination) {
oGrammalecteBackgroundPort.getSpellSuggestions(oCommand.sWord, oCommand.sDestination, oCommand.sErrorId);
}
break;
default:
console.log("[Grammalecte] Event: Unknown command", oCommand.sCommand);
}
}
catch (e) {
showError(e);
}
});
// The API script must be injected this way to be callable by the page
let xScriptGrammalecteAPI = document.createElement("script");
xScriptGrammalecteAPI.src = browser.extension.getURL("content_scripts/api.js");
document.documentElement.appendChild(xScriptGrammalecteAPI);
}
/*
Note:
Initialization starts when the background is connected.
See: oGrammalecteBackgroundPort.listen() -> case "init"
*/
|