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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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
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
397
398
399
400
401
|
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
autoRefreshOption();
/*
Connexion to the background
*/
const oGrammalecteBackgroundPort = {
let xGrammalectePort = browser.runtime.connect({name: "content-script port"});
xConnect: browser.runtime.connect({name: "content-script port"}),
/*
Send messages to the background
object {
sCommand: the action to perform
dParam: parameters necessary for the execution of the action
dInfo: all kind of informations that needs to be sent back (usually to know where to use the result)
}
*/
parseAndSpellcheck: function (sText, sDestination) {
this.xConnect.postMessage({
sCommand: "parseAndSpellcheck",
dParam: { sText: sText, sCountry: "FR", bDebug: false, bContext: false },
dInfo: { sDestination: sDestination }
});
},
parseAndSpellcheck1: function (sText, sDestination, sParagraphId) {
this.xConnect.postMessage({
sCommand: "parseAndSpellcheck1",
dParam: { sText: sText, sCountry: "FR", bDebug: false, bContext: false },
dInfo: { sDestination: sDestination, sParagraphId: sParagraphId }
});
},
getListOfTokens: function (sText) {
this.xConnect.postMessage({ sCommand: "getListOfTokens", dParam: { sText: sText }, dInfo: {} });
},
parseFull: function (sText) {
this.xConnect.postMessage({
sCommand: "parseFull",
dParam: { sText: sTex, sCountry: "FR", bDebug: false, bContext: false },
dInfo: {}
});
},
getVerb: function (sVerb, bStart=true, bPro=false, bNeg=false, bTpsCo=false, bInt=false, bFem=false) {
this.xConnect.postMessage({
sCommand: "getVerb",
dParam: { sVerb: sVerb, bPro: bPro, bNeg: bNeg, bTpsCo: bTpsCo, bInt: bInt, bFem: bFem },
dInfo: { bStart: bStart }
});
},
getSpellSuggestions: function (sWord, sErrorId) {
this.xConnect.postMessage({ sCommand: "getSpellSuggestions", dParam: { sWord: sWord }, dInfo: { sErrorId: sErrorId } });
},
openURL: function (sURL) {
this.xConnect.postMessage({ sCommand: "openURL", dParam: { "sURL": sURL }, dInfo: null });
},
openLexiconEditor: function () {
this.xConnect.postMessage({ sCommand: "openLexiconEditor", dParam: null, dInfo: null });
},
restartWorker: function (nTimeDelay=10) {
this.xConnect.postMessage({ sCommand: "restartWorker", dParam: { "nTimeDelay": nTimeDelay }, dInfo: {} });
},
/*
Messages from the background
*/
listen: function () {
xGrammalectePort.onMessage.addListener(function (oMessage) {
let {sActionDone, result, dInfo, bEnd, bError} = oMessage;
switch (sActionDone) {
case "init":
oGrammalecte.sExtensionUrl = oMessage.sUrl;
oGrammalecte.listenRightClick();
oGrammalecte.createButtons();
oGrammalecte.observePage();
break;
case "parseAndSpellcheck":
if (!bEnd) {
oGrammalecte.oGCPanel.addParagraphResult(result);
} else {
oGrammalecte.oGCPanel.stopWaitIcon();
oGrammalecte.oGCPanel.endTimer();
}
break;
case "parseAndSpellcheck1":
oGrammalecte.oGCPanel.refreshParagraph(dInfo.sParagraphId, result);
break;
case "parseFull":
// TODO
break;
case "getListOfTokens":
if (!bEnd) {
oGrammalecte.oGCPanel.addListOfTokens(result);
} else {
oGrammalecte.oGCPanel.stopWaitIcon();
oGrammalecte.oGCPanel.endTimer();
}
break;
case "getSpellSuggestions":
oGrammalecte.oGCPanel.oTooltip.setSpellSuggestionsFor(result.sWord, result.aSugg, result.iSuggBlock, dInfo.sErrorId);
break;
case "getVerb":
if (dInfo.bStart) {
oGrammalecte.oGCPanel.conjugateWith(result.oVerb, result.oConjTable);
} else {
oGrammalecte.oGCPanel.displayConj(result.oConjTable);
}
break;
case "workerRestarted":
oGrammalecte.oGCPanel.stopWaitIcon();
oGrammalecte.oGCPanel.showMessage("Le serveur grammatical a été arrêté et relancé.");
oGrammalecte.oGCPanel.endTimer();
break;
/*
Commands received from the context menu
(Context menu are initialized in background)
*/
// Grammar checker commands
case "grammar_checker_editable":
if (oGrammalecte.xRightClickedNode !== null) {
oGrammalecte.startGCPanel(oGrammalecte.xRightClickedNode);
} else {
oGrammalecte.showMessage("Erreur. Le node 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;
case "grammar_checker_page":
oGrammalecte.startGCPanel(oGrammalecte.getPageText());
break;
case "grammar_checker_selection":
oGrammalecte.startGCPanel(result, false); // result is the selected text
// selected text is sent to the GC worker in the background script.
break;
case "grammar_checker_iframe":
console.log("[Grammalecte] selected iframe: ", result);
if (document.activeElement.tagName == "IFRAME") {
//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;
// rescan page command
case "rescanPage":
oGrammalecte.rescanPage();
break;
default:
console.log("[Content script] Unknown command: " + sActionDone);
}
});
this.xConnect.onMessage.addListener(function (oMessage) {
let {sActionDone, result, dInfo, bEnd, bError} = oMessage;
switch (sActionDone) {
case "init":
oGrammalecte.sExtensionUrl = oMessage.sUrl;
oGrammalecte.listenRightClick();
oGrammalecte.createButtons();
oGrammalecte.observePage();
break;
case "parseAndSpellcheck":
if (dInfo.sDestination == "__GrammalectePanel__") {
if (!bEnd) {
oGrammalecte.oGCPanel.addParagraphResult(result);
} else {
oGrammalecte.oGCPanel.stopWaitIcon();
oGrammalecte.oGCPanel.endTimer();
}
}
break;
case "parseAndSpellcheck1":
if (dInfo.sDestination == "__GrammalectePanel__") {
oGrammalecte.oGCPanel.refreshParagraph(dInfo.sParagraphId, result);
}
break;
case "parseFull":
// TODO
break;
case "getListOfTokens":
if (!bEnd) {
oGrammalecte.oGCPanel.addListOfTokens(result);
} else {
oGrammalecte.oGCPanel.stopWaitIcon();
oGrammalecte.oGCPanel.endTimer();
}
break;
case "getSpellSuggestions":
oGrammalecte.oGCPanel.oTooltip.setSpellSuggestionsFor(result.sWord, result.aSugg, result.iSuggBlock, dInfo.sErrorId);
break;
case "getVerb":
if (dInfo.bStart) {
oGrammalecte.oGCPanel.conjugateWith(result.oVerb, result.oConjTable);
} else {
oGrammalecte.oGCPanel.displayConj(result.oConjTable);
}
break;
case "workerRestarted":
oGrammalecte.oGCPanel.stopWaitIcon();
oGrammalecte.oGCPanel.showMessage("Le serveur grammatical a été arrêté et relancé.");
oGrammalecte.oGCPanel.endTimer();
break;
/*
Commands received from the context menu
(Context menu are initialized in background)
*/
// Grammar checker commands
case "grammar_checker_editable":
if (oGrammalecte.xRightClickedNode !== null) {
oGrammalecte.startGCPanel(oGrammalecte.xRightClickedNode);
} else {
oGrammalecte.showMessage("Erreur. Le node 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;
case "grammar_checker_page":
oGrammalecte.startGCPanel(oGrammalecte.getPageText());
break;
case "grammar_checker_selection":
oGrammalecte.startGCPanel(result, false); // result is the selected text
// selected text is sent to the GC worker in the background script.
break;
case "grammar_checker_iframe":
console.log("[Grammalecte] selected iframe: ", result);
if (document.activeElement.tagName == "IFRAME") {
//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;
// rescan page command
case "rescanPage":
oGrammalecte.rescanPage();
break;
default:
console.log("[Content script] Unknown command: " + sActionDone);
}
});
},
/*
Other messages from background
*/
listen2: function () {
browser.runtime.onMessage.addListener(function (oMessage) {
let {sActionRequest} = oMessage;
let xActiveNode = document.activeElement;
switch (sActionRequest) {
/*
Commands received from the keyboard (shortcuts)
*/
case "shortcutGrammarChecker":
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.
The API script must be injected this way to be callable by the page
*/
|