Grammalecte  Check-in [76d78039bf]

Overview
Comment:[fx] update API and handle iframe node via GC Panel
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | fx
Files: files | file ages | folders
SHA3-256: 76d78039bf5e24dad66946d5123ccd4b8603c28a5bc8eb2c5a27af0c55007b2f
User & Date: olr on 2020-03-12 16:56:57
Other Links: manifest | tags
Context
2020-03-12
22:46
[fx] update API: send GC result as a string (JSON), not an object check-in: 1bb5f94a84 user: olr tags: trunk, fx
16:56
[fx] update API and handle iframe node via GC Panel check-in: 76d78039bf user: olr tags: trunk, fx
2020-03-11
21:44
[fr] faux positif check-in: 607796fcdc user: olr tags: trunk, fr
Changes

Modified gc_lang/fr/webext/content_scripts/api.js from [46315a7e69] to [85027b59ad].

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
// JavaScript

"use strict";


const oGrammalecteAPI = {
    // functions callable from within pages

    sVersion: "1.0",

    parse: function (arg1) {
        let xNode = null;
        if (typeof(arg1) === 'string') {
            if (document.getElementById(arg1)) {
                xNode = document.getElementById(arg1);
            } else {
                this.parseText(arg1);
            }
        }
        else if (arg1 instanceof HTMLElement) {
            xNode = arg1;
        }
        if (xNode) {
            console.log("xnode");
            if (xNode.tagName == "INPUT"  ||  xNode.tagName == "TEXTAREA"  ||  xNode.isContentEditable) {
                this.parseNode(xNode);


            }
            else if (xNode.tagName == "IFRAME") {
                this.parseText(xNode.contentWindow.document.body.innerText);
            }






            else {
                this.parseText(xNode.innerText);

            }

        }







    },

    parseNode: function (xNode) {
        console.log("parseNode");
        if (xNode instanceof HTMLElement) {
            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseNode", xNode: xNode} });
            document.dispatchEvent(xEvent);
        } else {
            console.log("[Grammalecte API] Error: parameter is not a HTML node.");
        }
    },

    parseText: function (sText) {
        console.log("parseText");
        if (typeof(sText) === "string") {
            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseText", sText: sText} });
            document.dispatchEvent(xEvent);
        } else {
            console.log("[Grammalecte API] Error: parameter is not a text.");
        }
    }










|





|






<
|
|
>
>

<
<
|
>
>
>
>
>
>
|
<
>
|
>
|
>
>
>
>
>
>
>



<









<







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
// JavaScript

"use strict";


const oGrammalecteAPI = {
    // functions callable from within pages

    sVersion: "1.0",

    openPanel: function (arg1) {
        let xNode = null;
        if (typeof(arg1) === 'string') {
            if (document.getElementById(arg1)) {
                xNode = document.getElementById(arg1);
            } else {
                this.openPanelForText(arg1);
            }
        }
        else if (arg1 instanceof HTMLElement) {
            xNode = arg1;
        }
        if (xNode) {

            if (xNode.tagName == "INPUT"  ||  xNode.tagName == "TEXTAREA"  ||  xNode.tagName == "IFRAME"  ||  xNode.isContentEditable) {
                this.openPanelForNode(xNode);
            } else {
                this.openPanelForText(xNode.innerText);
            }


        }
    },

    openPanelForNode: function (xNode) {
        if (xNode instanceof HTMLElement) {
            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForNode", xNode: xNode} });
            document.dispatchEvent(xEvent);
        } else {

            console.log("[Grammalecte API] Error: parameter is not a HTML node.");
        }
    },

    openPanelForText: function (sText) {
        if (typeof(sText) === "string") {
            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForText", sText: sText} });
            document.dispatchEvent(xEvent);
        } else {
            console.log("[Grammalecte API] Error: parameter is not a text.");
        }
    },

    parseNode: function (xNode) {

        if (xNode instanceof HTMLElement) {
            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseNode", xNode: xNode} });
            document.dispatchEvent(xEvent);
        } else {
            console.log("[Grammalecte API] Error: parameter is not a HTML node.");
        }
    },

    parseText: function (sText) {

        if (typeof(sText) === "string") {
            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseText", sText: sText} });
            document.dispatchEvent(xEvent);
        } else {
            console.log("[Grammalecte API] Error: parameter is not a text.");
        }
    }

Modified gc_lang/fr/webext/content_scripts/init.js from [83ad0c7397] to [e10aed13d1].

375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
            oGrammalecte.startGCPanel(result, false); // result is the selected text
            // selected text is sent to the GC worker in the background script.
            break;
        case "grammar_checker_iframe":
            console.log("[Grammalecte] selected iframe: ", result);
            if (document.activeElement.tagName == "IFRAME") {
                //console.log(document.activeElement.id); frameId given by result is different than frame.id
                oGrammalecte.startGCPanel(document.activeElement.contentWindow.document.body.innerText);
            } 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;
        // rescan page command
        case "rescanPage":
            oGrammalecte.rescanPage();







|







375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
            oGrammalecte.startGCPanel(result, false); // result is the selected text
            // selected text is sent to the GC worker in the background script.
            break;
        case "grammar_checker_iframe":
            console.log("[Grammalecte] selected iframe: ", result);
            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;
        // rescan page command
        case "rescanPage":
            oGrammalecte.rescanPage();
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419






420
421
422
423
424
425
426
    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);

document.addEventListener("GrammalecteCall", function (xEvent) {
    // GrammalecteCall events are dispatched by functions in the API
    try {
        let oCommand = xEvent.detail;
        switch (oCommand.sCommand) {
            case "parseNode":
                if (oCommand.xNode) {
                    oGrammalecte.startGCPanel(oCommand.xNode);
                }
                break;
            case "parseText":
                if (oCommand.sText) {
                    oGrammalecte.startGCPanel(oCommand.sText);
                }






                break;
            default:
                console.log("[Grammalecte] Event: Unknown command", oCommand.sCommand);
        }
    }
    catch (e) {
        showError(e);







|



|




|



>
>
>
>
>
>







400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
    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);

document.addEventListener("GrammalecteCall", function (xEvent) {
    // GrammalecteCall events are dispatched by functions in the API script
    try {
        let oCommand = xEvent.detail;
        switch (oCommand.sCommand) {
            case "openPanelForNode":
                if (oCommand.xNode) {
                    oGrammalecte.startGCPanel(oCommand.xNode);
                }
                break;
            case "openPanelForText":
                if (oCommand.sText) {
                    oGrammalecte.startGCPanel(oCommand.sText);
                }
                break;
            case "parseNode":
                // todo
                break;
            case "parseText":
                // todo
                break;
            default:
                console.log("[Grammalecte] Event: Unknown command", oCommand.sCommand);
        }
    }
    catch (e) {
        showError(e);

Modified gc_lang/fr/webext/content_scripts/panel_gc.js from [058490fba6] to [88c95d607e].

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        this.clear();
        this.hideMessage();
        this.resetTimer();
        if (typeof(what) === "string") {
            // text
            this.xNode = null;
            this.oTextControl.setText(what);
        } else if (what.nodeType && what.nodeType === 1) {
            // node
            this.xNode = what;
            this.oTextControl.setNode(this.xNode);
        } else {
            // error
            oGrammalecte.oMessageBox.showMessage("[BUG] Analyse d’un élément inconnu…");
            console.log("Grammalecte [bug]:", what);
        }
    }

    setAutoRefreshButton () {
        this.xAutoRefresh.style.backgroundColor = (this.bAutoRefresh) ? "hsl(150, 50%, 50%)" : "";
        this.xAutoRefresh.style.color = (this.bAutoRefresh) ? "hsl(150, 50%, 96%)" : "";
        this.xAutoRefresh.style.opacity = (this.bAutoRefresh) ? "1" : "";







|






|







145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        this.clear();
        this.hideMessage();
        this.resetTimer();
        if (typeof(what) === "string") {
            // text
            this.xNode = null;
            this.oTextControl.setText(what);
        } else if (what.nodeType && what.nodeType === 1) { // 1 = Node.ELEMENT_NODE
            // node
            this.xNode = what;
            this.oTextControl.setNode(this.xNode);
        } else {
            // error
            oGrammalecte.oMessageBox.showMessage("[BUG] Analyse d’un élément inconnu…");
            console.log("[Grammalecte] Unknown element:", what);
        }
    }

    setAutoRefreshButton () {
        this.xAutoRefresh.style.backgroundColor = (this.bAutoRefresh) ? "hsl(150, 50%, 50%)" : "";
        this.xAutoRefresh.style.color = (this.bAutoRefresh) ? "hsl(150, 50%, 96%)" : "";
        this.xAutoRefresh.style.opacity = (this.bAutoRefresh) ? "1" : "";
949
950
951
952
953
954
955
956

957
958
959
960
961
962

963

964











965

966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981


class GrammalecteTextControl {

    constructor () {
        this.xNode = null;
        this.dParagraph = new Map();
        this.bTextArea = null;

        this.bResultInEvent = false; // if true, the node content is not modified, but an event is dispatched on the node with the modified text
    }

    setNode (xNode) {
        this.clear();
        this.xNode = xNode;

        this.bTextArea = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT");

        if (!this.bTextArea) {











            oGrammalecte.oGCPanel.addMessageToGCPanel("Attention : La zone de texte analysée est un champ textuel enrichi susceptible de contenir des éléments non textuels qui seront effacés lors de la correction.");

        }
        this.xNode.disabled = true;
        this.bResultInEvent = Boolean(xNode.dataset.grammalecte_result_via_event && xNode.dataset.grammalecte_result_via_event == "true");
        this._loadText((this.bTextArea) ? this.xNode.value : this.xNode.innerText);
    }

    setText (sText) {
        this.clear();
        oGrammalecte.oGCPanel.addMessageToGCPanel("Note : Aucun champ textuel défini. Les changements ne seront pas répercutés sur la zone d’où le texte a été extrait.");
        this._loadText(sText);
    }

    _loadText (sText) {
        if (typeof(sText) === "string") {
            this.dParagraph.clear();
            let i = 0;







|
>






>

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

>

<
<
<




|







949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981



982
983
984
985
986
987
988
989
990
991
992
993


class GrammalecteTextControl {

    constructor () {
        this.xNode = null;
        this.dParagraph = new Map();
        this.bTextArea = false;
        this.bIframe = false;
        this.bResultInEvent = false; // if true, the node content is not modified, but an event is dispatched on the node with the modified text
    }

    setNode (xNode) {
        this.clear();
        this.xNode = xNode;
        this.bResultInEvent = Boolean(xNode.dataset.grammalecte_result_via_event && xNode.dataset.grammalecte_result_via_event == "true");
        this.bTextArea = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT");
        this.bIframe = (xNode.tagName == "IFRAME");
        if (this.bTextArea) {
            this.xNode.disabled = true;
            this._loadText(this.xNode.value);
        }
        else if (this.bIframe) {
            // iframe
            let sMessage = (this.bResultInEvent) ? "Note : La zone analysée est un cadre contenant une autre page web (“iframe”)." : "Attention : La zone analysée est un cadre contenant une autre page web (“iframe”). Les changements faits ne seront pas répercutés.";
            oGrammalecte.oGCPanel.addMessageToGCPanel(sMessage);
            this._loadText(this.xNode.contentWindow.document.body.innerText);
        }
        else {
            // editable node
            oGrammalecte.oGCPanel.addMessageToGCPanel("Attention : La zone de texte analysée est un champ textuel enrichi susceptible de contenir des éléments non textuels qui seront effacés lors de la correction.");
            this._loadText(this.xNode.innerText);
        }



    }

    setText (sText) {
        this.clear();
        oGrammalecte.oGCPanel.addMessageToGCPanel("Attention : Aucun champ textuel défini. Les changements ne seront pas répercutés sur la zone d’où le texte a été extrait.");
        this._loadText(sText);
    }

    _loadText (sText) {
        if (typeof(sText) === "string") {
            this.dParagraph.clear();
            let i = 0;
992
993
994
995
996
997
998


999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033



1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
        }
    }

    clear () {
        if (this.xNode !== null) {
            this.xNode.disabled = false;
            this.bTextArea = false;


            this.xNode = null;
        }
        this.dParagraph.clear();
    }

    getText () {
        let sText = "";
        this.dParagraph.forEach(function (val, key) {
            sText += val + "\n";
        });
        return sText.slice(0,-1).normalize("NFC");
    }

    setParagraph (iParagraph, sText) {
        this.dParagraph.set(iParagraph, sText);
    }

    eraseNodeContent () {
        while (this.xNode.firstChild) {
            this.xNode.removeChild(this.xNode.firstChild);
        }
    }

    write () {
        if (this.xNode !== null) {
            if (this.bResultInEvent) {
                const xEvent = new CustomEvent("GrammalecteNodeContentUpdated", {
                    detail: { text: [...this.dParagraph.values()].join("\n").normalize("NFC") }
                });
                this.xNode.dispatchEvent(xEvent);
                console.log("event", xEvent.detail.text);
            }
            else if (this.bTextArea) {
                this.xNode.value = this.getText();
            }



            else {
                this.eraseNodeContent();
                this.dParagraph.forEach((val, key) => {
                    this.xNode.appendChild(document.createTextNode(val.normalize("NFC")));
                    this.xNode.appendChild(document.createElement("br"));
                });
            }
        }
    }
}







>
>






<
|
<
<
<
















|


|




>
>
>










1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018

1019



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
        }
    }

    clear () {
        if (this.xNode !== null) {
            this.xNode.disabled = false;
            this.bTextArea = false;
            this.bIframe = false;
            this.bResultInEvent = false;
            this.xNode = null;
        }
        this.dParagraph.clear();
    }

    getText () {

        return [...this.dParagraph.values()].join("\n").normalize("NFC");



    }

    setParagraph (iParagraph, sText) {
        this.dParagraph.set(iParagraph, sText);
    }

    eraseNodeContent () {
        while (this.xNode.firstChild) {
            this.xNode.removeChild(this.xNode.firstChild);
        }
    }

    write () {
        if (this.xNode !== null) {
            if (this.bResultInEvent) {
                const xEvent = new CustomEvent("GrammalecteNodeContentUpdated", {
                    detail: { text: this.getText() }
                });
                this.xNode.dispatchEvent(xEvent);
                console.log("Texte renvoyé via un event :", xEvent.detail.text);
            }
            else if (this.bTextArea) {
                this.xNode.value = this.getText();
            }
            else if (this.bIframe) {
                //console.log(this.getText());
            }
            else {
                this.eraseNodeContent();
                this.dParagraph.forEach((val, key) => {
                    this.xNode.appendChild(document.createTextNode(val.normalize("NFC")));
                    this.xNode.appendChild(document.createElement("br"));
                });
            }
        }
    }
}