Index: gc_lang/fr/webext/background.js ================================================================== --- gc_lang/fr/webext/background.js +++ gc_lang/fr/webext/background.js @@ -2,12 +2,57 @@ "use strict"; let xGCEWorker = new Worker("gce_worker.js"); -xGCEWorker.postMessage(["init", browser.extension.getURL(".")]); +xGCEWorker.onmessage = function (e) { + console.log("Received from Worker:"); + switch (e.data[0]) { + case "grammar_errors": + console.log("GRAMMAR ERRORS"); + console.log(e.data[1].aGrammErr); + break; + case "spelling_and_grammar_errors": + console.log("SPELLING AND GRAMMAR ERRORS"); + console.log(e.data[1].aSpellErr); + console.log(e.data[1].aGrammErr); + break; + case "tests_results": + console.log("TESTS RESULTS"); + console.log(e.data[1]); + break; + case "options": + console.log("OPTIONS"); + console.log(e.data[1]); + break; + case "tokens": + console.log("TOKENS"); + console.log(e.data[1]); + break; + case "error": + console.log("ERROR"); + console.log(e.data[1]); + break; + default: + console.log("Unknown command: " + e.data[0]); + } +}; + +xGCEWorker.postMessage(["init", {sExtensionPath: browser.extension.getURL("."), sOptions: "", sContext: "Firefox"}]); + +xGCEWorker.postMessage(["parse", {sText: "J’en aie mare...", sCountry: "FR", bDebug: false, bContext: false}]); + +xGCEWorker.postMessage(["parseAndSpellcheck", {sText: "C’est terribles, ils va tout perdrre.", sCountry: "FR", bDebug: false, bContext: false}]); + +xGCEWorker.postMessage(["getListOfTokens", {sText: "J’en ai assez de ces âneries ! Merci bien. Ça suffira."}]); + +xGCEWorker.postMessage(["fullTests"]); + +/* + Messages from the extension (not the Worker) +*/ function handleMessage (oRequest, xSender, sendResponse) { console.log(`[background] received: ${oRequest.content}`); sendResponse({response: "response from background script"}); } Index: gc_lang/fr/webext/gce_worker.js ================================================================== --- gc_lang/fr/webext/gce_worker.js +++ gc_lang/fr/webext/gce_worker.js @@ -11,11 +11,11 @@ In Worker, we have importScripts() which imports everything in this scope. In order to use the same base of code with XUL-addon for Thunderbird and SDK-addon for Firefox, all modules have been “objectified”. And while they are still imported via “require” in the previous extensions, they are loaded as background scripts in WebExtension sharing - the same memory space (it seems)… + the same memory space… When JavaScript become a modern language, “deobjectify” the modules… ATM, import/export are not available by default: — Chrome 60 – behind the Experimental Web Platform flag in chrome:flags. @@ -24,13 +24,15 @@ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export */ +"use strict"; + console.log("GC Engine Worker [start]"); -console.log(self); +//console.log(self); importScripts("grammalecte/helpers.js"); importScripts("grammalecte/str_transform.js"); importScripts("grammalecte/ibdawg.js"); importScripts("grammalecte/text.js"); @@ -42,135 +44,171 @@ importScripts("grammalecte/fr/gc_options.js"); importScripts("grammalecte/fr/gc_rules.js"); importScripts("grammalecte/fr/gc_engine.js"); importScripts("grammalecte/fr/lexicographe.js"); importScripts("grammalecte/tests.js"); +/* + Warning. + 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”. +*/ /* - Warning. - Initialization can’t be completed at startup, for we need the path of the path of the extension - to load data stored in JSON files. - This path is retrieved in background.js and passed with the following event. + Message Event Object + https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent */ onmessage = function (e) { - if (e.data[0] == "init") { - loadGrammarChecker(e.data[1]); + let oParam = e.data[1]; + switch (e.data[0]) { + case "init": + loadGrammarChecker(oParam.sExtensionPath, oParam.sOptions, oParam.sContext); + break; + case "parse": + parse(oParam.sText, oParam.sCountry, oParam.bDebug, oParam.bContext); + break; + case "parseAndSpellcheck": + parseAndSpellcheck(oParam.sText, oParam.sCountry, oParam.bDebug, oParam.bContext); + break; + case "getOptions": + getOptions(); + break; + case "getDefaultOptions": + getDefaultOptions(); + break; + case "setOptions": + setOptions(oParam.sOptions); + break; + case "setOption": + setOption(oParam.sOptName, oParam.bValue); + break; + case "resetOptions": + resetOptions(); + break; + case "fullTests": + fullTests(); + break; + case "getListOfTokens": + getListOfTokens(oParam.sText); + break; + default: + console.log("Unknown command: " + e.data[0]); } } + let oDict = null; let oTokenizer = null; let oLxg = null; let oTest = null; -function loadGrammarChecker (sCGDataPath, sGCOptions="", sContext="JavaScript") { + +function loadGrammarChecker (sExtensionPath, sGCOptions="", sContext="JavaScript") { try { - console.log("Loading… path: " + sCGDataPath); - conj.init(helpers.loadFile(sCGDataPath + "/grammalecte/fr/conj_data.json")); - phonet.init(helpers.loadFile(sCGDataPath + "/grammalecte/fr/phonet_data.json")); - mfsp.init(helpers.loadFile(sCGDataPath + "/grammalecte/fr/mfsp_data.json")); - oTest = new TestGrammarChecking(gc_engine, sCGDataPath+"/grammalecte/fr/tests_data.json"); - console.log("JSON loaded"); - - oTokenizer = new Tokenizer("fr"); - console.log("Tokenizer loaded"); - - gc_engine.load(sContext, sCGDataPath+"grammalecte/_dictionaries"); + 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))); } - helpers.echo("START"); - - helpers.echo(conj.getConj("devenir", ":E", ":2s")); - - helpers.echo(mfsp.getMasForm("emmerdeuse", true)); - helpers.echo(mfsp.getMasForm("pointilleuse", false)); - - helpers.echo(phonet.getSimil("est")); - - let aRes = gc_engine.parse("Je suit..."); - for (let oErr of aRes) { - helpers.echo(text.getReadableError(oErr)); - } - fullTests(); + oTokenizer = new Tokenizer("fr"); + tests(); // we always retrieve options from the gc_engine, for setOptions filters obsolete options - //return gce.getOptions().gl_toString(); + postMessage(["options", gc_engine.getOptions().gl_toString()]); } catch (e) { - console.error("\n" + e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); + console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); + postMessage(["error", e.message]); } - } -function parse (sText, sLang, bDebug, bContext) { - let aGrammErr = gc_engine.parse(sText, sLang, bDebug, bContext); - return JSON.stringify(aGrammErr); +function parse (sText, sCountry, bDebug, bContext) { + let aGrammErr = gc_engine.parse(sText, sCountry, bDebug, bContext); + postMessage(["grammar_errors", {aGrammErr: JSON.stringify(aGrammErr)}]); } -function parseAndSpellcheck (sText, sLang, bDebug, bContext) { - let aGrammErr = gc_engine.parse(sText, sLang, bDebug, bContext); +function parseAndSpellcheck (sText, sCountry, bDebug, bContext) { + let aGrammErr = gc_engine.parse(sText, sCountry, bDebug, bContext); let aSpellErr = oTokenizer.getSpellingErrors(sText, oDict); - return JSON.stringify({ aGrammErr: aGrammErr, aSpellErr: aSpellErr }); + postMessage(["spelling_and_grammar_errors", {aGrammErr: JSON.stringify(aGrammErr), aSpellErr: JSON.stringify(aSpellErr)}]); } function getOptions () { - return gc_engine.getOptions().gl_toString(); + postMessage(["options", gc_engine.getOptions().gl_toString()]); } function getDefaultOptions () { - return gc_engine.getDefaultOptions().gl_toString(); + postMessage(["options", gc_engine.getDefaultOptions().gl_toString()]); } function setOptions (sGCOptions) { gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); - return gc_engine.getOptions().gl_toString(); + postMessage(["options", gc_engine.getOptions().gl_toString()]); } function setOption (sOptName, bValue) { gc_engine.setOptions(new Map([ [sOptName, bValue] ])); - return gc_engine.getOptions().gl_toString(); + postMessage(["options", gc_engine.getOptions().gl_toString()]); } function resetOptions () { gc_engine.resetOptions(); - return gc_engine.getOptions().gl_toString(); + postMessage(["options", gc_engine.getOptions().gl_toString()]); +} + +function tests () { + console.log(conj.getConj("devenir", ":E", ":2s")); + console.log(mfsp.getMasForm("emmerdeuse", true)); + console.log(mfsp.getMasForm("pointilleuse", false)); + console.log(phonet.getSimil("est")); + let aRes = gc_engine.parse("Je suit..."); + for (let oErr of aRes) { + console.log(text.getReadableError(oErr)); + } } function fullTests (sGCOptions='{"nbsp":true, "esp":true, "unit":true, "num":true}') { if (!gc_engine || !oDict) { - return "# Error: grammar checker or dictionary not loaded." + postMessage(["error", "# Error: grammar checker or dictionary not loaded."]); + return; } let dMemoOptions = gc_engine.getOptions(); if (sGCOptions) { gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); } - + let sMsg = ""; for (let sRes of oTest.testParse()) { - helpers.echo(sRes+"\n"); + sMsg += sRes + "\n"; + console.log(sRes); } gc_engine.setOptions(dMemoOptions); + postMessage(["tests_results", sMsg]); } // Lexicographer -function getListOfElements (sText) { +function getListOfTokens (sText) { try { let aElem = []; let aRes = null; for (let oToken of oTokenizer.genTokens(sText)) { aRes = oLxg.getInfoForToken(oToken); if (aRes) { aElem.push(aRes); } } - return JSON.stringify(aElem); + postMessage(["tokens", JSON.stringify(aElem)]); } catch (e) { helpers.logerror(e); + postMessage(["error", e.message]); } - return JSON.stringify([]); + } Index: gc_lang/fr/xpi/gce_worker.js ================================================================== --- gc_lang/fr/xpi/gce_worker.js +++ gc_lang/fr/xpi/gce_worker.js @@ -77,17 +77,17 @@ worker.log("# Error: " + e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); } } } -function parse (sText, sLang, bDebug, bContext) { - let aGrammErr = gce.parse(sText, sLang, bDebug, bContext); +function parse (sText, sCountry, bDebug, bContext) { + let aGrammErr = gce.parse(sText, sCountry, bDebug, bContext); return JSON.stringify(aGrammErr); } -function parseAndSpellcheck (sText, sLang, bDebug, bContext) { - let aGrammErr = gce.parse(sText, sLang, bDebug, bContext); +function parseAndSpellcheck (sText, sCountry, bDebug, bContext) { + let aGrammErr = gce.parse(sText, sCountry, bDebug, bContext); let aSpellErr = oTokenizer.getSpellingErrors(sText, oDict); return JSON.stringify({ aGrammErr: aGrammErr, aSpellErr: aSpellErr }); } function getOptions () {