Grammalecte  Check-in [c5404a7d12]

Overview
Comment:Hack for acces to iframe content and somme cleanup
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | comunicate
Files: files | file ages | folders
SHA3-256: c5404a7d12258da94b0167fa6708151b770115571973a40b3551754f9e0d4829
User & Date: IllusionPerdu on 2018-11-08 17:13:57
Other Links: branch diff | manifest | tags
Context
2018-11-08
17:51
Fix for firefox check-in: 175e6bd8bf user: IllusionPerdu tags: comunicate
17:13
Hack for acces to iframe content and somme cleanup check-in: c5404a7d12 user: IllusionPerdu tags: comunicate
10:34
Add comment, Add bidirectional communication check-in: 717fe4b51a user: IllusionPerdu tags: comunicate
Changes

Modified gc_lang/fr/webext/content_scripts/event.js from [f5b3e911d5] to [52dc78c9a0].

8
9
10
11
12
13
14
15

16
17
18
19
20
21
22
23

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

49
50
51




52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68


























69
70
71



72
73
74
75
76
77

78
79
80
81
82
83
84
85
86
87
88

89
90
91
92


93







94

95
96
97
98
99
100
101
102






103
104
105
106
107
108
109
110
111
112









113
114

115
116
117
118
119
120
121
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

44
45
46

47
48
49
50
51
52
53
54

















55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80



81
82
83






84





85
86
87
88
89

90
91
92


93
94
95
96
97
98
99
100
101
102

103
104
105






106
107
108
109
110
111
112
113








114
115
116
117
118
119
120
121
122
123

124
125
126
127
128
129
130
131







-
+







-
+




















-



-
+



+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
-
-
-
-
+
-
-
-
-
-





-
+


-
-
+
+

+
+
+
+
+
+
+
-
+


-
-
-
-
-
-
+
+
+
+
+
+


-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+

-
+







let browserURL;
document.addEventListener("GrammalecteToPage", function respListener(event) {
    var data = event.detail;
    // Message envoyer dès que le script est injecté
    if (typeof data.init !== "undefined") {
        browserURL = data.init;
    }
    console.log("GrammalecteToPage",data);
    //console.log("GrammalecteToPage",data);
});

// ! Permet d'envoyer des message vers le content script
// Retourne un identifiant unique au cas ou si besoin
// La ID unique peut être util si on permet d'intérogé grammalecte sans zone
function sendToGrammalecte(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;
    }

    //console.log('dataToSend', dataToSend);
    var eventGrammalecte = new CustomEvent("GrammalecteEvent", { detail: dataToSend });
    document.dispatchEvent(eventGrammalecte);
    return dataToSend.IdAction;
}

// ! Envoie de l'information que l'injection est bien faite ;)
// (peut être lu aussi bien par la page web que le content script)
var customAPILoaded = new CustomEvent("GrammalecteIsLoaded");
document.dispatchEvent(customAPILoaded);


// Gros Hack : Auto add a button in tinymce ;)
// Page to test v4 https://www.quackit.com/html/html_editors/tinymce_editor.cfm
// Page to test v3 http://www.imathas.com/editordemo/demo.html
if (typeof tinymce !== "undefined" && tinymce.majorVersion && tinymce.majorVersion >= 3 && tinymce.majorVersion <= 5) {
if (typeof tinyMCE !== "undefined" && tinyMCE.majorVersion && tinyMCE.majorVersion >= 3 && tinyMCE.majorVersion <= 5) {
    //console.log("Have TinyMCE");
    function TinyOnEditor(event, editor = null) {
        let xEditorAdd = editor || event.editor;

        if (typeof xEditorAdd.settings.Grammalecte === "undefined") {
            let aBtn;
            let plugSep;
        let bIsAdded = false;

        function addBtnTiny(bIsAdded, eBtn, iBtn, nBtn) {
            if (!bIsAdded && (typeof xEditorAdd.settings[eBtn] !== "undefined" || iBtn == nBtn)) {
                bIsAdded = true;
                if (typeof xEditorAdd.settings[eBtn] !== "undefined" && xEditorAdd.settings[eBtn] !== "") {
                    xEditorAdd.settings[eBtn] = (xEditorAdd.settings[eBtn] + ",Grammalecte").trim();
                } else {
                    let m = /(.*)([0-9])/.exec(eBtn);
                    if (m.length === 3 && parseInt(m[2]) > 1 && xEditorAdd.settings[eBtn] === "") {
                        eBtn = m[1] + (parseInt(m[2]) - 1);
                        xEditorAdd.settings[eBtn] = (xEditorAdd.settings[eBtn] + ",Grammalecte").trim();
                    } else {
                        xEditorAdd.settings[eBtn] = "Grammalecte";
                    }
                }
            }
            let bIsAdded = false;
            if (tinyMCE.majorVersion >= 4) {
                plugSep = " ";
                aBtn = ["toolbar3", "toolbar2", "toolbar1", "toolbar"];
            } else if (tinyMCE.majorVersion >= 3) {
                plugSep = ",";
                aBtn = ["theme_advanced_buttons3", "theme_advanced_buttons2", "theme_advanced_buttons1", "theme_advanced_buttons1_add_before"];
            }

            let iBtn = 0;
            let nBtn = aBtn.length;
            for (let eBtn of aBtn) {
                if (!bIsAdded && (typeof xEditorAdd.settings[eBtn] !== "undefined" || iBtn == nBtn)) {
                    bIsAdded = true;
                    if (typeof xEditorAdd.settings[eBtn] !== "undefined" && xEditorAdd.settings[eBtn] !== "") {
                        xEditorAdd.settings[eBtn] = (xEditorAdd.settings[eBtn] + plugSep + "Grammalecte").trim();
                    } else {
                        let m = /(.*)([0-9])/.exec(eBtn);
                        if (m.length === 3 && parseInt(m[2]) > 1 && xEditorAdd.settings[eBtn] === "") {
                            eBtn = m[1] + (parseInt(m[2]) - 1);
                            xEditorAdd.settings[eBtn] = (xEditorAdd.settings[eBtn] + plugSep + "Grammalecte").trim();
                        } else {
                            xEditorAdd.settings[eBtn] = "Grammalecte";
                        }
                    }
                }
            return bIsAdded;
        }

                iBtn++;
            }
            xEditorAdd.settings.Grammalecte = true;
        let aBtn;
        let iBtn = 0;
        if (tinymce.majorVersion >= 4) {
            aBtn = ["toolbar3", "toolbar2", "toolbar1", "toolbar"];
        } else if (tinymce.majorVersion >= 3) {
            aBtn = ["theme_advanced_buttons3", "theme_advanced_buttons2", "theme_advanced_buttons1"];
            //xEditorAdd.settings.theme_advanced_buttons1_add_before = "Grammalecte";
        }
        let nBtn = aBtn.length;
        for (let eBtn of aBtn) {
            bIsAdded = addBtnTiny(bIsAdded, eBtn, iBtn, nBtn);
            iBtn++;
        }

        xEditorAdd.addButton("Grammalecte", {
            text: "",
            icon: false,
            image: browserURL +"img/logo-16.png",
            image: browserURL + "img/logo-16.png",
            //"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAC8UlEQVQ4jX3TbUgTcRwH8P89ddu5u9tt082aZmpFEU4tFz0QGTUwCi0heniR9MSUIKRaD0RvIlKigsooo+iNFa0XJYuwIjEK19OcDtPElsG0ktyp591t7u7+vUh7MPX3+vf5/n8/+P0BmKJIPUUVlh2rdVVeesWlzEybqg+bFOsoylnqPmNavGFfknV2Omu2Lvja3vxAURKJib3opHizu8riLK6gjRyuKgmoSoMRFENRUqfXTzvBGK62LC2uoFkOl4RhjQ8+qWt7dPNE3sbdp+2LXbsGe9qb4rIo/BfwFy6nWQ4ThWGNDzbcfu29dMDh2nHU7CypYNLmzTda0/L5cNuzmDQi/A4Y27k6eQxLI79wS/11D0AAMNvs6XT6ojVJjJEgTbMy2BT77xBMp09KcpaWV1uc41jQoi0NdUHfjeOO9WWn7AVF7s7n986SithPJGeupBh2PCSP/xxqxAp3eq6wuUV7Wc6MSZIEhA8vHjbfOe/OcW3zmAuKy+nUzAyD2bow8ODaEROFq8AyZ5WBYdEZXGqGxZ61HJV+9HYCJRbTNA0QBA40HWunaKN5dKg/DBKxeCIe09Th/m4MJwiMSZmLEzMQAABQRuNqgu8NYX3doTcMpvCkLbtQZ2AJkrPOZG1zlnY13T+Hy9EehY90h57eqcorcZ/lctZuMzAsOjLEqwNv66/6vZcPYRBC+C3cGaBxhSet2av1BpYgTTY7k5y2JPT41slIR6Axv8R9nnOs+4Pf+2r992uOxGVJwgAAAEINfgt3BGgsESWtWas1iGDyl+CT/u7WpvxNFRc4x7qtBoZFhSFejb7z1fq9NYfjsiT+cwcQavBruCOgU4SIGo18amuoq3Js3FNlynVtH385+s53ze+t8cRkURx3yMTTRBAEQVAUXbFlf3XystJKA2NExeFBdWASDAAA+MQACCEEmqbJ0b6PMC7JwhDU8YFHV5u9NZ64LErT/oW/63tPV6uJwmKoOND78u7Fg5NhAAD4CVbzY9cwrWQrAAAAAElFTkSuQmCC",
            onclick: function(e) {
                //console.log( editorAdd.getContent() );
                //console.log( editorAdd.getBody().innerText )
                //console.log( xEditorAdd.getContent() );
                //console.log( xEditorAdd.getBody().innerText )
                let sText = xEditorAdd.getBody().innerText;
                let iframeElement;
                if (typeof xEditorAdd.iframeElement !== "undefined" && typeof xEditorAdd.iframeElement.id !== "undefined") {
                    iframeElement = xEditorAdd.iframeElement.id;
                } else if (typeof xEditorAdd.editorId !== "undefined") {
                    iframeElement = xEditorAdd.editorId + "_ifr";
                }

                sendToGrammalecte({ spellcheck: sText });
                sendToGrammalecte({ spellcheck: sText, iframe: iframeElement });
            }
        });
    };

    if (tinymce.majorVersion >= 4) {
        tinymce.on("AddEditor", TinyOnEditor);
    } else if (tinymce.majorVersion >= 3) {
        tinymce.onAddEditor.add(TinyOnEditor);
    }

    if (tinyMCE.majorVersion >= 4) {
        tinyMCE.on("AddEditor", TinyOnEditor);
    } else if (tinyMCE.majorVersion >= 3) {
        tinyMCE.onAddEditor.add(TinyOnEditor);
    }

    for (var i = tinymce.editors.length - 1; i > -1; i--) {
        let idTiny = tinymce.editors[i].id;
        if (tinymce.majorVersion >= 4) {
            tinymce.execCommand("mceRemoveEditor", true, idTiny);
            tinymce.execCommand("mceAddEditor", true, idTiny);
        } else if (tinymce.majorVersion >= 3) {
            tinymce.execCommand("mceRemoveControl", true, idTiny);
            tinymce.execCommand("mceAddControl", true, idTiny);
    for (var i = tinyMCE.editors.length - 1; i > -1; i--) {
        let idTiny = tinyMCE.editors[i].id;
        if (tinyMCE.majorVersion >= 4) {
            tinyMCE.execCommand("mceRemoveEditor", true, idTiny);
            tinyMCE.execCommand("mceAddEditor", true, idTiny);
        } else if (tinyMCE.majorVersion >= 3) {
            //tinyMCE.editors[i].onInit.add(TinyOnEditor);
            tinyMCE.execCommand("mceRemoveControl", true, idTiny);
            tinyMCE.execCommand("mceAddControl", true, idTiny);
        }
        //tinymce.settings = old_global_settings;
        //tinyMCE.settings = old_global_settings;
    }
}

/* // ! In the webpage script :
document.addEventListener('GrammalecteIsLoaded', function() {
    // Le gestionnaire d'évènement est prêt!
    // La page web peut effectuer des actions

Modified gc_lang/fr/webext/content_scripts/init.js from [bf6ff940d5] to [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("") });