Index: gc_lang/fr/webext/background.js ================================================================== --- gc_lang/fr/webext/background.js +++ gc_lang/fr/webext/background.js @@ -289,10 +289,14 @@ let iPortId = xPort.sender.tab.id; // identifier for the port: each port can be found at dConnx[iPortId] dConnx.set(iPortId, xPort); xPort.onMessage.addListener(function (oRequest) { let {sCommand, oParam, oInfo} = oRequest; switch (sCommand) { + case "ping": + //console.log("[background] ping"); + xPort.postMessage({sActionDone: "ping", result: null, bInfo: null, bEnd: true, bError: false}); + break; case "parse": case "parseAndSpellcheck": case "parseAndSpellcheck1": case "parseFull": case "getListOfTokens": 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 @@ -278,12 +278,28 @@ /* Connexion to the background */ 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.") + this.xConnect = browser.runtime.connect({name: "content-script port"}); + this.listen(); + this.ping(); + }, /* Send messages to the background object { sCommand: the action to perform @@ -291,16 +307,22 @@ oInfo: all kind of informations that needs to be sent back (usually to know where to use the result) } */ send: function (sCommand, oParam={}, oInfo={}) { - if (this.xConnect) { + if (this.bConnected) { this.xConnect.postMessage({ sCommand: sCommand, oParam: oParam, oInfo: oInfo }); } else { - oGrammalecte.showMessage("Erreur. La connexion vers le correcteur grammatical est perdue."); + oGrammalecte.showMessage("Erreur. La connexion vers le correcteur grammatical est perdue.", + "Tentative de reconnexion. Fermer la fenêtre et relancez. Si ça ne fonctionne pas, il sera nécessaire de recharger la page."); + this.restart(); } }, + + ping: function () { + this.xConnect.postMessage({ sCommand: "ping", oParam: {}, oInfo: {} }); + }, parseAndSpellcheck: function (sText, sDestination) { this.send("parseAndSpellcheck", { sText: sText, sCountry: "FR", bDebug: false, bContext: false }, { sDestination: sDestination }); }, @@ -347,21 +369,26 @@ } else if (browser.runtime.lastError) { sError = browser.runtime.lastError.message; } console.log("[Grammalecte] Connection to the background script has been lost. Error :", sError); - this.xConnect = browser.runtime.connect({name: "content-script port"}); - this.listen(); + this.bConnected = false; + this.restart(); }.bind(this)); this.xConnect.onMessage.addListener(function (oMessage) { let { sActionDone, result, oInfo, bEnd, bError } = oMessage; switch (sActionDone) { case "init": + this.bConnected = true; oGrammalecte.sExtensionUrl = oMessage.sUrl; oGrammalecte.listen(); oGrammalecte.createButton(); break; + case "ping": + console.log("[Grammalecte] Connection to background done."); + this.bConnected = true; + break; case "parseAndSpellcheck": if (oInfo.sDestination == "__GrammalectePanel__") { if (!bEnd) { oGrammalecte.oGCPanel.addParagraphResult(result); } else { @@ -438,13 +465,13 @@ } 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; default: - console.log("[Content script] Unknown command: " + sActionDone); + console.log("[Grammalecte] Content-script. Unknown command: ", sActionDone); } - }); + }.bind(this)); }, /* Other messages from background */ @@ -462,18 +489,18 @@ } else { oGrammalecte.startGCPanel(oGrammalecte.getPageText()); } break; default: - console.log("[Content script] Unknown command: " + sActionDone); + console.log("[Grammalecte] Content-script. Unknown command: ", sActionRequest); } }); } } -oGrammalecteBackgroundPort.listen() -oGrammalecteBackgroundPort.listen2() + +oGrammalecteBackgroundPort.start(); /* Callable API for the webpage.