Index: gc_lang/fr/webext/content_scripts/api.js ================================================================== --- gc_lang/fr/webext/content_scripts/api.js +++ gc_lang/fr/webext/content_scripts/api.js @@ -6,51 +6,62 @@ const oGrammalecteAPI = { // functions callable from within pages sVersion: "1.0", - parse: function (arg1) { + openPanel: function (arg1) { let xNode = null; if (typeof(arg1) === 'string') { if (document.getElementById(arg1)) { xNode = document.getElementById(arg1); } else { - this.parseText(arg1); + this.openPanelForText(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); + 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) { - 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."); } } } Index: gc_lang/fr/webext/content_scripts/init.js ================================================================== --- gc_lang/fr/webext/content_scripts/init.js +++ gc_lang/fr/webext/content_scripts/init.js @@ -377,11 +377,11 @@ 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); + 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 @@ -402,23 +402,29 @@ 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 + // GrammalecteCall events are dispatched by functions in the API script try { let oCommand = xEvent.detail; switch (oCommand.sCommand) { - case "parseNode": + case "openPanelForNode": if (oCommand.xNode) { oGrammalecte.startGCPanel(oCommand.xNode); } break; - case "parseText": + 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); } } Index: gc_lang/fr/webext/content_scripts/panel_gc.js ================================================================== --- gc_lang/fr/webext/content_scripts/panel_gc.js +++ gc_lang/fr/webext/content_scripts/panel_gc.js @@ -147,18 +147,18 @@ this.resetTimer(); if (typeof(what) === "string") { // text this.xNode = null; this.oTextControl.setText(what); - } else if (what.nodeType && what.nodeType === 1) { + } 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 [bug]:", what); + console.log("[Grammalecte] Unknown element:", what); } } setAutoRefreshButton () { this.xAutoRefresh.style.backgroundColor = (this.bAutoRefresh) ? "hsl(150, 50%, 50%)" : ""; @@ -951,29 +951,41 @@ class GrammalecteTextControl { constructor () { this.xNode = null; this.dParagraph = new Map(); - this.bTextArea = null; + 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"); - 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.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); } - 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); + 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("Note : Aucun champ textuel défini. Les changements ne seront pas répercutés sur la zone d’où le texte a été extrait."); + 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") { @@ -994,21 +1006,19 @@ 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 () { - let sText = ""; - this.dParagraph.forEach(function (val, key) { - sText += val + "\n"; - }); - return sText.slice(0,-1).normalize("NFC"); + return [...this.dParagraph.values()].join("\n").normalize("NFC"); } setParagraph (iParagraph, sText) { this.dParagraph.set(iParagraph, sText); } @@ -1021,18 +1031,21 @@ write () { if (this.xNode !== null) { if (this.bResultInEvent) { const xEvent = new CustomEvent("GrammalecteNodeContentUpdated", { - detail: { text: [...this.dParagraph.values()].join("\n").normalize("NFC") } + detail: { text: this.getText() } }); this.xNode.dispatchEvent(xEvent); - console.log("event", xEvent.detail.text); + 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"));