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 @@ -170,15 +170,11 @@ this.oGCPanel.showEditor(); this.oGCPanel.start(what); this.oGCPanel.startWaitIcon(); if (what && bCheckText) { let sText = this.oGCPanel.oTextControl.getText(); - xGrammalectePort.postMessage({ - sCommand: "parseAndSpellcheck", - dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false}, - dInfo: (what.nodeType && what.nodeType === 1) ? {sTextAreaId: what.id} : {} - }); + oGrammalecteBackgroundPort.parseAndSpellcheck(sText, "__GrammalectePanel__"); } }, showMessage: function (sMessage) { this.createMessageBox(); @@ -306,94 +302,193 @@ /* Connexion to the background */ -let xGrammalectePort = browser.runtime.connect({name: "content-script port"}); - -xGrammalectePort.onMessage.addListener(function (oMessage) { - let {sActionDone, result, dInfo, bEnd, bError} = oMessage; - switch (sActionDone) { - case "init": - oGrammalecte.sExtensionUrl = oMessage.sUrl; - oGrammalecte.listenRightClick(); - oGrammalecte.createButtons(); - oGrammalecte.observePage(); - break; - case "parseAndSpellcheck": - if (!bEnd) { - oGrammalecte.oGCPanel.addParagraphResult(result); - } else { - oGrammalecte.oGCPanel.stopWaitIcon(); - oGrammalecte.oGCPanel.endTimer(); - } - break; - case "parseAndSpellcheck1": - oGrammalecte.oGCPanel.refreshParagraph(dInfo.sParagraphId, result); - break; - case "parseFull": - // TODO - break; - case "getListOfTokens": - if (!bEnd) { - oGrammalecte.oGCPanel.addListOfTokens(result); - } else { - oGrammalecte.oGCPanel.stopWaitIcon(); - oGrammalecte.oGCPanel.endTimer(); - } - break; - case "getSpellSuggestions": - oGrammalecte.oGCPanel.oTooltip.setSpellSuggestionsFor(result.sWord, result.aSugg, result.iSuggBlock, dInfo.sErrorId); - break; - case "getVerb": - if (dInfo.bStart) { - oGrammalecte.oGCPanel.conjugateWith(result.oVerb, result.oConjTable); - } else { - oGrammalecte.oGCPanel.displayConj(result.oConjTable); - } - break; - case "workerRestarted": - oGrammalecte.oGCPanel.stopWaitIcon(); - oGrammalecte.oGCPanel.showMessage("Le serveur grammatical a été arrêté et relancé."); - oGrammalecte.oGCPanel.endTimer(); - break; - /* - Commands received from the context menu - (Context menu are initialized in background) - */ - // Grammar checker commands - case "grammar_checker_editable": - if (oGrammalecte.xRightClickedNode !== null) { - oGrammalecte.startGCPanel(oGrammalecte.xRightClickedNode); - } else { - oGrammalecte.showMessage("Erreur. Le node 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; - case "grammar_checker_page": - oGrammalecte.startGCPanel(oGrammalecte.getPageText()); - break; - case "grammar_checker_selection": - 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(); - break; - default: - console.log("[Content script] Unknown command: " + sActionDone); - } -}); +const oGrammalecteBackgroundPort = { + + xConnect: browser.runtime.connect({name: "content-script port"}), + + /* + Send messages to the background + object { + sCommand: the action to perform + dParam: parameters necessary for the execution of the action + dInfo: all kind of informations that needs to be sent back (usually to know where to use the result) + } + */ + parseAndSpellcheck: function (sText, sDestination) { + this.xConnect.postMessage({ + sCommand: "parseAndSpellcheck", + dParam: { sText: sText, sCountry: "FR", bDebug: false, bContext: false }, + dInfo: { sDestination: sDestination } + }); + }, + + parseAndSpellcheck1: function (sText, sDestination, sParagraphId) { + this.xConnect.postMessage({ + sCommand: "parseAndSpellcheck1", + dParam: { sText: sText, sCountry: "FR", bDebug: false, bContext: false }, + dInfo: { sDestination: sDestination, sParagraphId: sParagraphId } + }); + }, + + getListOfTokens: function (sText) { + this.xConnect.postMessage({ sCommand: "getListOfTokens", dParam: { sText: sText }, dInfo: {} }); + }, + + parseFull: function (sText) { + this.xConnect.postMessage({ + sCommand: "parseFull", + dParam: { sText: sTex, sCountry: "FR", bDebug: false, bContext: false }, + dInfo: {} + }); + }, + + getVerb: function (sVerb, bStart=true, bPro=false, bNeg=false, bTpsCo=false, bInt=false, bFem=false) { + this.xConnect.postMessage({ + sCommand: "getVerb", + dParam: { sVerb: sVerb, bPro: bPro, bNeg: bNeg, bTpsCo: bTpsCo, bInt: bInt, bFem: bFem }, + dInfo: { bStart: bStart } + }); + }, + + getSpellSuggestions: function (sWord, sErrorId) { + this.xConnect.postMessage({ sCommand: "getSpellSuggestions", dParam: { sWord: sWord }, dInfo: { sErrorId: sErrorId } }); + }, + + openURL: function (sURL) { + this.xConnect.postMessage({ sCommand: "openURL", dParam: { "sURL": sURL }, dInfo: null }); + }, + + openLexiconEditor: function () { + this.xConnect.postMessage({ sCommand: "openLexiconEditor", dParam: null, dInfo: null }); + }, + + restartWorker: function (nTimeDelay=10) { + this.xConnect.postMessage({ sCommand: "restartWorker", dParam: { "nTimeDelay": nTimeDelay }, dInfo: {} }); + }, + + /* + Messages from the background + */ + listen: function () { + this.xConnect.onMessage.addListener(function (oMessage) { + let {sActionDone, result, dInfo, bEnd, bError} = oMessage; + switch (sActionDone) { + case "init": + oGrammalecte.sExtensionUrl = oMessage.sUrl; + oGrammalecte.listenRightClick(); + oGrammalecte.createButtons(); + oGrammalecte.observePage(); + break; + case "parseAndSpellcheck": + if (dInfo.sDestination == "__GrammalectePanel__") { + if (!bEnd) { + oGrammalecte.oGCPanel.addParagraphResult(result); + } else { + oGrammalecte.oGCPanel.stopWaitIcon(); + oGrammalecte.oGCPanel.endTimer(); + } + } + break; + case "parseAndSpellcheck1": + if (dInfo.sDestination == "__GrammalectePanel__") { + oGrammalecte.oGCPanel.refreshParagraph(dInfo.sParagraphId, result); + } + break; + case "parseFull": + // TODO + break; + case "getListOfTokens": + if (!bEnd) { + oGrammalecte.oGCPanel.addListOfTokens(result); + } else { + oGrammalecte.oGCPanel.stopWaitIcon(); + oGrammalecte.oGCPanel.endTimer(); + } + break; + case "getSpellSuggestions": + oGrammalecte.oGCPanel.oTooltip.setSpellSuggestionsFor(result.sWord, result.aSugg, result.iSuggBlock, dInfo.sErrorId); + break; + case "getVerb": + if (dInfo.bStart) { + oGrammalecte.oGCPanel.conjugateWith(result.oVerb, result.oConjTable); + } else { + oGrammalecte.oGCPanel.displayConj(result.oConjTable); + } + break; + case "workerRestarted": + oGrammalecte.oGCPanel.stopWaitIcon(); + oGrammalecte.oGCPanel.showMessage("Le serveur grammatical a été arrêté et relancé."); + oGrammalecte.oGCPanel.endTimer(); + break; + /* + Commands received from the context menu + (Context menu are initialized in background) + */ + // Grammar checker commands + case "grammar_checker_editable": + if (oGrammalecte.xRightClickedNode !== null) { + oGrammalecte.startGCPanel(oGrammalecte.xRightClickedNode); + } else { + oGrammalecte.showMessage("Erreur. Le node 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; + case "grammar_checker_page": + oGrammalecte.startGCPanel(oGrammalecte.getPageText()); + break; + case "grammar_checker_selection": + 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(); + break; + default: + console.log("[Content script] Unknown command: " + sActionDone); + } + }); + }, + + /* + Other messages from background + */ + listen2: function () { + browser.runtime.onMessage.addListener(function (oMessage) { + let {sActionRequest} = oMessage; + let xActiveNode = document.activeElement; + switch (sActionRequest) { + /* + Commands received from the keyboard (shortcuts) + */ + case "shortcutGrammarChecker": + if (xActiveNode && (xActiveNode.tagName == "TEXTAREA" || xActiveNode.tagName == "INPUT" || xActiveNode.isContentEditable)) { + oGrammalecte.startGCPanel(xActiveNode); + } else { + oGrammalecte.startGCPanel(oGrammalecte.getPageText()); + } + break; + default: + console.log("[Content script] Unknown command: " + sActionDone); + } + }); + } +} + +oGrammalecteBackgroundPort.listen() +oGrammalecteBackgroundPort.listen2() /* Callable API for the webpage. @@ -430,29 +525,5 @@ } catch (e) { showError(e); } }); - - - -/* - Other messages from background -*/ -browser.runtime.onMessage.addListener(function (oMessage) { - let {sActionRequest} = oMessage; - let xActiveNode = document.activeElement; - switch (sActionRequest) { - /* - Commands received from the keyboard (shortcuts) - */ - case "shortcutGrammarChecker": - if (xActiveNode && (xActiveNode.tagName == "TEXTAREA" || xActiveNode.tagName == "INPUT" || xActiveNode.isContentEditable)) { - oGrammalecte.startGCPanel(xActiveNode); - } else { - oGrammalecte.startGCPanel(oGrammalecte.getPageText()); - } - break; - default: - console.log("[Content script] Unknown command: " + sActionDone); - } -}); Index: gc_lang/fr/webext/content_scripts/message_box.js ================================================================== --- gc_lang/fr/webext/content_scripts/message_box.js +++ gc_lang/fr/webext/content_scripts/message_box.js @@ -1,11 +1,11 @@ // JavaScript // Panel creator /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ -/* global oGrammalecte, xGrammalectePort, showError, window, document, console */ +/* global oGrammalecte, showError, window, document, console */ "use strict"; class GrammalecteMessageBox { Index: gc_lang/fr/webext/content_scripts/panel.js ================================================================== --- gc_lang/fr/webext/content_scripts/panel.js +++ gc_lang/fr/webext/content_scripts/panel.js @@ -1,11 +1,11 @@ // JavaScript // Panel creator /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ -/* global GrammalectePanel, oGrammalecte, xGrammalectePort, showError, window, document, console */ +/* global GrammalectePanel, oGrammalecte, showError, window, document, console */ "use strict"; class GrammalectePanel { @@ -270,28 +270,14 @@ this.xPanelMessageActionButton.style.display = "none"; } executeButtonAction (sActionName) { switch (sActionName) { - case "": - break; case "restartWorker": - xGrammalectePort.postMessage({ - sCommand: "restartWorker", - dParam: { "nTimeDelay": 10 }, - dInfo: {} - }); + oGrammalecteBackgroundPort.restartWorker(); this.stopWaitIcon(); break; default: console.log("Action inconnue: ", sAction); } } - - openURL (sURL) { - xGrammalectePort.postMessage({ - sCommand: "openURL", - dParam: {"sURL": sURL}, - dInfo: {} - }); - } } 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 @@ -1,10 +1,10 @@ // JavaScript /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ -/* global GrammalectePanel, oGrammalecte, xGrammalectePort, showError, window, document, console */ +/* global GrammalectePanel, oGrammalecte, oGrammalecteBackgroundPort, showError, window, document, console */ "use strict"; function onGrammalecteGCPanelClick (xEvent) { try { @@ -21,11 +21,11 @@ } else if (xElem.id.startsWith("grammalecte_err") && xElem.className !== "grammalecte_error_corrected" && xElem.className !== "grammalecte_error_ignored") { oGrammalecte.oGCPanel.oTooltip.show(xElem.parentNode, xElem.id); } else if (xElem.id === "grammalecte_tooltip_url" || xElem.id === "grammalecte_tooltip_db_search") { - oGrammalecte.oGCPanel.openURL(xElem.dataset.url); + oGrammalecteBackgroundPort.openURL(xElem.dataset.url); } else { oGrammalecte.oGCPanel.oTooltip.hide(); } } else { oGrammalecte.oGCPanel.oTooltip.hide(); @@ -109,29 +109,21 @@ this.xLxgButton.onclick = () => { if (!this.bWorking) { this.showLexicographer(); this.clearLexicographer(); this.startWaitIcon(); - xGrammalectePort.postMessage({ - sCommand: "getListOfTokens", - dParam: {sText: this.oTextControl.getText()}, - dInfo: ((this.xNode) ? {sTextAreaId: this.xNode.id} : {}) - }); - /*xGrammalectePort.postMessage({ - sCommand: "parseFull", - dParam: {sText: this.oTextControl.getText(), sCountry: "FR", bDebug: false, bContext: false}, - dInfo: ((this.xNode) ? {sTextAreaId: this.xNode.id} : {}) - });*/ + oGrammalecteBackgroundPort.getListOfTokens(this.oTextControl.getText()); + //oGrammalecteBackgroundPort.parseFull(this.oTextControl.getText()) } }; this.xConjButton.onclick = () => { if (!this.bWorking) { this.showConjugueur(); } }; this.xLexEditButton.onclick = () => { - xGrammalectePort.postMessage({sCommand: "openLexiconEditor", dParam: null, dInfo: null}); + oGrammalecteBackgroundPort.openLexiconEditor(); }; // Add tabs to menu this.xMenu.appendChild(this.xTFButton) this.xMenu.appendChild(this.xEditorButton) this.xMenu.appendChild(this.xLxgButton) @@ -170,15 +162,11 @@ this.oTooltip.hide(); this.showEditor(); this.clear(); this.startWaitIcon(); this.resetTimer(); - xGrammalectePort.postMessage({ - sCommand: "parseAndSpellcheck", - dParam: {sText: this.oTextControl.getText(), sCountry: "FR", bDebug: false, bContext: false}, - dInfo: ((this.xNode) ? {sTextAreaId: this.xNode.id} : {}) - }); + oGrammalecteBackgroundPort.parseAndSpellcheck(this.oTextControl.getText(), "__GrammalectePanel__"); } showEditor () { this.switchContentOn(this.xGCPanelContent, this.xEditorButton); this.switchContentOff(this.xLxgPanelContent, this.xLxgButton); @@ -286,15 +274,11 @@ recheckParagraph (iParaNum) { let sParagraphId = "grammalecte_paragraph" + iParaNum; let xParagraph = this.xParent.getElementById(sParagraphId); this._blockParagraph(xParagraph); let sText = this.purgeText(xParagraph.textContent); - xGrammalectePort.postMessage({ - sCommand: "parseAndSpellcheck1", - dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false}, - dInfo: {sParagraphId: sParagraphId} - }); + oGrammalecteBackgroundPort.parseAndSpellcheck1(sText, "__GrammalectePanel__", sParagraphId); this.oTextControl.setParagraph(iParaNum, sText); this.oTextControl.write(); } refreshParagraph (sParagraphId, oResult) { @@ -638,15 +622,11 @@ let bNeg = this.xParent.getElementById('grammalecte_conj_oneg').checked; let bTpsCo = this.xParent.getElementById('grammalecte_conj_otco').checked; let bInt = this.xParent.getElementById('grammalecte_conj_oint').checked; let bFem = this.xParent.getElementById('grammalecte_conj_ofem').checked; if (this.sVerb) { - xGrammalectePort.postMessage({ - sCommand: "getVerb", - dParam: {sVerb: this.sVerb, bPro: bPro, bNeg: bNeg, bTpsCo: bTpsCo, bInt: bInt, bFem: bFem}, - dInfo: {bStart: bStart} - }); + oGrammalecteBackgroundPort.getVerb(this.sVerb, bStart, bPro, bNeg, bTpsCo, bInt, bFem); } } conjugateWith (oVerb, oConjTable) { // function called when results come from the Worker @@ -874,15 +854,11 @@ } else { this.xParent.getElementById("grammalecte_tooltip_db_search").style.display = "none"; } this.clearSuggestionBlock(); this.xTooltipSuggBlock.textContent = "Recherche de graphies possibles…"; - xGrammalectePort.postMessage({ - sCommand: "getSpellSuggestions", - dParam: {sWord: xNodeErr.textContent}, - dInfo: {sErrorId: xNodeErr.dataset.error_id} - }); + oGrammalecteBackgroundPort.getSpellSuggestions(xNodeErr.textContent, xNodeErr.dataset.error_id); } this.xTooltipArrow.style.display = "block"; this.xTooltip.style.display = "block"; } catch (e) { @@ -964,33 +940,33 @@ 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); + this.loadText(this.xNode.value); } else if (this.bIframe) { // iframe if (!this.bResultInEvent) { oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ La zone analysée est un cadre contenant une autre page web (“iframe”). Les changements faits ne peuvent être pas répercutés dans cette zone."); } - this._loadText(this.xNode.contentWindow.document.body.innerText); + this.loadText(this.xNode.contentWindow.document.body.innerText); } else { // editable node oGrammalecte.oGCPanel.addMessageToGCPanel("❗ 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); + this.loadText(this.xNode.innerText); } } setText (sText) { this.clear(); oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ 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); + this.loadText(sText); } - _loadText (sText) { + loadText (sText) { if (typeof(sText) === "string") { this.dParagraph.clear(); let i = 0; let iStart = 0; let iEnd = 0;