Grammalecte  Diff

Differences From Artifact [b6a51f3cb6]:

To Artifact [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
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.xConnect) {
        if (this.bConnected) {
            this.xConnect.postMessage({ sCommand: sCommand, oParam: oParam, oInfo: oInfo });
        } else {
            oGrammalecte.showMessage("Erreur. La connexion vers le correcteur grammatical est perdue.");
            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
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.xConnect = browser.runtime.connect({name: "content-script port"});
            this.listen();
            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
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("[Content script] Unknown command: " + sActionDone);
                    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
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("[Content script] Unknown command: " + sActionDone);
                    console.log("[Grammalecte] Content-script. Unknown command: ", sActionRequest);
            }
        });
    }
}

oGrammalecteBackgroundPort.listen()
oGrammalecteBackgroundPort.listen2()

oGrammalecteBackgroundPort.start();



/*
    Callable API for the webpage.

*/