Grammalecte  Check-in [d5c7fd1677]

Overview
Comment:[fx] restart connection to background when it’s lost
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | fx
Files: files | file ages | folders
SHA3-256: d5c7fd1677eedc002187c6af4c4f6a51ffd6c783804e1f5f33532cae00d5f38e
User & Date: olr on 2020-04-14 13:29:33
Other Links: manifest | tags
Context
2020-04-14
15:26
[graphspell] add 1 to distance between word and suggestion if word is split check-in: a00365529c user: olr tags: trunk, graphspell
13:29
[fx] restart connection to background when it’s lost check-in: d5c7fd1677 user: olr tags: trunk, fx
10:00
[fr] bug: condition mal écrite check-in: e09fb49f56 user: olr tags: trunk, fr
Changes

Modified gc_lang/fr/webext/background.js from [3958c7eb7d] to [6248266d14].

287
288
289
290
291
292
293




294
295
296
297
298
299
300
function handleConnexion (xPort) {
    // Messages from tabs
    let iPortId = xPort.sender.tab.id; // identifier for the port: each port can be found at dConnx[iPortId]
    dConnx.set(iPortId, xPort);
    xPort.onMessage.addListener(function (oRequest) {
        let {sCommand, oParam, oInfo} = oRequest;
        switch (sCommand) {




            case "parse":
            case "parseAndSpellcheck":
            case "parseAndSpellcheck1":
            case "parseFull":
            case "getListOfTokens":
            case "getSpellSuggestions":
            case "getVerb":







>
>
>
>







287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
function handleConnexion (xPort) {
    // Messages from tabs
    let iPortId = xPort.sender.tab.id; // identifier for the port: each port can be found at dConnx[iPortId]
    dConnx.set(iPortId, xPort);
    xPort.onMessage.addListener(function (oRequest) {
        let {sCommand, oParam, oInfo} = oRequest;
        switch (sCommand) {
            case "ping":
                //console.log("[background] ping");
                xPort.postMessage({sActionDone: "ping", result: null, bInfo: null, bEnd: true, bError: false});
                break;
            case "parse":
            case "parseAndSpellcheck":
            case "parseAndSpellcheck1":
            case "parseFull":
            case "getListOfTokens":
            case "getSpellSuggestions":
            case "getVerb":

Modified gc_lang/fr/webext/content_scripts/init.js from [b6a51f3cb6] to [acbd3c2f80].

276
277
278
279
280
281
282
283


284














285
286
287
288
289
290
291
292
293
294
295
296
297
298
299


300
301




302
303
304
305
306
307
308
autoRefreshOption();


/*
    Connexion to the background
*/
const oGrammalecteBackgroundPort = {



    xConnect: browser.runtime.connect({name: "content-script port"}),















    /*
        Send messages to the background
        object {
            sCommand: the action to perform
            oParam: parameters necessary for the execution of the action
            oInfo: all kind of informations that needs to be sent back (usually to know where to use the result)
        }
    */

    send: function (sCommand, oParam={}, oInfo={}) {
        if (this.xConnect) {
            this.xConnect.postMessage({ sCommand: sCommand, oParam: oParam, oInfo: oInfo });
        } else {
            oGrammalecte.showMessage("Erreur. La connexion vers le correcteur grammatical est perdue.");


        }
    },





    parseAndSpellcheck: function (sText, sDestination) {
        this.send("parseAndSpellcheck", { sText: sText, sCountry: "FR", bDebug: false, bContext: false }, { sDestination: sDestination });
    },

    parseAndSpellcheck1: function (sText, sDestination, sParagraphId) {
        this.send("parseAndSpellcheck1", { sText: sText, sCountry: "FR", bDebug: false, bContext: false }, { sDestination: sDestination, sParagraphId: sParagraphId });








>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>











|


|
>
>


>
>
>
>







276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
autoRefreshOption();


/*
    Connexion to the background
*/
const oGrammalecteBackgroundPort = {

    bConnected: false,

    xConnect: browser.runtime.connect({name: "content-script port"}),

    start: function () {
        //console.log("[Grammalecte] background port: start.");
        this.listen();
        this.listen2();
        //this.ping();
    },

    restart: function () {
        console.log("[Grammalecte] try to reconnect to the background.")
        this.xConnect = browser.runtime.connect({name: "content-script port"});
        this.listen();
        this.ping();
    },

    /*
        Send messages to the background
        object {
            sCommand: the action to perform
            oParam: parameters necessary for the execution of the action
            oInfo: all kind of informations that needs to be sent back (usually to know where to use the result)
        }
    */

    send: function (sCommand, oParam={}, oInfo={}) {
        if (this.bConnected) {
            this.xConnect.postMessage({ sCommand: sCommand, oParam: oParam, oInfo: oInfo });
        } else {
            oGrammalecte.showMessage("Erreur. La connexion vers le correcteur grammatical est perdue.",
                                     "Tentative de reconnexion. Fermer la fenêtre et relancez. Si ça ne fonctionne pas, il sera nécessaire de recharger la page.");
            this.restart();
        }
    },

    ping: function () {
        this.xConnect.postMessage({ sCommand: "ping", oParam: {}, oInfo: {} });
    },

    parseAndSpellcheck: function (sText, sDestination) {
        this.send("parseAndSpellcheck", { sText: sText, sCountry: "FR", bDebug: false, bContext: false }, { sDestination: sDestination });
    },

    parseAndSpellcheck1: function (sText, sDestination, sParagraphId) {
        this.send("parseAndSpellcheck1", { sText: sText, sCountry: "FR", bDebug: false, bContext: false }, { sDestination: sDestination, sParagraphId: sParagraphId });
345
346
347
348
349
350
351
352
353
354
355
356
357
358

359
360
361
362




363
364
365
366
367
368
369
            if (xPort.error) {
                sError = xPort.error.message;
            }
            else if (browser.runtime.lastError) {
                sError = browser.runtime.lastError.message;
            }
            console.log("[Grammalecte] Connection to the background script has been lost. Error :", sError);
            this.xConnect = browser.runtime.connect({name: "content-script port"});
            this.listen();
        }.bind(this));
        this.xConnect.onMessage.addListener(function (oMessage) {
            let { sActionDone, result, oInfo, bEnd, bError } = oMessage;
            switch (sActionDone) {
                case "init":

                    oGrammalecte.sExtensionUrl = oMessage.sUrl;
                    oGrammalecte.listen();
                    oGrammalecte.createButton();
                    break;




                case "parseAndSpellcheck":
                    if (oInfo.sDestination == "__GrammalectePanel__") {
                        if (!bEnd) {
                            oGrammalecte.oGCPanel.addParagraphResult(result);
                        } else {
                            oGrammalecte.oGCPanel.stopWaitIcon();
                            oGrammalecte.oGCPanel.endTimer();







|
|





>




>
>
>
>







367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
            if (xPort.error) {
                sError = xPort.error.message;
            }
            else if (browser.runtime.lastError) {
                sError = browser.runtime.lastError.message;
            }
            console.log("[Grammalecte] Connection to the background script has been lost. Error :", sError);
            this.bConnected = false;
            this.restart();
        }.bind(this));
        this.xConnect.onMessage.addListener(function (oMessage) {
            let { sActionDone, result, oInfo, bEnd, bError } = oMessage;
            switch (sActionDone) {
                case "init":
                    this.bConnected = true;
                    oGrammalecte.sExtensionUrl = oMessage.sUrl;
                    oGrammalecte.listen();
                    oGrammalecte.createButton();
                    break;
                case "ping":
                    console.log("[Grammalecte] Connection to background done.");
                    this.bConnected = true;
                    break;
                case "parseAndSpellcheck":
                    if (oInfo.sDestination == "__GrammalectePanel__") {
                        if (!bEnd) {
                            oGrammalecte.oGCPanel.addParagraphResult(result);
                        } else {
                            oGrammalecte.oGCPanel.stopWaitIcon();
                            oGrammalecte.oGCPanel.endTimer();
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
                        //console.log(document.activeElement.id); frameId given by result is different than frame.id
                        oGrammalecte.startGCPanel(document.activeElement);
                    } else {
                        oGrammalecte.showMessage("Erreur. Le cadre sur lequel vous avez cliqué n’a pas pu être identifié. Sélectionnez le texte à corriger et relancez le correcteur via le menu contextuel.");
                    }
                    break;
                default:
                    console.log("[Content script] Unknown command: " + sActionDone);
            }
        });
    },

    /*
        Other messages from background
    */
    listen2: function () {
        browser.runtime.onMessage.addListener(function (oMessage) {







|

|







463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
                        //console.log(document.activeElement.id); frameId given by result is different than frame.id
                        oGrammalecte.startGCPanel(document.activeElement);
                    } else {
                        oGrammalecte.showMessage("Erreur. Le cadre sur lequel vous avez cliqué n’a pas pu être identifié. Sélectionnez le texte à corriger et relancez le correcteur via le menu contextuel.");
                    }
                    break;
                default:
                    console.log("[Grammalecte] Content-script. Unknown command: ", sActionDone);
            }
        }.bind(this));
    },

    /*
        Other messages from background
    */
    listen2: function () {
        browser.runtime.onMessage.addListener(function (oMessage) {
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
                    if (xActiveNode && (xActiveNode.tagName == "TEXTAREA" || xActiveNode.tagName == "INPUT" || xActiveNode.isContentEditable)) {
                        oGrammalecte.startGCPanel(xActiveNode);
                    } else {
                        oGrammalecte.startGCPanel(oGrammalecte.getPageText());
                    }
                    break;
                default:
                    console.log("[Content script] Unknown command: " + sActionDone);
            }
        });
    }
}

oGrammalecteBackgroundPort.listen()
oGrammalecteBackgroundPort.listen2()



/*
    Callable API for the webpage.

*/







|





|
|







487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
                    if (xActiveNode && (xActiveNode.tagName == "TEXTAREA" || xActiveNode.tagName == "INPUT" || xActiveNode.isContentEditable)) {
                        oGrammalecte.startGCPanel(xActiveNode);
                    } else {
                        oGrammalecte.startGCPanel(oGrammalecte.getPageText());
                    }
                    break;
                default:
                    console.log("[Grammalecte] Content-script. Unknown command: ", sActionRequest);
            }
        });
    }
}


oGrammalecteBackgroundPort.start();



/*
    Callable API for the webpage.

*/