Index: gc_core/js/ibdawg.js ================================================================== --- gc_core/js/ibdawg.js +++ gc_core/js/ibdawg.js @@ -16,13 +16,13 @@ class IBDAWG { // INDEXABLE BINARY DIRECT ACYCLIC WORD GRAPH - constructor (sDicName) { + constructor (sDicName, sPath="") { try { - let sURL = (typeof(browser) !== 'undefined') ? browser.extension.getURL("grammalecte/_dictionaries/"+sDicName) : "resource://grammalecte/_dictionaries/"+sDicName; + let sURL = (sPath !== "") ? sPath + "/" + sDicName : "resource://grammalecte/_dictionaries/"+sDicName; const dict = JSON.parse(helpers.loadFile(sURL)); Object.assign(this, dict); } catch (e) { throw Error("# Error. File not found or not loadable.\n" + e.message + "\n"); Index: gc_core/js/lang_core/gc_engine.js ================================================================== --- gc_core/js/lang_core/gc_engine.js +++ gc_core/js/lang_core/gc_engine.js @@ -310,17 +310,17 @@ return gc_rules.lParagraphRules; }, //// Initialization - load: function (sContext="JavaScript") { + load: function (sContext="JavaScript", sPath="") { try { if (typeof(require) !== 'undefined') { var ibdawg = require("resource://grammalecte/ibdawg.js"); _oDict = new ibdawg.IBDAWG("${dic_name}.json"); } else { - _oDict = new IBDAWG("${dic_name}.json"); + _oDict = new IBDAWG("${dic_name}.json", sPath); } _sAppContext = sContext; _dOptions = gc_options.getOptions(sContext).gl_shallowCopy(); // duplication necessary, to be able to reset to default } catch (e) { Index: gc_core/js/tests.js ================================================================== --- gc_core/js/tests.js +++ gc_core/js/tests.js @@ -8,18 +8,19 @@ } class TestGrammarChecking { - constructor (gce) { + constructor (gce, spfTests="") { this.gce = gce; + this.spfTests = spfTests this._aRuleTested = new Set(); }; * testParse (bDebug=false) { const t0 = Date.now(); - let sURL = (typeof(browser) !== 'undefined') ? browser.extension.getURL("grammalecte/"+this.gce.lang+"/tests_data.json") : "resource://grammalecte/"+this.gce.lang+"/tests_data.json"; + let sURL = (this.spfTests !== "") ? this.spfTests : "resource://grammalecte/"+this.gce.lang+"/tests_data.json"; const aData = JSON.parse(helpers.loadFile(sURL)).aData; let nInvalid = 0 let nTotal = 0 let sErrorText; let sSugg; Index: gc_lang/fr/modules-js/conj.js ================================================================== --- gc_lang/fr/modules-js/conj.js +++ gc_lang/fr/modules-js/conj.js @@ -477,24 +477,24 @@ } // Initialization if (typeof(browser) !== 'undefined') { - // WebExtension + // WebExtension (but not in Worker) conj.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/conj_data.json"))); } else if (typeof(require) !== 'undefined') { // Add-on SDK and Thunderbird let helpers = require("resource://grammalecte/helpers.js"); conj.init(helpers.loadFile("resource://grammalecte/fr/conj_data.json")); -} else if (typeof(self) !== 'undefined') { +} else if (typeof(self) !== 'undefined' && typeof(self.port) !== 'undefined' && typeof(self.port.on) === "undefined") { // used within Firefox content script (conjugation panel). // can’t load JSON from here, so we do it in ui.js and send it here. self.port.on("provideConjData", function (sJSONData) { conj.init(sJSONData); - }); + }); } else { - console.log("Error: Impossible d’initialiser le module conj"); + console.log("Module conj non initialisé"); } if (typeof(exports) !== 'undefined') { exports._lVtyp = conj._lVtyp; Index: gc_lang/fr/modules-js/mfsp.js ================================================================== --- gc_lang/fr/modules-js/mfsp.js +++ gc_lang/fr/modules-js/mfsp.js @@ -11,13 +11,13 @@ var mfsp = { // list of affix codes _lTagMiscPlur: [], _lTagMasForm: [], // dictionary of words with uncommon plurals (-x, -ux, english, latin and italian plurals) and tags to generate them - _dMiscPlur: {}, + _dMiscPlur: new Map(), // dictionary of feminine forms and tags to generate masculine forms (singular and plural) - _dMasForm: {}, + _dMasForm: new Map(), init: function (sJSONData) { try { let _oData = JSON.parse(sJSONData); this._lTagMiscPlur = _oData.lTagMiscPlur; @@ -97,11 +97,11 @@ mfsp.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/mfsp_data.json"))); } else if (typeof(require) !== 'undefined') { // Add-on SDK and Thunderbird mfsp.init(helpers.loadFile("resource://grammalecte/fr/mfsp_data.json")); } else { - console.log("Error: Impossible d’initialiser le module mfsp"); + console.log("Module mfsp non initialisé"); } if (typeof(exports) !== 'undefined') { exports._lTagMiscPlur = mfsp._lTagMiscPlur; Index: gc_lang/fr/modules-js/phonet.js ================================================================== --- gc_lang/fr/modules-js/phonet.js +++ gc_lang/fr/modules-js/phonet.js @@ -4,13 +4,13 @@ var helpers = require("resource://grammalecte/helpers.js"); } var phonet = { - _dWord: {}, + _dWord: new Map(), _lSet: [], - _dMorph: {}, + _dMorph: new Map(), init: function (sJSONData) { try { let _oData = JSON.parse(sJSONData); this._dWord = helpers.objectToMap(_oData.dWord); @@ -86,11 +86,11 @@ phonet.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/phonet_data.json"))); } else if (typeof(require) !== 'undefined') { // Add-on SDK and Thunderbird phonet.init(helpers.loadFile("resource://grammalecte/fr/phonet_data.json")); } else { - console.log("Error: Impossible d’initialiser le module phonet"); + console.log("Module phonet non initialisé"); } if (typeof(exports) !== 'undefined') { exports._dWord = phonet._dWord; Index: gc_lang/fr/webext/background.js ================================================================== --- gc_lang/fr/webext/background.js +++ gc_lang/fr/webext/background.js @@ -1,10 +1,12 @@ // Background "use strict"; let xGCEWorker = new Worker("gce_worker.js"); + +xGCEWorker.postMessage(["init", browser.extension.getURL(".")]); 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 @@ -2,11 +2,11 @@ WORKER: https://developer.mozilla.org/en-US/docs/Web/API/Worker https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope - JavaScript still sucks. + JavaScript sucks. No module available in WebExtension at the moment! :( No require, no import/export. In Worker, we have importScripts() which imports everything in this scope. @@ -40,42 +40,71 @@ importScripts("grammalecte/fr/phonet.js"); importScripts("grammalecte/fr/cregex.js"); 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"); - -helpers.echo("helpers echo"); +/* + 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. +*/ +onmessage = function (e) { + if (e.data[0] == "init") { + loadGrammarChecker(e.data[1]); + } +} +let oDict = null; let oTokenizer = null; let oLxg = null; - -function loadGrammarChecker (sGCOptions="", sContext="JavaScript") { - if (gc_engine === null) { - try { - gc_engine = require("resource://grammalecte/fr/gc_engine.js"); - helpers = require("resource://grammalecte/helpers.js"); - text = require("resource://grammalecte/text.js"); - tkz = require("resource://grammalecte/tokenizer.js"); - lxg = require("resource://grammalecte/fr/lexicographe.js"); - oTokenizer = new tkz.Tokenizer("fr"); - helpers.setLogOutput(console.log); - gc_engine.load(sContext); - oDict = gc_engine.getDictionary(); - oLxg = new lxg.Lexicographe(oDict); - if (sGCOptions !== "") { - gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); - } - // we always retrieve options from the gc_engine, for setOptions filters obsolete options - return gc_engine.getOptions()._toString(); - } - catch (e) { - console.log("# Error: " + e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); - } - } +let oTest = null; + +function loadGrammarChecker (sCGDataPath, 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"); + oDict = gc_engine.getDictionary(); + + 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(); + // we always retrieve options from the gc_engine, for setOptions filters obsolete options + //return gce.getOptions().gl_toString(); + } + catch (e) { + console.error("\n" + e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); + } + } function parse (sText, sLang, bDebug, bContext) { let aGrammErr = gc_engine.parse(sText, sLang, bDebug, bContext); return JSON.stringify(aGrammErr); @@ -116,11 +145,11 @@ } let dMemoOptions = gc_engine.getOptions(); if (sGCOptions) { gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); } - let oTest = new TestGrammarChecking(gc_engine); + for (let sRes of oTest.testParse()) { helpers.echo(sRes+"\n"); } gc_engine.setOptions(dMemoOptions); } @@ -143,29 +172,5 @@ catch (e) { helpers.logerror(e); } return JSON.stringify([]); } - - -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 oDict = new IBDAWG("French.json"); -helpers.echo(oDict.getMorph("merde")); - -gc_engine.load("JavaScript"); -let aRes = gc_engine.parse("Je suit..."); -for (let oErr of aRes) { - helpers.echo(text.getReadableError(oErr)); -} - - -//fullTests(); - -