Grammalecte  Diff

Differences From Artifact [bf6ff940d5]:

To Artifact [342fbed920]:


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

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







-
+










-
+





+
+
+
+
-
+

-
-
+
+

-
-
+



-
+



-
+



-
+




-








-
+









-
+











-
+


-
+







-
+
    Communicate webpage script <=> web-extension
    La méthode d'injection de ce script est importante !

    Pour que la page web puisse envoyer des infos au background
    Page web => Script injecté => Content script => Background
    Pour la réponse se sont les même étape en sens inverse.
*/
var scriptEvent = document.createElement('script');
var scriptEvent = document.createElement("script");
scriptEvent.src = browser.extension.getURL("content_scripts/event.js");
document.documentElement.appendChild(scriptEvent);

var min = Math.ceil(0);
var max = Math.floor(9999999);
function uniqueID() {
    return Date.now().toString(36) + "-" + (Math.floor(Math.random() * (max - min)) + min).toString(36);
}

// ! Ecoute des messages venant du scipt injecté
document.addEventListener('GrammalecteEvent', function(event) {
document.addEventListener("GrammalecteEvent", function(event) {
    let actionFromPage = event.detail;
    //console.log(event);
    let sText = false;
    let dInfo = {};
    let elmForGramma = null;

    if (actionFromPage.iframe) {
        elmForGramma = document.getElementById(actionFromPage.iframe).contentWindow.document.body;
    }
    if (actionFromPage.elm){
    if (actionFromPage.elm) {
        elmForGramma = document.getElementById(actionFromPage.elm);
        sText = (elmForGramma.tagName == "TEXTAREA") ? elmForGramma.value : elmForGramma.innerText;
        dInfo = {sTextAreaId: elmForGramma.id};
        sText = elmForGramma.tagName == "TEXTAREA" ? elmForGramma.value : elmForGramma.innerText;
        dInfo = { sTextAreaId: elmForGramma.id };
    }

    if (actionFromPage.spellcheck){
    if (actionFromPage.spellcheck) {
        oGrammalecte.startGCPanel(elmForGramma);
        xGrammalectePort.postMessage({
            sCommand: "parseAndSpellcheck",
            dParam: {sText: sText || actionFromPage.spellcheck, sCountry: "FR", bDebug: false, bContext: false},
            dParam: { sText: sText || actionFromPage.spellcheck, sCountry: "FR", bDebug: false, bContext: false },
            dInfo: dInfo
        });
    }
    if (actionFromPage.lexique){
    if (actionFromPage.lexique) {
        oGrammalecte.startLxgPanel();
        xGrammalectePort.postMessage({
            sCommand: "getListOfTokens",
            dParam: {sText: sText || actionFromPage.lexique},
            dParam: { sText: sText || actionFromPage.lexique },
            dInfo: dInfo
        });
    }
});


let isLoaded = false;
let bufferMsg = [];

// ! Permet d'envoyer des messages vers le scipt injecté
// (peut aussi être lu par un script sur la page web)
function sendToWebpage(dataAction) {
    let dataToSend = dataAction;
    if (typeof dataToSend.IdAction === "undefined"){
    if (typeof dataToSend.IdAction === "undefined") {
        dataToSend.IdAction = uniqueID();
    }
    if (dataAction.elm) {
        if (!dataAction.elm.id) {
            dataAction.elm.id = uniqueID();
        }
        dataToSend.elm = dataAction.elm.id;
    }

    if ( !isLoaded ){
    if (!isLoaded) {
        bufferMsg.push(dataToSend);
    } else {
        //console.log('sendToWebpage', dataToSend);
        var eventGrammalecte = new CustomEvent("GrammalecteToPage", { detail: dataToSend });
        document.dispatchEvent(eventGrammalecte);
    }

    return dataToSend.IdAction;
}

// ! Les message ne peuvent être envoyer qu'après que le script est injecté
document.addEventListener('GrammalecteIsLoaded', function() {
document.addEventListener("GrammalecteIsLoaded", function() {
    //console.log("GrammalecteIsLoaded EXT");
    isLoaded = true;
    if ( bufferMsg.length > 0 ){
    if (bufferMsg.length > 0) {
        for (const dataToSend of bufferMsg) {
            var eventGrammalecte = new CustomEvent("GrammalecteToPage", { detail: dataToSend });
            document.dispatchEvent(eventGrammalecte);
        }
    }
});

sendToWebpage({init: browser.extension.getURL("")});
sendToWebpage({ init: browser.extension.getURL("") });