Index: gc_lang/fr/webext/background.js ================================================================== --- gc_lang/fr/webext/background.js +++ gc_lang/fr/webext/background.js @@ -14,77 +14,80 @@ let xGCEWorker = xGCESharedWorker.port; xGCEWorker.onmessage = function (e) { // https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent try { - switch (e.data[0]) { - case "grammar_errors": + let {sActionDone, result, dInfo, bError} = e.data; + switch (sActionDone) { + case "init": + console.log("INIT DONE"); + case "parse": console.log("GRAMMAR ERRORS"); - console.log(e.data[1].aGrammErr); - browser.runtime.sendMessage({sCommand: "grammar_errors", aGrammErr: e.data[1].aGrammErr}); + console.log(result); break; - case "spelling_and_grammar_errors": + case "parseAndSpellcheck": console.log("SPELLING AND GRAMMAR ERRORS"); - console.log(e.data[1].aSpellErr); - console.log(e.data[1].aGrammErr); - break; - case "text_to_test_result": - browser.runtime.sendMessage({sCommand: "text_to_test_result", sResult: e.data[1]}); - break; - case "fulltests_result": - console.log("TESTS RESULTS"); - browser.runtime.sendMessage({sCommand: "fulltests_result", sResult: e.data[1]}); - break; - case "options": + console.log(result.aSpellErr); + console.log(result.aGrammErr); + break; + case "textToTest": + console.log("TEXT TO TEXT RESULTS"); + browser.runtime.sendMessage({sCommand: "textToTest", sResult: result}); + break; + case "fullTests": + console.log("FULL TESTS RESULTS"); + browser.runtime.sendMessage({sCommand: "fullTests", sResult: result}); + break; + case "getOptions": + case "getDefaultOptions": + case "setOptions": + case "setOption": console.log("OPTIONS"); console.log(e.data[1]); break; - case "tokens": + case "getListOfTokens": console.log("TOKENS"); console.log(e.data[1]); let xLxgTab = browser.tabs.create({ url: browser.extension.getURL("panel/lexicographer.html"), }); xLxgTab.then(onCreated, onError); break; break; - case "error": - console.log("ERROR"); - console.log(e.data[1]); - break; default: - console.log("Unknown command: " + e.data[0]); + console.log("Unknown command: " + sActionDone); + console.log(result); } } catch (e) { showError(e); } }; -xGCEWorker.postMessage(["init", {sExtensionPath: browser.extension.getURL("."), sOptions: "", sContext: "Firefox"}]); +xGCEWorker.postMessage({sCommand: "init", dParam: {sExtensionPath: browser.extension.getURL("."), sOptions: "", sContext: "Firefox"}, dInfo: {}}); /* Messages from the extension (not the Worker) */ function handleMessage (oRequest, xSender, sendResponse) { //console.log(xSender); switch(oRequest.sCommand) { case "parse": - xGCEWorker.postMessage(["parse", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]); + xGCEWorker.postMessage({sCommand: "parse", dParam: {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}, dInfo: {}}); break; case "parse_and_spellcheck": - xGCEWorker.postMessage(["parseAndSpellcheck", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]); + xGCEWorker.postMessage({sCommand: "parseAndSpellcheck", dParam: {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}, dInfo: {}}); break; case "get_list_of_tokens": - xGCEWorker.postMessage(["getListOfTokens", {sText: oRequest.sText}]); + xGCEWorker.postMessage({sCommand: "getListOfTokens", dParam: {sText: oRequest.sText}, dInfo: {}}); break; case "text_to_test": - xGCEWorker.postMessage(["textToTest", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]); + xGCEWorker.postMessage({sCommand: "textToTest", dParam: {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}, dInfo: {}}); break; case "fulltests": - xGCEWorker.postMessage(["fullTests"]); + xGCEWorker.postMessage({sCommand: "fullTests", dParam: {}, dInfo: {}}); break; } //sendResponse({response: "response from background script"}); } Index: gc_lang/fr/webext/content_scripts/communicate.js ================================================================== --- gc_lang/fr/webext/content_scripts/communicate.js +++ gc_lang/fr/webext/content_scripts/communicate.js @@ -35,11 +35,11 @@ console.log(xGCEWorker); //xGCEWorker.port.start(); //console.log("Content script [port started]"); - xGCEWorker.port.postMessage(["init", {sExtensionPath: sPath, sOptions: "", sContext: "Firefox"}]); + xGCEWorker.port.postMessage({sCommand: "init", dParam: {sExtensionPath: sPath, sOptions: "", sContext: "Firefox"}, dInfo: {}}); //xGCEWorker.port.postMessage(["parse", {sText: "Vas... J’en aie mare...", sCountry: "FR", bDebug: false, bContext: false}]); //xGCEWorker.port.postMessage(["parseAndSpellcheck", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]); //xGCEWorker.port.postMessage(["getListOfTokens", {sText: oRequest.sText}]); //xGCEWorker.port.postMessage(["textToTest", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]); //xGCEWorker.port.postMessage(["fullTests"]); Index: gc_lang/fr/webext/content_scripts/modify_page.js ================================================================== --- gc_lang/fr/webext/content_scripts/modify_page.js +++ gc_lang/fr/webext/content_scripts/modify_page.js @@ -13,17 +13,18 @@ * Pour effectuer différent action sur la page en cours */ function receivedMessageFromIframe (oEvent) { if (typeof oEvent.data.SharedWorker !== "undefined") { console.log('[Web] received (from iframe (Sharedworker)):', oEvent); - let [sCommand, answer] = oEvent.data.SharedWorker; - console.log(sCommand); - switch (sCommand) { - case "grammar_errors": - console.log(answer.aGrammErr); - for (let oErr of answer.aGrammErr) { + let {sActionDone, result, dInfo, bError} = oEvent.data.SharedWorker; + console.log(sActionDone); + switch (sActionDone) { + case "parse": + console.log(result); + for (let oErr of result) { console.log(oErr); + console.log(text.getReadableError(oErr)); } break; } } } @@ -47,11 +48,11 @@ console.log('[Web] Initialise the worker :s'); console.log('[Web] Domaine ext: '+browser.extension.getURL("")); xFrameContent.postMessage({sPath: browser.extension.getURL(""), sPage: location.origin.trim("/")}, browser.extension.getURL("")); //Un petit test pour débogage ;) console.log('[Web] Test the worker :s'); - xFrameContent.postMessage(["parse", {sText: "Vas... J’en aie mare...", sCountry: "FR", bDebug: false, bContext: false}], browser.extension.getURL("")); + xFrameContent.postMessage({sCommand: "parse", dParam: {sText: "Vas... J’en aie mare...", sCountry: "FR", bDebug: false, bContext: false}, dInfo: {}}, browser.extension.getURL("")); } catch (e) { console.error(e); } } @@ -138,18 +139,18 @@ xToolbar.appendChild(xTFButton); let xLxgButton = document.createElement("div"); xLxgButton.textContent = "Analyser"; xLxgButton.style = sButtonStyle; xLxgButton.onclick = function() { - createLxgPanel(xTextArea); + console.log("Analyser"); }; xToolbar.appendChild(xLxgButton); let xGCButton = document.createElement("div"); xGCButton.textContent = "Corriger"; xGCButton.style = sButtonStyle; xGCButton.onclick = function() { - createGCPanel(xTextArea); + xFrameContent.postMessage({sCommand: "parse", dParam: {sText: xTextArea.value, sCountry: "FR", bDebug: false, bContext: false}, dInfo: {sInfoId: xTextArea.id}}, browser.extension.getURL("")); }; xToolbar.appendChild(xGCButton); return xToolbar; } catch (e) { @@ -184,11 +185,10 @@ console.log("Analyse"); } function createGCPanel (xTextArea) { console.log("Correction grammaticale"); - xFrameContent.postMessage(["parse", {sText: xTextArea.value, sCountry: "FR", bDebug: false, bContext: false}], browser.extension.getURL("")); } function createCloseButton (xParentNode) { let xButton = document.createElement("div"); xButton.style = "float: right; width: 20px; padding: 5px 10px; color: hsl(210, 0%, 100%); text-align: center;" Index: gc_lang/fr/webext/gce_sharedworker.js ================================================================== --- gc_lang/fr/webext/gce_sharedworker.js +++ gc_lang/fr/webext/gce_sharedworker.js @@ -59,131 +59,162 @@ https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent */ let xPort = null; -function showError (e) { +function createResponse (sActionDone, result, dInfo, bError=false) { + return { + "sActionDone": sActionDone, + "result": result, // can be of any type + "dInfo": dInfo, + "bError": bError + }; +} + +function createErrorResult (e, sDescr="no description") { + return { + "sType": "error", + "sDescription": sDescr, + "sMessage": e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message + }; +} + +function showData (e) { for (let sParam in e) { console.log(sParam); console.log(e[sParam]); } } -onconnect = function(e) { - console.log("START CONNECTION"); +onconnect = function (e) { + console.log("[Sharedworker] START CONNECTION"); xPort = e.ports[0]; xPort.onmessage = function (e) { console.log("[Sharedworker] ONMESSAGE"); - console.log(e); - console.log(e.data[0]); - let oParam = e.data[1]; - switch (e.data[0]) { + let {sCommand, dParam, dInfo} = e.data; + console.log(e.data); + switch (sCommand) { case "init": - loadGrammarChecker(oParam.sExtensionPath, oParam.sOptions, oParam.sContext); + init(dParam.sExtensionPath, dParam.sOptions, dParam.sContext, dInfo); break; case "parse": - parse(oParam.sText, oParam.sCountry, oParam.bDebug, oParam.bContext); + parse(dParam.sText, dParam.sCountry, dParam.bDebug, dParam.bContext, dInfo); break; case "parseAndSpellcheck": - parseAndSpellcheck(oParam.sText, oParam.sCountry, oParam.bDebug, oParam.bContext); + parseAndSpellcheck(dParam.sText, dParam.sCountry, dParam.bDebug, dParam.bContext, dInfo); break; case "getOptions": - getOptions(); + getOptions(dInfo); break; case "getDefaultOptions": - getDefaultOptions(); + getDefaultOptions(dInfo); break; case "setOptions": - setOptions(oParam.sOptions); + setOptions(dParam.sOptions, dInfo); break; case "setOption": - setOption(oParam.sOptName, oParam.bValue); + setOption(dParam.sOptName, dParam.bValue, dInfo); break; case "resetOptions": - resetOptions(); + resetOptions(dInfo); break; case "textToTest": - textToTest(oParam.sText, oParam.sCountry, oParam.bDebug, oParam.bContext); + textToTest(dParam.sText, dParam.sCountry, dParam.bDebug, dParam.bContext, dInfo); break; case "fullTests": - fullTests(); + fullTests('{"nbsp":true, "esp":true, "unit":true, "num":true}', dInfo); break; case "getListOfTokens": - getListOfTokens(oParam.sText); + getListOfTokens(dParam.sText, dInfo); break; default: - console.log("Unknown command: " + showError(e.data[0])); + console.log("Unknown command: " + sCommand); + showData(e.data); } } //xPort.start(); } +let bInitDone = false; let oDict = null; let oTokenizer = null; let oLxg = null; let oTest = null; -function loadGrammarChecker (sExtensionPath, sGCOptions="", sContext="JavaScript") { +function init (sExtensionPath, sGCOptions="", sContext="JavaScript", dInfo={}) { try { - console.log("Loading… Extension path: " + sExtensionPath); - conj.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/conj_data.json")); - phonet.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/phonet_data.json")); - mfsp.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/mfsp_data.json")); - console.log("Modules have been initialized…"); - gc_engine.load(sContext, sExtensionPath+"grammalecte/_dictionaries"); - oDict = gc_engine.getDictionary(); - oTest = new TestGrammarChecking(gc_engine, sExtensionPath+"/grammalecte/fr/tests_data.json"); - oLxg = new Lexicographe(oDict); - if (sGCOptions !== "") { - gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); - } - oTokenizer = new Tokenizer("fr"); - //tests(); - // we always retrieve options from the gc_engine, for setOptions filters obsolete options - xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); + if (!bInitDone) { + console.log("[Sharedworker] Loading… Extension path: " + sExtensionPath); + conj.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/conj_data.json")); + phonet.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/phonet_data.json")); + mfsp.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/mfsp_data.json")); + console.log("[Sharedworker] Modules have been initialized…"); + gc_engine.load(sContext, sExtensionPath+"grammalecte/_dictionaries"); + oDict = gc_engine.getDictionary(); + oTest = new TestGrammarChecking(gc_engine, sExtensionPath+"/grammalecte/fr/tests_data.json"); + oLxg = new Lexicographe(oDict); + if (sGCOptions !== "") { + gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); + } + oTokenizer = new Tokenizer("fr"); + //tests(); + // we always retrieve options from the gc_engine, for setOptions filters obsolete options + //xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); + bInitDone = true; + } else { + console.log("[Sharedworker] Already initialized…") + } + xPort.postMessage(createResponse("init", gc_engine.getOptions().gl_toString(), dInfo)); } catch (e) { - console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); - xPort.postMessage(["error", e.message]); + helpers.logerror(e); + xPort.postMessage(createResponse("init", createErrorResult(e, "init failed"), dInfo, true)); } } -function parse (sText, sCountry, bDebug, bContext) { +function parse (sText, sCountry, bDebug, bContext, dInfo={}) { let aGrammErr = gc_engine.parse(sText, sCountry, bDebug, bContext); - xPort.postMessage(["grammar_errors", {aGrammErr: aGrammErr}]); + //xPort.postMessage(["grammar_errors", {aGrammErr: aGrammErr}]); + xPort.postMessage({sActionDone: "parse", result: aGrammErr, dInfo: dInfo}); } -function parseAndSpellcheck (sText, sCountry, bDebug, bContext) { +function parseAndSpellcheck (sText, sCountry, bDebug, bContext, dInfo={}) { let aGrammErr = gc_engine.parse(sText, sCountry, bDebug, bContext); let aSpellErr = oTokenizer.getSpellingErrors(sText, oDict); - xPort.postMessage(["spelling_and_grammar_errors", {aGrammErr: aGrammErr, aSpellErr: aSpellErr}]); -} - -function getOptions () { - xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); -} - -function getDefaultOptions () { - xPort.postMessage(["options", gc_engine.getDefaultOptions().gl_toString()]); -} - -function setOptions (sGCOptions) { - gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); - xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); -} - -function setOption (sOptName, bValue) { - gc_engine.setOptions(new Map([ [sOptName, bValue] ])); - xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); -} - -function resetOptions () { - gc_engine.resetOptions(); - xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); + //xPort.postMessage(["spelling_and_grammar_errors", {aGrammErr: aGrammErr, aSpellErr: aSpellErr}]); + xPort.postMessage(createResponse("parseAndSpellcheck", {aGrammErr: aGrammErr, aSpellErr: aSpellErr}, dInfo)); +} + +function getOptions (dInfo={}) { + //xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); + xPort.postMessage(createResponse("getOptions", gc_engine.getOptions().gl_toString(), dInfo)); +} + +function getDefaultOptions (dInfo={}) { + //xPort.postMessage(["options", gc_engine.getDefaultOptions().gl_toString()]); + xPort.postMessage(createResponse("getDefaultOptions", gc_engine.getDefaultOptions().gl_toString(), dInfo)); +} + +function setOptions (sGCOptions, dInfo={}) { + gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); + //xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); + xPort.postMessage(createResponse("setOptions", gc_engine.getOptions().gl_toString(), dInfo)); +} + +function setOption (sOptName, bValue, dInfo={}) { + gc_engine.setOptions(new Map([ [sOptName, bValue] ])); + //xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); + xPort.postMessage(createResponse("setOption", gc_engine.getOptions().gl_toString(), dInfo)); +} + +function resetOptions (dInfo={}) { + gc_engine.resetOptions(); + //xPort.postMessage(["options", gc_engine.getOptions().gl_toString()]); + xPort.postMessage(createResponse("resetOptions", gc_engine.getOptions().gl_toString(), dInfo)); } function tests () { console.log(conj.getConj("devenir", ":E", ":2s")); console.log(mfsp.getMasForm("emmerdeuse", true)); @@ -193,26 +224,27 @@ for (let oErr of aRes) { console.log(text.getReadableError(oErr)); } } -function textToTest (sText, sCountry, bDebug, bContext) { +function textToTest (sText, sCountry, bDebug, bContext, dInfo={}) { if (!gc_engine || !oDict) { - xPort.postMessage(["error", "# Error: grammar checker or dictionary not loaded."]); + xPort.postMessage(createResponse("textToTest", "# Grammar checker or dictionary not loaded.", dInfo)); return; } let aGrammErr = gc_engine.parse(sText, sCountry, bDebug, bContext); let sMsg = ""; for (let oErr of aGrammErr) { sMsg += text.getReadableError(oErr) + "\n"; } - xPort.postMessage(["text_to_test_result", sMsg]); + //xPort.postMessage(["text_to_test_result", sMsg]); + xPort.postMessage(createResponse("textToTest", sMsg, dInfo)); } -function fullTests (sGCOptions='{"nbsp":true, "esp":true, "unit":true, "num":true}') { +function fullTests (sGCOptions="", dInfo={}) { if (!gc_engine || !oDict) { - xPort.postMessage(["error", "# Error: grammar checker or dictionary not loaded."]); + xPort.postMessage(createResponse("fullTests", "# Grammar checker or dictionary not loaded.", dInfo)); return; } let dMemoOptions = gc_engine.getOptions(); if (sGCOptions) { gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); @@ -221,28 +253,30 @@ for (let sRes of oTest.testParse()) { sMsg += sRes + "\n"; console.log(sRes); } gc_engine.setOptions(dMemoOptions); - xPort.postMessage(["fulltests_result", sMsg]); + //xPort.postMessage(["fulltests_result", sMsg]); + xPort.postMessage(createResponse("fullTests", sMsg, dInfo)); } // Lexicographer -function getListOfTokens (sText) { +function getListOfTokens (sText, dInfo={}) { try { let aElem = []; let aRes = null; for (let oToken of oTokenizer.genTokens(sText)) { aRes = oLxg.getInfoForToken(oToken); if (aRes) { aElem.push(aRes); } } - xPort.postMessage(["tokens", aElem]); + //xPort.postMessage(["tokens", aElem]); + xPort.postMessage(createResponse("getListOfTokens", aElem, dInfo)); } catch (e) { helpers.logerror(e); - xPort.postMessage(["error", e.message]); + xPort.postMessage(createResponse("getListOfTokens", createErrorResult(e, "no tokens"), dInfo, true)); } } Index: gc_lang/fr/webext/manifest.json ================================================================== --- gc_lang/fr/webext/manifest.json +++ gc_lang/fr/webext/manifest.json @@ -34,11 +34,11 @@ ] }, "content_scripts": [ { "matches": ["*://*/*"], - "js": ["content_scripts/modify_page.js"] + "js": ["content_scripts/modify_page.js", "grammalecte/text.js"] } ], "web_accessible_resources": [ "grammalecte/_dictionaries/French.json", "grammalecte/fr/conj_data.json", Index: gc_lang/fr/webext/panel/main.js ================================================================== --- gc_lang/fr/webext/panel/main.js +++ gc_lang/fr/webext/panel/main.js @@ -82,21 +82,27 @@ /* Messages received */ function handleMessage (oMessage, xSender, sendResponse) { console.log(xSender); + console.log(oMessage.sCommand); switch(oMessage.sCommand) { case "show_tokens": console.log("show tokens"); addParagraphOfTokens(oMessage.oResult); break; - case "text_to_test_result": + case "textToTest": + console.log(oMessage.sResult); showTestResult(oMessage.sResult); break; - case "fulltests_result": + case "fullTests": + console.log(oMessage.sResult); showTestResult(oMessage.sResult); break; + default: + console.log("Unknow command: " + oMessage.sCommand); + } sendResponse({sCommand: "none", sResult: "done"}); } browser.runtime.onMessage.addListener(handleMessage);