Index: gc_lang/fr/webext/background.js ================================================================== --- gc_lang/fr/webext/background.js +++ gc_lang/fr/webext/background.js @@ -12,77 +12,82 @@ let xGCEWorker = new Worker("gce_worker.js"); 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"); + break; + 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: "text_to_test_result", sResult: result}); + break; + case "fullTests": + console.log("FULL TESTS RESULTS"); + browser.runtime.sendMessage({sCommand: "fulltests_result", 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"}); } @@ -114,15 +119,15 @@ title: "Conjugueur [onglet]", contexts: ["all"] }); function onCreated(windowInfo) { - console.log(`Created window: ${windowInfo.id}`); + console.log(`Created window: ${windowInfo.id}`); } function onError(error) { - console.log(`Error: ${error}`); + console.log(`Error: ${error}`); } let xConjWindow = null; let xConjTab = null; @@ -161,14 +166,14 @@ } }); async function newwin () { + // test for popup window-like, which doesn’t close when losing the focus console.log("Async on"); const getActive = browser.tabs.query({ currentWindow: true, active: true, }); const xWindowInfo = await browser.windows.getLastFocused(); - // the pop-up will not resize itself as the panel would, so the dimensions can be passed as query params 'w' and 'h' const width = 710, height = 980; // the maximum size for panels is somewhere around 700x800. Firefox needs some additional pixels: 14x42 for FF54 on Win 10 with dpi 1.25 const left = Math.round(xWindowInfo.left + xWindowInfo.width - width - 25); const top = Math.round(xWindowInfo.top + 74); // the actual frame height of the main window varies, but 74px should place the pop-up at the bottom if the button const xWin = await browser.windows.create({ type: 'panel', url: browser.extension.getURL("panel/conjugueur.html"), top: top, left: left, width: width, height: height, @@ -176,79 +181,5 @@ browser.windows.update(xWin.id, { top:top, left:left, }); // firefox currently ignores top and left in .create(), so move it here console.log("Async done"); } //newwin(); - - -/* - Worker (separate thread to avoid freezing Firefox) -*/ -/* -let xGCESharedWorker = new SharedWorker("gce_sharedworker.js"); - -xGCESharedWorker.port.onmessage = function (e) { - // https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent - try { - switch (e.data[0]) { - case "grammar_errors": - console.log("GRAMMAR ERRORS (SHARE)"); - console.log(e.data[1].aGrammErr); - //browser.runtime.sendMessage({sCommand: "grammar_errors", aGrammErr: e.data[1].aGrammErr}); - break; - case "spelling_and_grammar_errors": - console.log("SPELLING AND GRAMMAR ERRORS (SHARE)"); - console.log(e.data[1].aSpellErr); - console.log(e.data[1].aGrammErr); - break; - case "text_to_test_result": - console.log("TESTS RESULTS (SHARE)"); - console.log(e.data[1]); - break; - case "fulltests_result": - console.log("TESTS RESULTS (SHARE)"); - console.log(e.data[1]); - break; - case "options": - console.log("OPTIONS (SHARE)"); - console.log(e.data[1]); - break; - case "tokens": - console.log("TOKENS (SHARE)"); - console.log(e.data[1]); - let xLxgTab = browser.tabs.create({ - url: browser.extension.getURL("panel/lexicographer.html"), - }); - xLxgTab.then(onCreated, onError); - break; - case "error": - console.log("ERROR (SHARE)"); - console.log(e.data[1]); - break; - default: - console.log("Unknown command (SHARE): " + e.data[0]); - } - } - catch (e) { - showError(e); - } -}; - -console.log("Content script [worker]"); -console.log(xGCESharedWorker); - - -//xGCESharedWorker.port.start(); -//console.log("Content script [port started]"); - -xGCESharedWorker.port.postMessage(["init", {sExtensionPath: browser.extension.getURL("."), sOptions: "", sContext: "Firefox"}]); - -console.log("Content script [worker initialzed]"); - -xGCESharedWorker.port.postMessage(["parse", {sText: "Vas... J’en aie mare...", sCountry: "FR", bDebug: false, bContext: false}]); -//xGCESharedWorker.port.postMessage(["parseAndSpellcheck", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]); -//xGCESharedWorker.port.postMessage(["getListOfTokens", {sText: oRequest.sText}]); -//xGCESharedWorker.port.postMessage(["textToTest", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]); -//xGCESharedWorker.port.postMessage(["fullTests"]); - - -*/ Index: gc_lang/fr/webext/gce_worker.js ================================================================== --- gc_lang/fr/webext/gce_worker.js +++ gc_lang/fr/webext/gce_worker.js @@ -27,11 +27,11 @@ */ "use strict"; -console.log("GC Engine Worker [start]"); +console.log("[Worker] GC Engine Worker [start]"); //console.log(self); importScripts("grammalecte/helpers.js"); importScripts("grammalecte/str_transform.js"); importScripts("grammalecte/ibdawg.js"); @@ -51,120 +51,156 @@ Initialization can’t be completed at startup of the worker, for we need the path of the extension to load data stored in JSON files. This path is retrieved in background.js and passed with the event “init”. */ + +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]); + } +} + /* Message Event Object https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent */ onmessage = function (e) { - let oParam = e.data[1]; - switch (e.data[0]) { + console.log("[Worker] message received"); + let {sCommand, dParam, dInfo} = 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: " + e.data[0]); + console.log("Unknown command: " + sCommand); + showData(e.data); } } + +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 - postMessage(["options", gc_engine.getOptions().gl_toString()]); + if (!bInitDone) { + console.log("[Worker] 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("[Worker] 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(); + bInitDone = true; + } else { + console.log("[Worker] Already initialized…") + } + // we always retrieve options from the gc_engine, for setOptions filters obsolete options + postMessage(createResponse("init", gc_engine.getOptions().gl_toString(), dInfo)); } catch (e) { - console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); - postMessage(["error", e.message]); + helpers.logerror(e); + 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); - postMessage(["grammar_errors", {aGrammErr: aGrammErr}]); + 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); - postMessage(["spelling_and_grammar_errors", {aGrammErr: aGrammErr, aSpellErr: aSpellErr}]); -} - -function getOptions () { - postMessage(["options", gc_engine.getOptions().gl_toString()]); -} - -function getDefaultOptions () { - postMessage(["options", gc_engine.getDefaultOptions().gl_toString()]); -} - -function setOptions (sGCOptions) { - gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); - postMessage(["options", gc_engine.getOptions().gl_toString()]); -} - -function setOption (sOptName, bValue) { - gc_engine.setOptions(new Map([ [sOptName, bValue] ])); - postMessage(["options", gc_engine.getOptions().gl_toString()]); -} - -function resetOptions () { - gc_engine.resetOptions(); - postMessage(["options", gc_engine.getOptions().gl_toString()]); + postMessage(createResponse("parseAndSpellcheck", {aGrammErr: aGrammErr, aSpellErr: aSpellErr}, dInfo)); +} + +function getOptions (dInfo={}) { + postMessage(createResponse("getOptions", gc_engine.getOptions().gl_toString(), dInfo)); +} + +function getDefaultOptions (dInfo={}) { + postMessage(createResponse("getDefaultOptions", gc_engine.getDefaultOptions().gl_toString(), dInfo)); +} + +function setOptions (sGCOptions, dInfo={}) { + gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); + postMessage(createResponse("setOptions", gc_engine.getOptions().gl_toString(), dInfo)); +} + +function setOption (sOptName, bValue, dInfo={}) { + gc_engine.setOptions(new Map([ [sOptName, bValue] ])); + postMessage(createResponse("setOption", gc_engine.getOptions().gl_toString(), dInfo)); +} + +function resetOptions (dInfo={}) { + gc_engine.resetOptions(); + postMessage(createResponse("resetOptions", gc_engine.getOptions().gl_toString(), dInfo)); } function tests () { console.log(conj.getConj("devenir", ":E", ":2s")); console.log(mfsp.getMasForm("emmerdeuse", true)); @@ -174,26 +210,26 @@ 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) { - postMessage(["error", "# Error: grammar checker or dictionary not loaded."]); + 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"; } - postMessage(["text_to_test_result", sMsg]); + postMessage(createResponse("textToTest", sMsg, dInfo)); } -function fullTests (sGCOptions='{"nbsp":true, "esp":true, "unit":true, "num":true}') { +function fullTests (sGCOptions="", dInfo={}) { if (!gc_engine || !oDict) { - postMessage(["error", "# Error: grammar checker or dictionary not loaded."]); + 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))); @@ -202,28 +238,29 @@ for (let sRes of oTest.testParse()) { sMsg += sRes + "\n"; console.log(sRes); } gc_engine.setOptions(dMemoOptions); - postMessage(["fulltests_result", sMsg]); + 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); } } - postMessage(["tokens", aElem]); + postMessage(createResponse("getListOfTokens", aElem, dInfo)); } catch (e) { helpers.logerror(e); - postMessage(["error", e.message]); + postMessage(createResponse("getListOfTokens", createErrorResult(e, "no tokens"), dInfo, true)); } } Index: gc_lang/fr/webext/panel/main.html ================================================================== --- gc_lang/fr/webext/panel/main.html +++ gc_lang/fr/webext/panel/main.html @@ -15,11 +15,10 @@ @@ -38,15 +37,10 @@

GRAMMALECTE

-
-

CORRECTEUR GRAMMATICAL

-
-
-

OPTIONS GRAMMATICALES