Grammalecte  Diff

Differences From Artifact [acbd3c2f80]:

To Artifact [689258b9fa]:


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
    // 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") {
    bThunderbird = true;
}

/*
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.")







|







285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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.")
497
498
499
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"
*/







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|

>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
>







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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
    }
}


oGrammalecteBackgroundPort.start();


/*
    ComposeAction
    (Thunderbird only)
*/
if (bThunderbird) {
    console.log("Listen...");
    try {
        browser.composeAction.onClicked.addListener(function (oTab, oData) {
            console.log("START");
            oGrammalecte.startGCPanel("J'en aie mare...");
        });
    }
    catch (e) {
        showError(e)
    }
    console.log("Done.");
}


/*
    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"
*/