Grammalecte  Check-in [b1dd1ce191]

Overview
Comment:[fx] some code cleaning + another test
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fx | webext_sharedworker
Files: files | file ages | folders
SHA3-256: b1dd1ce191b3b31d131c13d5d2b2202c9473846443ca82b636a0b99d7ef69097
User & Date: olr on 2017-08-11 06:05:45
Other Links: branch diff | manifest | tags
Context
2017-08-11
07:15
[fx] rename communicate check-in: cd64de6f5a user: olr tags: fx, webext_sharedworker
06:05
[fx] some code cleaning + another test check-in: b1dd1ce191 user: olr tags: fx, webext_sharedworker
2017-08-10
21:20
[fx] Use only the Sharedworker (cleanup code in background) check-in: db25c98bc2 user: IllusionPerdu tags: fx, webext_sharedworker
Changes

Modified gc_lang/fr/webext/content_scripts/comunicate.js from [9a6ca194be] to [f425809667].

1
2
3
4
5
6
7
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
let xGCEWorker = null;

console.log('[Iframe] Loaded');

/*
    Worker (separate thread to avoid freezing Firefox)
*/
function createSharedworker (sPath) {
    try {
        xGCEWorker = new SharedWorker(sPath+"gce_sharedworker.js");
    }
    catch (e) {
        console.error(e);
    }

    xGCEWorker.onerror = function(e) {
        console.log('There is an error with your worker!');
        console.log(typeof(e));
        console.log(e);
        for (let sParam in e) {
            console.log(sParam);
            console.log(e.sParam);
        }
    }

    xGCEWorker.port.onmessage = function (e) {
        // https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
        try {
            //On retransmet directement le message à la page
            console.log("[Iframe] send from Message Worker");
            window.postMessage({SharedWorker: e.data}, sPath );
        }
        catch (e) {
            console.error(e);
        }
    };

    console.log("[Iframe] [worker]");
    console.log(xGCEWorker);


    //xGCEWorker.port.start();
    //console.log("Content script [port started]");

    //xGCEWorker.port.postMessage(["init", {sExtensionPath: browser.extension.getURL("."), sOptions: "", sContext: "Firefox"}]);

    console.log("[Iframe] [worker initialzed]");

    //xGCEWorker.port.postMessage(["parse", {sText: "Vas... J’en aie mare...", sCountry: "FR", bDebug: false, bContext: false}]);
    //xGCEWorker.port.postMessage(["parseAndSpellcheck", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]);
    //xGCEWorker.port.postMessage(["getListOfTokens", {sText: oRequest.sText}]);
    //xGCEWorker.port.postMessage(["textToTest", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]);
    //xGCEWorker.port.postMessage(["fullTests"]);
}


var savePathExtension = '';
var savePathOrigine = '';
console.log('[Iframe] Set receivedMessageWeb');
function receivedMessageWeb (oEvent) {
    //Pour être sure que ca viens bien de notre iframe ;)
    if ( !xGCEWorker && typeof oEvent.data.sPath !== "undefined" && typeof oEvent.data.sPage !== "undefined" && oEvent.data.sPage === oEvent.origin ){
        console.log('[Iframe] Create the Sharedworker ', oEvent.origin);
        savePathExtension = oEvent.data.sPath;
        savePathOrigine = oEvent.origin;
        //On créer le Shraredworker
        createSharedworker(savePathExtension);
        //On initialise le Shraredworker
        xGCEWorker.port.postMessage(["init", {sExtensionPath: savePathExtension, sOptions: "", sContext: "Firefox"}]);

    } else if ( xGCEWorker && savePathOrigine === oEvent.origin && typeof oEvent.data.SharedWorker === "undefined") {
        console.log('[Iframe] received (no Sharedworker):', oEvent, savePathExtension, savePathOrigine, oEvent.origin);
        //Les messages reçus maintenant, on un Sharedworker fonctionnel 
        //On transmet au Sharedworker uniquement si ça viens bien de la page web et on s'assure que c'est pas une réponse du Sharedworker
        //TODO: Fodrait établir un protocol de communication afin qu'on ne traite vraiment que les messages a transmettre util ;)


        console.log('[Iframe] exec command with Sharedworker');
        xGCEWorker.port.postMessage(oEvent.data);
        console.log('[Iframe] end send message to worker');
    }
}

window.addEventListener("message", receivedMessageWeb, false);


console.log('[Iframe] END');







|











<
<
<
<





|

|









<



|
<
<
<








<
|


|
|

<
|
<
<
<
<
>
|
|
|
<
<
>
>
|




>


<
|
1
2
3
4
5
6
7
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
let xGCEWorker = null;

console.log('[Iframe] Loaded');

/*
    Worker (separate thread to avoid freezing Firefox)
*/
function createSharedWorker (sPath) {
    try {
        xGCEWorker = new SharedWorker(sPath+"gce_sharedworker.js");
    }
    catch (e) {
        console.error(e);
    }

    xGCEWorker.onerror = function(e) {
        console.log('There is an error with your worker!');
        console.log(typeof(e));
        console.log(e);




    }

    xGCEWorker.port.onmessage = function (e) {
        // https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
        try {
            // On retransmet directement le message à la page
            console.log("[Iframe] send from Message Worker");
            window.postMessage({SharedWorker: e.data}, sPath);
        }
        catch (e) {
            console.error(e);
        }
    };

    console.log("[Iframe] [worker]");
    console.log(xGCEWorker);


    //xGCEWorker.port.start();
    //console.log("Content script [port started]");

    xGCEWorker.port.postMessage(["init", {sExtensionPath: sPath, sOptions: "", sContext: "Firefox"}]);



    //xGCEWorker.port.postMessage(["parse", {sText: "Vas... J’en aie mare...", sCountry: "FR", bDebug: false, bContext: false}]);
    //xGCEWorker.port.postMessage(["parseAndSpellcheck", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]);
    //xGCEWorker.port.postMessage(["getListOfTokens", {sText: oRequest.sText}]);
    //xGCEWorker.port.postMessage(["textToTest", {sText: oRequest.sText, sCountry: "FR", bDebug: false, bContext: false}]);
    //xGCEWorker.port.postMessage(["fullTests"]);
}



var sPathOrigin = '';
console.log('[Iframe] Set receivedMessageWeb');
function receivedMessageWeb (oEvent) {
    // Pour être sûr que ça vient bien de notre iframe ;)
    if (!xGCEWorker && typeof oEvent.data.sPath !== "undefined" && typeof oEvent.data.sPage !== "undefined" && oEvent.data.sPage === oEvent.origin) {
        console.log('[Iframe] Create the Sharedworker ', oEvent.origin);

        sPathOrigin = oEvent.origin;




        createSharedWorker(oEvent.data.sPath);
    } else if (xGCEWorker && sPathOrigin === oEvent.origin && typeof oEvent.data.SharedWorker === "undefined") {
        console.log('[Iframe] received (no SharedWorker):', oEvent, oEvent.origin);
        // Les messages reçus maintenant ont un SharedWorker fonctionnel 


        // On transmet au SharedWorker uniquement si ça vient de la page web et on s’assure que ce n’est pas une réponse du SharedWorker.
        // TODO: Établir un protocole de communication afin de traiter uniquement les messages utiles
        console.log('[Iframe] exec command with SharedWorker');
        xGCEWorker.port.postMessage(oEvent.data);
        console.log('[Iframe] end send message to worker');
    }
}

window.addEventListener("message", receivedMessageWeb, false);


console.log('[Iframe] END');

Modified gc_lang/fr/webext/content_scripts/modify_page.js from [64fb970984] to [6c7b38de44].

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
..
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}

console.log("Content script [start]");

/*
* Pour effectuer différent action sur la page en cours
*/
function receivedMessageIframe (oEvent) {
    if ( typeof oEvent.data.SharedWorker !== "undefined" ) {
        //C'est ici que les action devront être effectuées
        console.log('[Web] received (from iframe (Sharedworker)):', oEvent);










    }    
}

/*
* Creation d'une iframe pour communiquer entre la page visitée et le Shareworker
*/
var sFrameID = browser.extension.getURL("").split('/')[2];
var iframe = document.createElement('iframe');
iframe.id = sFrameID;
iframe.src = browser.extension.getURL('content_scripts/comunicate.html');
iframe.hidden = true;
iframe.onload= function() {
    console.log('[Web] Init protocol de communication');
    //var iframeContent = iframe.contentWindow;
    var iframeContent = document.getElementById(sFrameID).contentWindow;
    iframeContent.addEventListener("message", receivedMessageIframe, false);

    try {
        //La frame est chargé on envoie l'initialisation du Sharedworker
        console.log('[Web] Initialise the worker :s');
        console.log('[Web] Domaine ext: '+browser.extension.getURL(""));
        iframeContent.postMessage({sPath:browser.extension.getURL(""), sPage:location.origin.trim("/")}, browser.extension.getURL("") );
   

        //Un petit test pour débogage ;)
        console.log('[Web] Test the worker :s');
        iframeContent.postMessage(["parse", {sText: "Vas... J’en aie mare...", sCountry: "FR", bDebug: false, bContext: false}], browser.extension.getURL(""));
    }
    catch (e) {
        console.error(e);
    }
}
document.body.appendChild(iframe);

/*
* Bidoule pour les les images du module et les intégrès sur la page
*/

function loadImage(sContennaireID, sImagePath){
    let xRequest;
    xRequest = new XMLHttpRequest();
    xRequest.open('GET', browser.extension.getURL("")+sImagePath, false);
    xRequest.responseType = "arraybuffer";
    xRequest.send();

    let blobTxt = new Blob([xRequest.response], {type: 'image/png'});
    let img = document.createElement('img');
    img.src = (URL || webkitURL).createObjectURL(blobTxt);

    Array.filter( document.getElementsByClassName(sContennaireID), function(oElment){
        oElment.appendChild(img);
    });     
}


console.log('[Web] La suite des initialisations');

let nWrapper = 0;
let xConjPanel = null;
let xTFPanel = null;
let xLxgPanel = null;
................................................................................
        xWrapper.style = "padding: 5px; color: hsl(210, 10%, 90%); background-color: hsl(210, 50%, 50%); border-radius: 3px;";
        xWrapper.id = nWrapper + 1;
        nWrapper += 1;
        xParentElement.insertBefore(xWrapper, xTextArea);
        xWrapper.appendChild(xTextArea); // move textarea in wrapper
        let xToolbar = createWrapperToolbar(xTextArea);
        xWrapper.appendChild(xToolbar);

        loadImage("GrammalecteTitle", "img/logo-16.png");
    }
    catch (e) {
        showError(e);
    }
}








|
|
<

>
>
>
>
>
>
>
>
>
>







|
|
|
|
|

|
|
|
<




|

<


|





|

<
<
<
>
|





<



<
|
|


>







 







<







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
..
95
96
97
98
99
100
101

102
103
104
105
106
107
108
}

console.log("Content script [start]");

/*
* Pour effectuer différent action sur la page en cours
*/
function receivedMessageFromIframe (oEvent) {
    if (typeof oEvent.data.SharedWorker !== "undefined") {

        console.log('[Web] received (from iframe (Sharedworker)):', oEvent);
        let [sCommand, answer] = oEvent.data.SharedWorker;
        console.log(sCommand);
        switch (sCommand) {
            case "grammar_errors":
                console.log(answer.aGrammErr);
                for (let oErr of answer.aGrammErr) {
                    console.log(oErr);
                }
                break;
        }
    }    
}

/*
* Creation d'une iframe pour communiquer entre la page visitée et le Shareworker
*/
var sFrameID = browser.extension.getURL("").split('/')[2];
var xIframe = document.createElement('iframe');
xIframe.id = sFrameID;
xIframe.src = browser.extension.getURL('content_scripts/comunicate.html');
xIframe.hidden = true;
xIframe.onload= function () {
    console.log('[Web] Init protocol de communication');
    //var xFrameContent = xIframe.contentWindow;
    var xFrameContent = document.getElementById(sFrameID).contentWindow;
    xFrameContent.addEventListener("message", receivedMessageFromIframe, false);

    try {
        //La frame est chargé on envoie l'initialisation du Sharedworker
        console.log('[Web] Initialise the worker :s');
        console.log('[Web] Domaine ext: '+browser.extension.getURL(""));
        xFrameContent.postMessage({sPath: browser.extension.getURL(""), sPage: location.origin.trim("/")}, browser.extension.getURL(""));
   

        //Un petit test pour débogage ;)
        console.log('[Web] Test the worker :s');
        xFrameContent.postMessage(["parse", {sText: "Vas... J’en aie mare...", sCountry: "FR", bDebug: false, bContext: false}], browser.extension.getURL(""));
    }
    catch (e) {
        console.error(e);
    }
}
document.body.appendChild(xIframe);





function loadImage (sContainerClass, sImagePath) {
    let xRequest;
    xRequest = new XMLHttpRequest();
    xRequest.open('GET', browser.extension.getURL("")+sImagePath, false);
    xRequest.responseType = "arraybuffer";
    xRequest.send();

    let blobTxt = new Blob([xRequest.response], {type: 'image/png'});
    let img = document.createElement('img');
    img.src = (URL || webkitURL).createObjectURL(blobTxt);

    Array.filter(document.getElementsByClassName(sContainerClass), function (oElem) {
        oElem.appendChild(img);
    });     
}


console.log('[Web] La suite des initialisations');

let nWrapper = 0;
let xConjPanel = null;
let xTFPanel = null;
let xLxgPanel = null;
................................................................................
        xWrapper.style = "padding: 5px; color: hsl(210, 10%, 90%); background-color: hsl(210, 50%, 50%); border-radius: 3px;";
        xWrapper.id = nWrapper + 1;
        nWrapper += 1;
        xParentElement.insertBefore(xWrapper, xTextArea);
        xWrapper.appendChild(xTextArea); // move textarea in wrapper
        let xToolbar = createWrapperToolbar(xTextArea);
        xWrapper.appendChild(xToolbar);

        loadImage("GrammalecteTitle", "img/logo-16.png");
    }
    catch (e) {
        showError(e);
    }
}