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: fx, trunk
16:56
[fx] update API and handle iframe node via GC Panel check-in: 76d78039bf user: olr tags: fx, trunk
2020-03-11
21:44
[fr] faux positif check-in: 607796fcdc user: olr tags: fr, trunk
Changes

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

     4      4   
     5      5   
     6      6   const oGrammalecteAPI = {
     7      7       // functions callable from within pages
     8      8   
     9      9       sVersion: "1.0",
    10     10   
    11         -    parse: function (arg1) {
           11  +    openPanel: function (arg1) {
    12     12           let xNode = null;
    13     13           if (typeof(arg1) === 'string') {
    14     14               if (document.getElementById(arg1)) {
    15     15                   xNode = document.getElementById(arg1);
    16     16               } else {
    17         -                this.parseText(arg1);
           17  +                this.openPanelForText(arg1);
    18     18               }
    19     19           }
    20     20           else if (arg1 instanceof HTMLElement) {
    21     21               xNode = arg1;
    22     22           }
    23     23           if (xNode) {
    24         -            console.log("xnode");
    25         -            if (xNode.tagName == "INPUT"  ||  xNode.tagName == "TEXTAREA"  ||  xNode.isContentEditable) {
    26         -                this.parseNode(xNode);
    27         -            }
    28         -            else if (xNode.tagName == "IFRAME") {
    29         -                this.parseText(xNode.contentWindow.document.body.innerText);
    30         -            }
    31         -            else {
    32         -                this.parseText(xNode.innerText);
           24  +            if (xNode.tagName == "INPUT"  ||  xNode.tagName == "TEXTAREA"  ||  xNode.tagName == "IFRAME"  ||  xNode.isContentEditable) {
           25  +                this.openPanelForNode(xNode);
           26  +            } else {
           27  +                this.openPanelForText(xNode.innerText);
    33     28               }
    34     29           }
    35     30       },
           31  +
           32  +    openPanelForNode: function (xNode) {
           33  +        if (xNode instanceof HTMLElement) {
           34  +            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForNode", xNode: xNode} });
           35  +            document.dispatchEvent(xEvent);
           36  +        } else {
           37  +            console.log("[Grammalecte API] Error: parameter is not a HTML node.");
           38  +        }
           39  +    },
           40  +
           41  +    openPanelForText: function (sText) {
           42  +        if (typeof(sText) === "string") {
           43  +            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForText", sText: sText} });
           44  +            document.dispatchEvent(xEvent);
           45  +        } else {
           46  +            console.log("[Grammalecte API] Error: parameter is not a text.");
           47  +        }
           48  +    },
    36     49   
    37     50       parseNode: function (xNode) {
    38         -        console.log("parseNode");
    39     51           if (xNode instanceof HTMLElement) {
    40     52               let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseNode", xNode: xNode} });
    41     53               document.dispatchEvent(xEvent);
    42     54           } else {
    43     55               console.log("[Grammalecte API] Error: parameter is not a HTML node.");
    44     56           }
    45     57       },
    46     58   
    47     59       parseText: function (sText) {
    48         -        console.log("parseText");
    49     60           if (typeof(sText) === "string") {
    50     61               let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseText", sText: sText} });
    51     62               document.dispatchEvent(xEvent);
    52     63           } else {
    53     64               console.log("[Grammalecte API] Error: parameter is not a text.");
    54     65           }
    55     66       }
    56     67   }

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

   375    375               oGrammalecte.startGCPanel(result, false); // result is the selected text
   376    376               // selected text is sent to the GC worker in the background script.
   377    377               break;
   378    378           case "grammar_checker_iframe":
   379    379               console.log("[Grammalecte] selected iframe: ", result);
   380    380               if (document.activeElement.tagName == "IFRAME") {
   381    381                   //console.log(document.activeElement.id); frameId given by result is different than frame.id
   382         -                oGrammalecte.startGCPanel(document.activeElement.contentWindow.document.body.innerText);
          382  +                oGrammalecte.startGCPanel(document.activeElement);
   383    383               } else {
   384    384                   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.");
   385    385               }
   386    386               break;
   387    387           // rescan page command
   388    388           case "rescanPage":
   389    389               oGrammalecte.rescanPage();
................................................................................
   400    400       The API script must be injected this way to be callable by the page
   401    401   */
   402    402   let xScriptGrammalecteAPI = document.createElement("script");
   403    403   xScriptGrammalecteAPI.src = browser.extension.getURL("content_scripts/api.js");
   404    404   document.documentElement.appendChild(xScriptGrammalecteAPI);
   405    405   
   406    406   document.addEventListener("GrammalecteCall", function (xEvent) {
   407         -    // GrammalecteCall events are dispatched by functions in the API
          407  +    // GrammalecteCall events are dispatched by functions in the API script
   408    408       try {
   409    409           let oCommand = xEvent.detail;
   410    410           switch (oCommand.sCommand) {
   411         -            case "parseNode":
          411  +            case "openPanelForNode":
   412    412                   if (oCommand.xNode) {
   413    413                       oGrammalecte.startGCPanel(oCommand.xNode);
   414    414                   }
   415    415                   break;
   416         -            case "parseText":
          416  +            case "openPanelForText":
   417    417                   if (oCommand.sText) {
   418    418                       oGrammalecte.startGCPanel(oCommand.sText);
   419    419                   }
          420  +                break;
          421  +            case "parseNode":
          422  +                // todo
          423  +                break;
          424  +            case "parseText":
          425  +                // todo
   420    426                   break;
   421    427               default:
   422    428                   console.log("[Grammalecte] Event: Unknown command", oCommand.sCommand);
   423    429           }
   424    430       }
   425    431       catch (e) {
   426    432           showError(e);

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

   145    145           this.clear();
   146    146           this.hideMessage();
   147    147           this.resetTimer();
   148    148           if (typeof(what) === "string") {
   149    149               // text
   150    150               this.xNode = null;
   151    151               this.oTextControl.setText(what);
   152         -        } else if (what.nodeType && what.nodeType === 1) {
          152  +        } else if (what.nodeType && what.nodeType === 1) { // 1 = Node.ELEMENT_NODE
   153    153               // node
   154    154               this.xNode = what;
   155    155               this.oTextControl.setNode(this.xNode);
   156    156           } else {
   157    157               // error
   158    158               oGrammalecte.oMessageBox.showMessage("[BUG] Analyse d’un élément inconnu…");
   159         -            console.log("Grammalecte [bug]:", what);
          159  +            console.log("[Grammalecte] Unknown element:", what);
   160    160           }
   161    161       }
   162    162   
   163    163       setAutoRefreshButton () {
   164    164           this.xAutoRefresh.style.backgroundColor = (this.bAutoRefresh) ? "hsl(150, 50%, 50%)" : "";
   165    165           this.xAutoRefresh.style.color = (this.bAutoRefresh) ? "hsl(150, 50%, 96%)" : "";
   166    166           this.xAutoRefresh.style.opacity = (this.bAutoRefresh) ? "1" : "";
................................................................................
   949    949   
   950    950   
   951    951   class GrammalecteTextControl {
   952    952   
   953    953       constructor () {
   954    954           this.xNode = null;
   955    955           this.dParagraph = new Map();
   956         -        this.bTextArea = null;
          956  +        this.bTextArea = false;
          957  +        this.bIframe = false;
   957    958           this.bResultInEvent = false; // if true, the node content is not modified, but an event is dispatched on the node with the modified text
   958    959       }
   959    960   
   960    961       setNode (xNode) {
   961    962           this.clear();
   962    963           this.xNode = xNode;
          964  +        this.bResultInEvent = Boolean(xNode.dataset.grammalecte_result_via_event && xNode.dataset.grammalecte_result_via_event == "true");
   963    965           this.bTextArea = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT");
   964         -        if (!this.bTextArea) {
   965         -            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.");
          966  +        this.bIframe = (xNode.tagName == "IFRAME");
          967  +        if (this.bTextArea) {
          968  +            this.xNode.disabled = true;
          969  +            this._loadText(this.xNode.value);
          970  +        }
          971  +        else if (this.bIframe) {
          972  +            // iframe
          973  +            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.";
          974  +            oGrammalecte.oGCPanel.addMessageToGCPanel(sMessage);
          975  +            this._loadText(this.xNode.contentWindow.document.body.innerText);
   966    976           }
   967         -        this.xNode.disabled = true;
   968         -        this.bResultInEvent = Boolean(xNode.dataset.grammalecte_result_via_event && xNode.dataset.grammalecte_result_via_event == "true");
   969         -        this._loadText((this.bTextArea) ? this.xNode.value : this.xNode.innerText);
          977  +        else {
          978  +            // editable node
          979  +            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.");
          980  +            this._loadText(this.xNode.innerText);
          981  +        }
   970    982       }
   971    983   
   972    984       setText (sText) {
   973    985           this.clear();
   974         -        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.");
          986  +        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.");
   975    987           this._loadText(sText);
   976    988       }
   977    989   
   978    990       _loadText (sText) {
   979    991           if (typeof(sText) === "string") {
   980    992               this.dParagraph.clear();
   981    993               let i = 0;
................................................................................
   992   1004           }
   993   1005       }
   994   1006   
   995   1007       clear () {
   996   1008           if (this.xNode !== null) {
   997   1009               this.xNode.disabled = false;
   998   1010               this.bTextArea = false;
         1011  +            this.bIframe = false;
         1012  +            this.bResultInEvent = false;
   999   1013               this.xNode = null;
  1000   1014           }
  1001   1015           this.dParagraph.clear();
  1002   1016       }
  1003   1017   
  1004   1018       getText () {
  1005         -        let sText = "";
  1006         -        this.dParagraph.forEach(function (val, key) {
  1007         -            sText += val + "\n";
  1008         -        });
  1009         -        return sText.slice(0,-1).normalize("NFC");
         1019  +        return [...this.dParagraph.values()].join("\n").normalize("NFC");
  1010   1020       }
  1011   1021   
  1012   1022       setParagraph (iParagraph, sText) {
  1013   1023           this.dParagraph.set(iParagraph, sText);
  1014   1024       }
  1015   1025   
  1016   1026       eraseNodeContent () {
................................................................................
  1019   1029           }
  1020   1030       }
  1021   1031   
  1022   1032       write () {
  1023   1033           if (this.xNode !== null) {
  1024   1034               if (this.bResultInEvent) {
  1025   1035                   const xEvent = new CustomEvent("GrammalecteNodeContentUpdated", {
  1026         -                    detail: { text: [...this.dParagraph.values()].join("\n").normalize("NFC") }
         1036  +                    detail: { text: this.getText() }
  1027   1037                   });
  1028   1038                   this.xNode.dispatchEvent(xEvent);
  1029         -                console.log("event", xEvent.detail.text);
         1039  +                console.log("Texte renvoyé via un event :", xEvent.detail.text);
  1030   1040               }
  1031   1041               else if (this.bTextArea) {
  1032   1042                   this.xNode.value = this.getText();
  1033   1043               }
         1044  +            else if (this.bIframe) {
         1045  +                //console.log(this.getText());
         1046  +            }
  1034   1047               else {
  1035   1048                   this.eraseNodeContent();
  1036   1049                   this.dParagraph.forEach((val, key) => {
  1037   1050                       this.xNode.appendChild(document.createTextNode(val.normalize("NFC")));
  1038   1051                       this.xNode.appendChild(document.createElement("br"));
  1039   1052                   });
  1040   1053               }
  1041   1054           }
  1042   1055       }
  1043   1056   }