Grammalecte  Check-in [8c4e3dec4c]

Overview
Comment:[fx] grammar checking options
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fx | webext2
Files: files | file ages | folders
SHA3-256: 8c4e3dec4c44a022655f32bdaf13106f0b21943e6975dea80004a04645c7f0ae
User & Date: olr on 2017-08-28 14:45:33
Other Links: branch diff | manifest | tags
Context
2017-08-28
15:35
[fx] wrapper CSS update check-in: 4414e3913f user: olr tags: fx, webext2
14:45
[fx] grammar checking options check-in: 8c4e3dec4c user: olr tags: fx, webext2
2017-08-27
09:19
[fx] main panel ui adjustment check-in: 8c33d83d25 user: olr tags: fx, webext2
Changes

Modified gc_lang/fr/build.py from [3334f6b510] to [677a5b04fd].

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def _createOptionsForWebExtension (dVars):
    sHTML = ""
    sLang = dVars['sDefaultUILang']
    for sSection, lOpt in dVars['lStructOpt']:
        sHTML += f'\n<div id="subsection_{sSection}" class="opt_subsection">\n  <h2 data-l10n-id="option_{sSection}">{dVars["dOptLabel"][sLang][sSection][0]}</h2>\n'
        for lLineOpt in lOpt:
            for sOpt in lLineOpt:
                sHTML += f'  <p><input type="checkbox" id="option_{sOpt}" /><label id="option_label_{sOpt}" for="option_{sOpt}" data-l10n-id="option_{sOpt}">{dVars["dOptLabel"][sLang][sOpt][0]}</label></p>\n'
        sHTML += '</div>\n'
    return sHTML


def createFirefoxExtension (sLang, dVars):
    "create extension for Firefox"
    print("Building extension for Firefox")







|







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def _createOptionsForWebExtension (dVars):
    sHTML = ""
    sLang = dVars['sDefaultUILang']
    for sSection, lOpt in dVars['lStructOpt']:
        sHTML += f'\n<div id="subsection_{sSection}" class="opt_subsection">\n  <h2 data-l10n-id="option_{sSection}">{dVars["dOptLabel"][sLang][sSection][0]}</h2>\n'
        for lLineOpt in lOpt:
            for sOpt in lLineOpt:
                sHTML += f'  <p><input type="checkbox" id="option_{sOpt}" data-option="{sOpt}"/><label id="option_label_{sOpt}" for="option_{sOpt}" data-l10n-id="option_{sOpt}">{dVars["dOptLabel"][sLang][sOpt][0]}</label></p>\n'
        sHTML += '</div>\n'
    return sHTML


def createFirefoxExtension (sLang, dVars):
    "create extension for Firefox"
    print("Building extension for Firefox")

Modified gc_lang/fr/webext/background.js from [c04d704430] to [32e8b61dc7].

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

xGCEWorker.onmessage = function (e) {
    // https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
    try {
        let {sActionDone, result, dInfo} = e.data;
        switch (sActionDone) {
            case "init":

                break;
            case "parse":
            case "parseAndSpellcheck":
            case "parseAndSpellcheck1":
            case "getListOfTokens":

                if (typeof(dInfo.iReturnPort) === "number") {
                    let xPort = dConnx.get(dInfo.iReturnPort);
                    xPort.postMessage(e.data);
                } else {
                    console.log("[background] don’t know where to send results");
                    console.log(e.data);
                }
                break;
            case "textToTest":
                browser.runtime.sendMessage({sCommand: "text_to_test_result", sResult: result});
                break;
            case "fullTests":
                browser.runtime.sendMessage({sCommand: "fulltests_result", sResult: result});
                break;
            case "getOptions":
            case "getDefaultOptions":



            case "setOptions":
            case "setOption":
                console.log("OPTIONS");

                break;
            default:
                console.log("Unknown command: " + sActionDone);
                console.log(result);
        }
    }
    catch (e) {
        showError(e);
    }
};

















xGCEWorker.postMessage({sCommand: "init", dParam: {sExtensionPath: browser.extension.getURL("."), sOptions: "", sContext: "Firefox"}, dInfo: {}});









/*
    Ports from content-scripts
*/

let dConnx = new Map();







>





>









<
<

<
<


>
>
>


<
>












>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>







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

xGCEWorker.onmessage = function (e) {
    // https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
    try {
        let {sActionDone, result, dInfo} = e.data;
        switch (sActionDone) {
            case "init":
                browser.storage.local.set({"gc_options": result});
                break;
            case "parse":
            case "parseAndSpellcheck":
            case "parseAndSpellcheck1":
            case "getListOfTokens":
                // send result to content script
                if (typeof(dInfo.iReturnPort) === "number") {
                    let xPort = dConnx.get(dInfo.iReturnPort);
                    xPort.postMessage(e.data);
                } else {
                    console.log("[background] don’t know where to send results");
                    console.log(e.data);
                }
                break;
            case "textToTest":


            case "fullTests":


            case "getOptions":
            case "getDefaultOptions":
                // send result to panel
                browser.runtime.sendMessage(e.data);
                break;
            case "setOptions":
            case "setOption":

                browser.storage.local.set({"gc_options": result});
                break;
            default:
                console.log("Unknown command: " + sActionDone);
                console.log(result);
        }
    }
    catch (e) {
        showError(e);
    }
};


function init () {
    let xPromise = browser.storage.local.get("gc_options");
    xPromise.then(
        function (dSavedOptions) {
            let dOptions = (dSavedOptions.hasOwnProperty("gc_options")) ? dSavedOptions.gc_options : null;
            xGCEWorker.postMessage({
                sCommand: "init",
                dParam: {sExtensionPath: browser.extension.getURL("."), dOptions: dOptions, sContext: "Firefox"},
                dInfo: {}
            });
        },
        function (e) {
            showError(e);
            xGCEWorker.postMessage({
                sCommand: "init",
                dParam: {sExtensionPath: browser.extension.getURL("."), dOptions: null, sContext: "Firefox"},
                dInfo: {}
            });
        }
    );
}

init();


/*
    Ports from content-scripts
*/

let dConnx = new Map();

Modified gc_lang/fr/webext/gce_worker.js from [1a2b4f786b] to [615fe67e99].

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    This worker don’t work as a PromiseWorker (which returns a promise),  so when we send request
    to this worker, we can’t wait the return of the answer just after the request made.
    The answer is received by the background in another function (onmessage).
    That’s why the full text to analyze is send in one block, but analyse is returned paragraph
    by paragraph.
*/

function init (sExtensionPath, sGCOptions="", sContext="JavaScript", dInfo={}) {
    try {
        if (!bInitDone) {
            //console.log("[Worker] Loading… Extension path: " + sExtensionPath);
            conj.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/conj_data.json"));
            phonet.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/phonet_data.json"));
            mfsp.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/mfsp_data.json"));
            //console.log("[Worker] Modules have been initialized…");
            gc_engine.load(sContext, sExtensionPath+"grammalecte/_dictionaries");
            oDict = gc_engine.getDictionary();
            oTest = new TestGrammarChecking(gc_engine, sExtensionPath+"/grammalecte/fr/tests_data.json");
            oLxg = new Lexicographe(oDict);
            if (sGCOptions !== "") {
                gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
            }
            oTokenizer = new Tokenizer("fr");
            //tests();
            bInitDone = true;
        } else {
            console.log("[Worker] Already initialized…")
        }
        // we always retrieve options from the gc_engine, for setOptions filters obsolete options
        postMessage(createResponse("init", gc_engine.getOptions().gl_toString(), dInfo, true));
    }
    catch (e) {
        helpers.logerror(e);
        postMessage(createResponse("init", createErrorResult(e, "init failed"), dInfo, true, true));
    }
}








|











|
|








|







143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    This worker don’t work as a PromiseWorker (which returns a promise),  so when we send request
    to this worker, we can’t wait the return of the answer just after the request made.
    The answer is received by the background in another function (onmessage).
    That’s why the full text to analyze is send in one block, but analyse is returned paragraph
    by paragraph.
*/

function init (sExtensionPath, dOptions=null, sContext="JavaScript", dInfo={}) {
    try {
        if (!bInitDone) {
            //console.log("[Worker] Loading… Extension path: " + sExtensionPath);
            conj.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/conj_data.json"));
            phonet.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/phonet_data.json"));
            mfsp.init(helpers.loadFile(sExtensionPath + "/grammalecte/fr/mfsp_data.json"));
            //console.log("[Worker] Modules have been initialized…");
            gc_engine.load(sContext, sExtensionPath+"grammalecte/_dictionaries");
            oDict = gc_engine.getDictionary();
            oTest = new TestGrammarChecking(gc_engine, sExtensionPath+"/grammalecte/fr/tests_data.json");
            oLxg = new Lexicographe(oDict);
            if (dOptions !== null) {
                gc_engine.setOptions(dOptions);
            }
            oTokenizer = new Tokenizer("fr");
            //tests();
            bInitDone = true;
        } else {
            console.log("[Worker] Already initialized…")
        }
        // we always retrieve options from the gc_engine, for setOptions filters obsolete options
        postMessage(createResponse("init", gc_engine.getOptions(), dInfo, true));
    }
    catch (e) {
        helpers.logerror(e);
        postMessage(createResponse("init", createErrorResult(e, "init failed"), dInfo, true, true));
    }
}

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

220
221
222
223
224
225
226
227
228
229
230
231
232
233
function parseAndSpellcheck1 (sParagraph, sCountry, bDebug, bContext, dInfo={}) {
    let aGrammErr = gc_engine.parse(sParagraph, sCountry, bDebug, bContext);
    let aSpellErr = oTokenizer.getSpellingErrors(sParagraph, oDict);
    postMessage(createResponse("parseAndSpellcheck1", {sParagraph: sParagraph, aGrammErr: aGrammErr, aSpellErr: aSpellErr}, dInfo, true));
}

function getOptions (dInfo={}) {
    postMessage(createResponse("getOptions", gc_engine.getOptions().gl_toString(), dInfo, true));
}

function getDefaultOptions (dInfo={}) {
    postMessage(createResponse("getDefaultOptions", gc_engine.getDefaultOptions().gl_toString(), dInfo, true));
}

function setOptions (sGCOptions, dInfo={}) {
    gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
    postMessage(createResponse("setOptions", gc_engine.getOptions().gl_toString(), dInfo, true));
}

function setOption (sOptName, bValue, dInfo={}) {

    gc_engine.setOptions(new Map([ [sOptName, bValue] ]));
    postMessage(createResponse("setOption", gc_engine.getOptions().gl_toString(), dInfo, true));
}

function resetOptions (dInfo={}) {
    gc_engine.resetOptions();
    postMessage(createResponse("resetOptions", gc_engine.getOptions().gl_toString(), dInfo, true));
}

function tests () {
    console.log(conj.getConj("devenir", ":E", ":2s"));
    console.log(mfsp.getMasForm("emmerdeuse", true));
    console.log(mfsp.getMasForm("pointilleuse", false));
    console.log(phonet.getSimil("est"));







|



|


|
|
|



>

|




|







200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
function parseAndSpellcheck1 (sParagraph, sCountry, bDebug, bContext, dInfo={}) {
    let aGrammErr = gc_engine.parse(sParagraph, sCountry, bDebug, bContext);
    let aSpellErr = oTokenizer.getSpellingErrors(sParagraph, oDict);
    postMessage(createResponse("parseAndSpellcheck1", {sParagraph: sParagraph, aGrammErr: aGrammErr, aSpellErr: aSpellErr}, dInfo, true));
}

function getOptions (dInfo={}) {
    postMessage(createResponse("getOptions", gc_engine.getOptions(), dInfo, true));
}

function getDefaultOptions (dInfo={}) {
    postMessage(createResponse("getDefaultOptions", gc_engine.getDefaultOptions(), dInfo, true));
}

function setOptions (dOptions, dInfo={}) {
    gc_engine.setOptions(dOptions);
    postMessage(createResponse("setOptions", gc_engine.getOptions(), dInfo, true));
}

function setOption (sOptName, bValue, dInfo={}) {
    console.log(sOptName+": "+bValue);
    gc_engine.setOptions(new Map([ [sOptName, bValue] ]));
    postMessage(createResponse("setOption", gc_engine.getOptions(), dInfo, true));
}

function resetOptions (dInfo={}) {
    gc_engine.resetOptions();
    postMessage(createResponse("resetOptions", gc_engine.getOptions(), dInfo, true));
}

function tests () {
    console.log(conj.getConj("devenir", ":E", ":2s"));
    console.log(mfsp.getMasForm("emmerdeuse", true));
    console.log(mfsp.getMasForm("pointilleuse", false));
    console.log(phonet.getSimil("est"));

Modified gc_lang/fr/webext/panel/main.js from [40cb36fd8a] to [bc8475212d].

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
// Main panel

"use strict";

/*
    Common functions
*/
function showError (e) {
    console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
}

function showPage (sPageName) {
    try {
        // hide them all
        for (let xNodePage of document.getElementsByClassName("page")) {
            xNodePage.style.display = "none";
        }
        // show the selected one
        document.getElementById(sPageName).style.display = "block";
    }
    catch (e) {
        showError(e);
    }
}

function startWaitIcon () {
    document.getElementById("waiticon").hidden = false;
}

function stopWaitIcon () {
    document.getElementById("waiticon").hidden = true;
}


/*
    Events
*/
window.addEventListener(
    "click",
    function (xEvent) {
        let xElem = xEvent.target;
        if (xElem.id) {
            switch (xElem.id) {

                case "text_to_test":
                    browser.runtime.sendMessage({sCommand: "textToTest", dParam: {sText: document.getElementById("text_to_test").value, sCountry: "FR", bDebug: false, bContext: false}, dInfo: {}});
                    break;



                case "fulltests":
                    document.getElementById("tests_result").textContent = "Veuillez patienter…";
                    browser.runtime.sendMessage({sCommand: "fullTests", dParam: {}, dInfo: {}});

                    break;









            }
        } else if (xElem.className.startsWith("select")) {
            showPage(xElem.dataset.page);
        }/* else if (xElem.tagName === "A") {
            openURL(xElem.getAttribute("href"));
        }*/
    },




|
<
<




<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<









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







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
// Main panel

"use strict";




function showError (e) {
    console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
}
























/*
    Events
*/
window.addEventListener(
    "click",
    function (xEvent) {
        let xElem = xEvent.target;
        if (xElem.id) {
            if (xElem.id === "text_to_test") {
                browser.runtime.sendMessage({
                    sCommand: "textToTest",
                    dParam: {sText: document.getElementById("text_to_test").value, sCountry: "FR", bDebug: false, bContext: false},

                    dInfo: {}
                });
            }
            else if (xElem.id === "fulltests") {
                document.getElementById("tests_result").textContent = "Veuillez patienter…";
                browser.runtime.sendMessage({
                    sCommand: "fullTests",
                    dParam: {},
                    dInfo: {}
                });
            }
            else if (xElem.id.startsWith("option_")) {
                browser.runtime.sendMessage({
                    sCommand: "setOption",
                    dParam: {sOptName: xElem.dataset.option, bValue: xElem.checked},
                    dInfo: {}
                });
            }
        } else if (xElem.className.startsWith("select")) {
            showPage(xElem.dataset.page);
        }/* else if (xElem.tagName === "A") {
            openURL(xElem.getAttribute("href"));
        }*/
    },
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



















}


/*
    Messages received
*/
function handleMessage (oMessage, xSender, sendResponse) {

    switch(oMessage.sCommand) {
        case "text_to_test_result":

            showTestResult(oMessage.sResult);
            break;
        case "fulltests_result":
            showTestResult(oMessage.sResult);
            break;


    }
    sendResponse({sCommand: "none", sResult: "done"});
}

browser.runtime.onMessage.addListener(handleMessage);


/*



    DEDICATED FUNCTIONS 






*/










/*
    Test page
*/
function showTestResult (sText) {
    document.getElementById("tests_result").textContent = sText;
}


























>
|
|
>
|

|
|

>
>

|






>
>

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



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
132
133
134
135
136
}


/*
    Messages received
*/
function handleMessage (oMessage, xSender, sendResponse) {
    let {sActionDone, result, dInfo, bEnd, bError} = oMessage;
    switch(sActionDone) {
        case "textToTest":
        case "fullTests":
            showTestResult(result);
            break;
        case "getOptions":
        case "getDefaultOptions":
            break;
        default:
            console.log("GRAMMALECTE. Unknown command: " + oMessage.sCommand);
    }
    sendResponse({sCommand: "none", result: "done"});
}

browser.runtime.onMessage.addListener(handleMessage);


/*
    Actions
*/


function showPage (sPageName) {
    try {
        // hide them all
        for (let xNodePage of document.getElementsByClassName("page")) {
            xNodePage.style.display = "none";
        }

        // show the selected one
        document.getElementById(sPageName).style.display = "block";
        if (sPageName == "gc_options_page") {
            setGCOptions();
        }
    }
    catch (e) {
        showError(e);
    }
}



function showTestResult (sText) {
    document.getElementById("tests_result").textContent = sText;
}

function setGCOptions () {
    let xPromise = browser.storage.local.get("gc_options");
    xPromise.then(
        function (dSavedOptions) {
            console.log(dSavedOptions);
            if (dSavedOptions.hasOwnProperty("gc_options")) {
                for (let [sOpt, bVal] of dSavedOptions.gc_options) {
                    if (document.getElementById("option_"+sOpt)) {
                        document.getElementById("option_"+sOpt).checked = bVal;
                    }
                }
            }
        },
        function (e) {
            showError(e);
        }
    );
}