Grammalecte  Check-in [0d2811ed48]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:[fx] new initialization for background and worker
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | fx
Files: files | file ages | folders
SHA3-256:0d2811ed48262c7ca3aeb419eabe498c5ec3b8e0768143742d677a0d5d7ccc0c
User & Date: olr 2019-08-09 10:02:00
Context
2019-08-10
10:56
[fx] buttons to stop and restart the worker check-in: 7bf95d9332 user: olr tags: fx, trunk
2019-08-09
10:02
[fx] new initialization for background and worker check-in: 0d2811ed48 user: olr tags: fx, trunk
07:23
[fr] nr: confusion aile/elle check-in: ba098fe978 user: olr tags: fr, trunk
Changes

Changes to gc_lang/fr/webext/background.js.

     1      1   // Background
     2      2   
     3      3   /* jshint esversion:6, -W097 */
     4      4   /* jslint esversion:6 */
     5         -/* global GrammalectePanel, oGrammalecte, helpers, showError, Worker, chrome, console */
            5  +/* global helpers, showError, Worker, chrome, console */
     6      6   
     7      7   "use strict";
     8      8   
     9      9   
    10         -function showError (e) {
    11         -    console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
    12         -}
    13         -
    14     10   // Chrome don’t follow the W3C specification:
    15     11   // https://browserext.github.io/browserext/
    16     12   let bChrome = false;
    17     13   if (typeof(browser) !== "object") {
    18     14       var browser = chrome;
    19     15       bChrome = true;
    20     16   }
    21     17   
    22     18   
    23         -/*
    24         -    Worker (separate thread to avoid freezing Firefox)
    25         -*/
    26         -let xGCEWorker = new Worker("gce_worker.js");
    27         -
    28         -xGCEWorker.onmessage = function (e) {
    29         -    // https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
    30         -    try {
    31         -        let {sActionDone, result, dInfo, bEnd, bError} = e.data;
    32         -        if (bError) {
    33         -            console.log(result);
    34         -            console.log(dInfo);
           19  +const oWorkerHandler = {
           20  +    xGCEWorker: null,
           21  +
           22  +    nLastTimeWorkerResponse: 0,  // milliseconds since 1970-01-01
           23  +
           24  +    oTask: {},
           25  +
           26  +    start: function () {
           27  +        this.xGCEWorker = new Worker("gce_worker.js");
           28  +        this.xGCEWorker.onmessage = function (e) {
           29  +            // Messages received from the Worker
           30  +            // https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
           31  +            try {
           32  +                this.nLastTimeWorkerResponse = Date.now();
           33  +                let {sActionDone, result, dInfo, bEnd, bError} = e.data;
           34  +                if (bError) {
           35  +                    console.log(result);
           36  +                    console.log(dInfo);
           37  +                    return;
           38  +                }
           39  +                switch (sActionDone) {
           40  +                    case "init":
           41  +                        storeGCOptions(result);
           42  +                        break;
           43  +                    case "parse":
           44  +                    case "parseAndSpellcheck":
           45  +                    case "parseAndSpellcheck1":
           46  +                    case "parseFull":
           47  +                    case "getListOfTokens":
           48  +                    case "getSpellSuggestions":
           49  +                    case "getVerb":
           50  +                        // send result to content script
           51  +                        if (typeof(dInfo.iReturnPort) === "number") {
           52  +                            let xPort = dConnx.get(dInfo.iReturnPort);
           53  +                            xPort.postMessage(e.data);
           54  +                        } else {
           55  +                            console.log("[background] don’t know where to send results");
           56  +                            console.log(e.data);
           57  +                        }
           58  +                        break;
           59  +                    case "textToTest":
           60  +                    case "fullTests":
           61  +                        // send result to panel
           62  +                        browser.runtime.sendMessage(e.data);
           63  +                        break;
           64  +                    case "getOptions":
           65  +                    case "getDefaultOptions":
           66  +                    case "resetOptions":
           67  +                        // send result to panel
           68  +                        storeGCOptions(result);
           69  +                        browser.runtime.sendMessage(e.data);
           70  +                        break;
           71  +                    case "setOptions":
           72  +                    case "setOption":
           73  +                        storeGCOptions(result);
           74  +                        break;
           75  +                    case "setDictionary":
           76  +                    case "setDictionaryOnOff":
           77  +                        //console.log("[background] " + sActionDone + ": " + result);
           78  +                        break;
           79  +                    default:
           80  +                        console.log("[background] Unknown command: " + sActionDone);
           81  +                        console.log(e.data);
           82  +                }
           83  +            }
           84  +            catch (error) {
           85  +                showError(error);
           86  +                console.log(e.data);
           87  +            }
           88  +        };
           89  +    },
           90  +
           91  +    getTimeSinceLastResponse: function () {
           92  +        // result in seconds
           93  +        return Math.floor((Date.now() - this.nLastTimeWorkerResponse) / 1000);
           94  +    },
           95  +
           96  +    restart: function () {
           97  +        if (this.getTimeSinceLastResponse() <= 5) {
           98  +            return false;
           99  +        }
          100  +        if (this.xGCEWorker) {
          101  +            this.xGCEWorker.terminate();
          102  +        }
          103  +        this.start();
          104  +        oInitHandler.initGrammarChecker();
          105  +        return true;
          106  +    },
          107  +
          108  +    addTask: function () {
          109  +        //
          110  +    },
          111  +
          112  +    closeTask: function () {
          113  +        //
          114  +    }
          115  +}
          116  +
          117  +
          118  +const oInitHandler = {
          119  +
          120  +    initUIOptions: function () {
          121  +        if (bChrome) {
          122  +            browser.storage.local.get("ui_options", this._initUIOptions);
          123  +            return;
          124  +        }
          125  +        browser.storage.local.get("ui_options").then(this._initUIOptions, showError);
          126  +    },
          127  +
          128  +    initGrammarChecker: function () {
          129  +        if (bChrome) {
          130  +            browser.storage.local.get("gc_options", this._initGrammarChecker);
          131  +            browser.storage.local.get("personal_dictionary", this._setSpellingDictionaries);
          132  +            browser.storage.local.get("community_dictionary", this._setSpellingDictionaries);
          133  +            browser.storage.local.get("oPersonalDictionary", this._setSpellingDictionaries); // deprecated
          134  +            browser.storage.local.get("sc_options", this._initSCOptions);
    35    135               return;
    36    136           }
    37         -        switch (sActionDone) {
    38         -            case "init":
    39         -                storeGCOptions(result);
    40         -                break;
    41         -            case "parse":
    42         -            case "parseAndSpellcheck":
    43         -            case "parseAndSpellcheck1":
    44         -            case "parseFull":
    45         -            case "getListOfTokens":
    46         -            case "getSpellSuggestions":
    47         -            case "getVerb":
    48         -                // send result to content script
    49         -                if (typeof(dInfo.iReturnPort) === "number") {
    50         -                    let xPort = dConnx.get(dInfo.iReturnPort);
    51         -                    xPort.postMessage(e.data);
    52         -                } else {
    53         -                    console.log("[background] don’t know where to send results");
    54         -                    console.log(e.data);
    55         -                }
    56         -                break;
    57         -            case "textToTest":
    58         -            case "fullTests":
    59         -                // send result to panel
    60         -                browser.runtime.sendMessage(e.data);
    61         -                break;
    62         -            case "getOptions":
    63         -            case "getDefaultOptions":
    64         -            case "resetOptions":
    65         -                // send result to panel
    66         -                storeGCOptions(result);
    67         -                browser.runtime.sendMessage(e.data);
    68         -                break;
    69         -            case "setOptions":
    70         -            case "setOption":
    71         -                storeGCOptions(result);
    72         -                break;
    73         -            case "setDictionary":
    74         -            case "setDictionaryOnOff":
    75         -                //console.log("[background] " + sActionDone + ": " + result);
    76         -                break;
    77         -            default:
    78         -                console.log("[background] Unknown command: " + sActionDone);
    79         -                console.log(e.data);
          137  +        browser.storage.local.get("gc_options").then(this._initGrammarChecker, showError);
          138  +        browser.storage.local.get("personal_dictionary").then(this._setSpellingDictionaries, showError);
          139  +        browser.storage.local.get("community_dictionary").then(this._setSpellingDictionaries, showError);
          140  +        browser.storage.local.get("oPersonalDictionary").then(this._setSpellingDictionaries, showError); // deprecated
          141  +        browser.storage.local.get("sc_options").then(this._initSCOptions, showError);
          142  +    },
          143  +
          144  +    _initUIOptions: function (oSavedOptions) {
          145  +        if (!oSavedOptions.hasOwnProperty("ui_options")) {
          146  +            browser.storage.local.set({"ui_options": {
          147  +                textarea: true,
          148  +                editablenode: true
          149  +            }});
          150  +        }
          151  +    },
          152  +
          153  +    _initGrammarChecker: function (oSavedOptions) {
          154  +        try {
          155  +            let dOptions = (oSavedOptions.hasOwnProperty("gc_options")) ? oSavedOptions.gc_options : null;
          156  +            if (dOptions !== null && Object.getOwnPropertyNames(dOptions).length == 0) {
          157  +                console.log("# Error: the saved options was an empty object.");
          158  +                dOptions = null;
          159  +            }
          160  +            oWorkerHandler.xGCEWorker.postMessage({
          161  +                sCommand: "init",
          162  +                dParam: {sExtensionPath: browser.extension.getURL(""), dOptions: dOptions, sContext: "Firefox"},
          163  +                dInfo: {}
          164  +            });
          165  +        }
          166  +        catch (e) {
          167  +            console.log("initGrammarChecker failed");
          168  +            showError(e);
          169  +        }
          170  +    },
          171  +
          172  +    _setSpellingDictionaries: function (oData) {
          173  +        if (oData.hasOwnProperty("oPersonalDictionary")) {
          174  +            // deprecated (to be removed in 2020)
          175  +            console.log("personal dictionary migration");
          176  +            browser.storage.local.set({ "personal_dictionary": oData["oPersonalDictionary"] });
          177  +            oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionary", dParam: { sDictionary: "personal", oDict: oData["oPersonalDictionary"] }, dInfo: {} });
          178  +            browser.storage.local.remove("oPersonalDictionary");
          179  +        }
          180  +        if (oData.hasOwnProperty("community_dictionary")) {
          181  +            oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionary", dParam: { sDictionary: "community", oDict: oData["community_dictionary"] }, dInfo: {} });
          182  +        }
          183  +        if (oData.hasOwnProperty("personal_dictionary")) {
          184  +            oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionary", dParam: { sDictionary: "personal", oDict: oData["personal_dictionary"] }, dInfo: {} });
          185  +        }
          186  +    },
          187  +
          188  +    _initSCOptions: function (oData) {
          189  +        if (!oData.hasOwnProperty("sc_options")) {
          190  +            browser.storage.local.set({"sc_options": {
          191  +                extended: true,
          192  +                community: true,
          193  +                personal: true
          194  +            }});
          195  +            oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionaryOnOff", dParam: { sDictionary: "community", bActivate: true }, dInfo: {} });
          196  +            oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionaryOnOff", dParam: { sDictionary: "personal", bActivate: true }, dInfo: {} });
          197  +        } else {
          198  +            oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionaryOnOff", dParam: { sDictionary: "community", bActivate: oData.sc_options["community"] }, dInfo: {} });
          199  +            oWorkerHandler.xGCEWorker.postMessage({ sCommand: "setDictionaryOnOff", dParam: { sDictionary: "personal", bActivate: oData.sc_options["personal"] }, dInfo: {} });
    80    200           }
    81    201       }
    82         -    catch (error) {
    83         -        showError(error);
    84         -        console.log(e.data);
    85         -    }
    86         -};
    87         -
    88         -function initUIOptions (dSavedOptions) {
    89         -    if (!dSavedOptions.hasOwnProperty("ui_options")) {
    90         -        browser.storage.local.set({"ui_options": {
    91         -            textarea: true,
    92         -            editablenode: true
    93         -        }});
    94         -    }
    95         -}
    96         -
    97         -function initGrammarChecker (dSavedOptions) {
    98         -    try {
    99         -        let dOptions = (dSavedOptions.hasOwnProperty("gc_options")) ? dSavedOptions.gc_options : null;
   100         -        if (dOptions !== null && Object.getOwnPropertyNames(dOptions).length == 0) {
   101         -            console.log("# Error: the saved options was an empty object.");
   102         -            dOptions = null;
   103         -        }
   104         -        xGCEWorker.postMessage({
   105         -            sCommand: "init",
   106         -            dParam: {sExtensionPath: browser.extension.getURL(""), dOptions: dOptions, sContext: "Firefox"},
   107         -            dInfo: {}
   108         -        });
   109         -    }
   110         -    catch (e) {
   111         -        console.log("initGrammarChecker failed");
   112         -        showError(e);
   113         -    }
   114         -}
   115         -
   116         -function setDictionaryOnOff (sDictionary, bActivate) {
   117         -    xGCEWorker.postMessage({
   118         -        sCommand: "setDictionaryOnOff",
   119         -        dParam: { sDictionary: sDictionary, bActivate: bActivate },
   120         -        dInfo: {}
   121         -    });
   122         -}
   123         -
   124         -function initSCOptions (oData) {
   125         -    if (!oData.hasOwnProperty("sc_options")) {
   126         -        browser.storage.local.set({"sc_options": {
   127         -            extended: true,
   128         -            community: true,
   129         -            personal: true
   130         -        }});
   131         -        setDictionaryOnOff("community", true);
   132         -        setDictionaryOnOff("personal", true);
   133         -    } else {
   134         -        setDictionaryOnOff("community", oData.sc_options["community"]);
   135         -        setDictionaryOnOff("personal", oData.sc_options["personal"]);
   136         -    }
   137         -}
   138         -
   139         -function setDictionary (sDictionary, oDictionary) {
   140         -    xGCEWorker.postMessage({
   141         -        sCommand: "setDictionary",
   142         -        dParam: { sDictionary: sDictionary, oDict: oDictionary },
   143         -        dInfo: {}
   144         -    });
   145         -}
   146         -
   147         -function setSpellingDictionaries (oData) {
   148         -    if (oData.hasOwnProperty("oPersonalDictionary")) {
   149         -        // deprecated (to be removed in 2020)
   150         -        console.log("personal dictionary migration");
   151         -        browser.storage.local.set({ "personal_dictionary": oData["oPersonalDictionary"] });
   152         -        setDictionary("personal", oData["oPersonalDictionary"]);
   153         -        browser.storage.local.remove("oPersonalDictionary");
   154         -    }
   155         -    if (oData.hasOwnProperty("personal_dictionary")) {
   156         -        setDictionary("personal", oData["personal_dictionary"]);
   157         -    }
   158         -    if (oData.hasOwnProperty("community_dictionary")) {
   159         -        setDictionary("community", oData["community_dictionary"]);
   160         -    }
   161         -}
   162         -
   163         -function init () {
   164         -    if (bChrome) {
   165         -        browser.storage.local.get("gc_options", initGrammarChecker);
   166         -        browser.storage.local.get("ui_options", initUIOptions);
   167         -        browser.storage.local.get("personal_dictionary", setSpellingDictionaries);
   168         -        browser.storage.local.get("community_dictionary", setSpellingDictionaries);
   169         -        browser.storage.local.get("oPersonalDictionary", setSpellingDictionaries); // deprecated
   170         -        browser.storage.local.get("sc_options", initSCOptions);
   171         -        return;
   172         -    }
   173         -    browser.storage.local.get("gc_options").then(initGrammarChecker, showError);
   174         -    browser.storage.local.get("ui_options").then(initUIOptions, showError);
   175         -    browser.storage.local.get("personal_dictionary").then(setSpellingDictionaries, showError);
   176         -    browser.storage.local.get("community_dictionary").then(setSpellingDictionaries, showError);
   177         -    browser.storage.local.get("oPersonalDictionary").then(setSpellingDictionaries, showError); // deprecated
   178         -    browser.storage.local.get("sc_options").then(initSCOptions, showError);
   179         -}
   180         -
   181         -init();
   182         -
   183         -
          202  +}
          203  +
          204  +// start the Worker for the GC
          205  +oWorkerHandler.start();
          206  +
          207  +// init the options stuff and start the GC
          208  +oInitHandler.initUIOptions();
          209  +oInitHandler.initGrammarChecker();
          210  +
          211  +
          212  +// When the extension is installed or updated
   184    213   browser.runtime.onInstalled.addListener(function (oDetails) {
   185    214       // launched at installation or update
   186    215       // https://developer.mozilla.org/fr/Add-ons/WebExtensions/API/runtime/onInstalled
   187    216       if (oDetails.reason == "update"  ||  oDetails.reason == "installed") {
   188    217           // todo
   189    218           //browser.tabs.create({url: "http://grammalecte.net"});
   190    219       }
................................................................................
   212    241           case "setOptions":
   213    242           case "setOption":
   214    243           case "resetOptions":
   215    244           case "textToTest":
   216    245           case "fullTests":
   217    246           case "setDictionary":
   218    247           case "setDictionaryOnOff":
   219         -            xGCEWorker.postMessage(oRequest);
          248  +            oWorkerHandler.xGCEWorker.postMessage(oRequest);
   220    249               break;
   221    250           case "openURL":
   222    251               browser.tabs.create({url: dParam.sURL});
   223    252               break;
   224    253           case "openConjugueurTab":
   225    254               openConjugueurTab();
   226    255               break;
................................................................................
   251    280               case "parseAndSpellcheck":
   252    281               case "parseAndSpellcheck1":
   253    282               case "parseFull":
   254    283               case "getListOfTokens":
   255    284               case "getSpellSuggestions":
   256    285               case "getVerb":
   257    286                   oRequest.dInfo.iReturnPort = iPortId; // we pass the id of the return port to receive answer
   258         -                xGCEWorker.postMessage(oRequest);
          287  +                oWorkerHandler.xGCEWorker.postMessage(oRequest);
   259    288                   break;
   260    289               case "openURL":
   261    290                   browser.tabs.create({url: dParam.sURL});
   262    291                   break;
   263    292               case "openConjugueurTab":
   264    293                   openConjugueurTab();
   265    294                   break;
................................................................................
   307    336           // analyze
   308    337           case "grammar_checker_editable":
   309    338           case "grammar_checker_page":
   310    339               sendCommandToTab(xTab.id, xInfo.menuItemId);
   311    340               break;
   312    341           case "grammar_checker_selection":
   313    342               sendCommandToTab(xTab.id, xInfo.menuItemId, xInfo.selectionText);
   314         -            xGCEWorker.postMessage({
          343  +            oWorkerHandler.xGCEWorker.postMessage({
   315    344                   sCommand: "parseAndSpellcheck",
   316    345                   dParam: {sText: xInfo.selectionText, sCountry: "FR", bDebug: false, bContext: false},
   317    346                   dInfo: {iReturnPort: xTab.id}
   318    347               });
   319    348               break;
   320    349           // tools
   321    350           case "conjugueur_window":
................................................................................
   407    436           return;
   408    437       }
   409    438       browser.tabs.query({ currentWindow: true, active: true }).then((lTabs) => {
   410    439           for (let xTab of lTabs) {
   411    440               console.log(xTab);
   412    441               browser.tabs.sendMessage(xTab.id, {sActionRequest: sCommand});
   413    442           }
   414         -    }, onError);
          443  +    }, showError);
   415    444   }
   416    445   
   417    446   function openLexiconEditor (sName="__personal__") {
   418    447       if (nTabLexiconEditor === null) {
   419    448           if (bChrome) {
   420    449               browser.tabs.create({
   421    450                   url: browser.extension.getURL("panel/lex_editor.html")
   422    451               }, onLexiconEditorOpened);
   423    452               return;
   424    453           }
   425    454           let xLexEditor = browser.tabs.create({
   426    455               url: browser.extension.getURL("panel/lex_editor.html")
   427    456           });
   428         -        xLexEditor.then(onLexiconEditorOpened, onError);
          457  +        xLexEditor.then(onLexiconEditorOpened, showError);
   429    458       }
   430    459       else {
   431    460           browser.tabs.update(nTabLexiconEditor, {active: true});
   432    461       }
   433    462   }
   434    463   
   435    464   function onLexiconEditorOpened (xTab) {
................................................................................
   443    472                   url: browser.extension.getURL("panel/dictionaries.html")
   444    473               }, onDictionariesOpened);
   445    474               return;
   446    475           }
   447    476           let xLexEditor = browser.tabs.create({
   448    477               url: browser.extension.getURL("panel/dictionaries.html")
   449    478           });
   450         -        xLexEditor.then(onDictionariesOpened, onError);
          479  +        xLexEditor.then(onDictionariesOpened, showError);
   451    480       }
   452    481       else {
   453    482           browser.tabs.update(nTabDictionaries, {active: true});
   454    483       }
   455    484   }
   456    485   
   457    486   function onDictionariesOpened (xTab) {
................................................................................
   465    494                   url: browser.extension.getURL("panel/conjugueur.html")
   466    495               }, onConjugueurOpened);
   467    496               return;
   468    497           }
   469    498           let xConjTab = browser.tabs.create({
   470    499               url: browser.extension.getURL("panel/conjugueur.html")
   471    500           });
   472         -        xConjTab.then(onConjugueurOpened, onError);
          501  +        xConjTab.then(onConjugueurOpened, showError);
   473    502       }
   474    503       else {
   475    504           browser.tabs.update(nTabConjugueur, {active: true});
   476    505       }
   477    506   }
   478    507   
   479    508   function onConjugueurOpened (xTab) {
................................................................................
   495    524           type: "popup",
   496    525           width: 710,
   497    526           height: 980
   498    527       });
   499    528   }
   500    529   
   501    530   
   502         -function onError (e) {
          531  +function showError (e) {
   503    532       console.error(e);
          533  +    //console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
   504    534   }