Grammalecte  Check-in [c775552b47]

Overview
Comment:[fx] API for web pages and context menu for iframes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | fx
Files: files | file ages | folders
SHA3-256: c775552b47b5d24dc69d3ee28c3854c37f34a5a7736344bfd427130793171caa
User & Date: olr on 2020-03-11 20:29:34
Other Links: manifest | tags
Context
2020-03-11
21:44
[fr] faux positif check-in: 607796fcdc user: olr tags: trunk, fr
20:29
[fx] API for web pages and context menu for iframes check-in: c775552b47 user: olr tags: trunk, fx
2020-03-10
07:01
[fx] send result via event only with the parameter grammalecte_result_via_event check-in: 981544fa30 user: olr tags: trunk, fx
Changes

Modified gc_lang/fr/webext/background.js from [abadc3982d] to [b8162f6122].

329
330
331
332
333
334
335
336

337
338
339
340
341
342
343


/*
    Context Menu
*/
// Analyze
browser.contextMenus.create({ id: "grammar_checker_editable",   title: "Analyser cette zone de texte",              contexts: ["editable"] });
browser.contextMenus.create({ id: "grammar_checker_selection",   title: "Analyser la sélection",                     contexts: ["selection"] });

browser.contextMenus.create({ id: "grammar_checker_page",       title: "Analyser la page",                          contexts: ["all"] });
browser.contextMenus.create({ id: "separator_tools",            type: "separator",                                  contexts: ["all"] });
// Tools
browser.contextMenus.create({ id: "conjugueur_tab",             title: "Conjugueur [onglet]",                       contexts: ["all"] });
browser.contextMenus.create({ id: "conjugueur_window",          title: "Conjugueur [fenêtre]",                      contexts: ["all"] });
//browser.contextMenus.create({ id: "dictionaries",               title: "Dictionnaires",                             contexts: ["all"] });
browser.contextMenus.create({ id: "lexicon_editor",             title: "Éditeur lexical",                           contexts: ["all"] });







|
>







329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344


/*
    Context Menu
*/
// Analyze
browser.contextMenus.create({ id: "grammar_checker_editable",   title: "Analyser cette zone de texte",              contexts: ["editable"] });
browser.contextMenus.create({ id: "grammar_checker_selection",  title: "Analyser la sélection",                     contexts: ["selection"] });
browser.contextMenus.create({ id: "grammar_checker_iframe",     title: "Analyser le contenu de ce cadre",           contexts: ["frame"] });
browser.contextMenus.create({ id: "grammar_checker_page",       title: "Analyser la page",                          contexts: ["all"] });
browser.contextMenus.create({ id: "separator_tools",            type: "separator",                                  contexts: ["all"] });
// Tools
browser.contextMenus.create({ id: "conjugueur_tab",             title: "Conjugueur [onglet]",                       contexts: ["all"] });
browser.contextMenus.create({ id: "conjugueur_window",          title: "Conjugueur [fenêtre]",                      contexts: ["all"] });
//browser.contextMenus.create({ id: "dictionaries",               title: "Dictionnaires",                             contexts: ["all"] });
browser.contextMenus.create({ id: "lexicon_editor",             title: "Éditeur lexical",                           contexts: ["all"] });
352
353
354
355
356
357
358



359
360
361
362
363
364
365
    // confusing: no way to get the node where we click?!
    switch (xInfo.menuItemId) {
        // analyze
        case "grammar_checker_editable":
        case "grammar_checker_page":
            sendCommandToTab(xTab.id, xInfo.menuItemId);
            break;



        case "grammar_checker_selection":
            sendCommandToTab(xTab.id, xInfo.menuItemId, xInfo.selectionText);
            oWorkerHandler.xGCEWorker.postMessage({
                sCommand: "parseAndSpellcheck",
                dParam: {sText: xInfo.selectionText, sCountry: "FR", bDebug: false, bContext: false},
                dInfo: {iReturnPort: xTab.id}
            });







>
>
>







353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
    // confusing: no way to get the node where we click?!
    switch (xInfo.menuItemId) {
        // analyze
        case "grammar_checker_editable":
        case "grammar_checker_page":
            sendCommandToTab(xTab.id, xInfo.menuItemId);
            break;
        case "grammar_checker_iframe":
            sendCommandToTab(xTab.id, xInfo.menuItemId, xInfo.frameId);
            break;
        case "grammar_checker_selection":
            sendCommandToTab(xTab.id, xInfo.menuItemId, xInfo.selectionText);
            oWorkerHandler.xGCEWorker.postMessage({
                sCommand: "parseAndSpellcheck",
                dParam: {sText: xInfo.selectionText, sCountry: "FR", bDebug: false, bContext: false},
                dInfo: {iReturnPort: xTab.id}
            });

Added gc_lang/fr/webext/content_scripts/api.js version [46315a7e69].

















































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
// JavaScript

"use strict";


const oGrammalecteAPI = {
    // functions callable from within pages

    sVersion: "1.0",

    parse: function (arg1) {
        let xNode = null;
        if (typeof(arg1) === 'string') {
            if (document.getElementById(arg1)) {
                xNode = document.getElementById(arg1);
            } else {
                this.parseText(arg1);
            }
        }
        else if (arg1 instanceof HTMLElement) {
            xNode = arg1;
        }
        if (xNode) {
            console.log("xnode");
            if (xNode.tagName == "INPUT"  ||  xNode.tagName == "TEXTAREA"  ||  xNode.isContentEditable) {
                this.parseNode(xNode);
            }
            else if (xNode.tagName == "IFRAME") {
                this.parseText(xNode.contentWindow.document.body.innerText);
            }
            else {
                this.parseText(xNode.innerText);
            }
        }
    },

    parseNode: function (xNode) {
        console.log("parseNode");
        if (xNode instanceof HTMLElement) {
            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseNode", xNode: xNode} });
            document.dispatchEvent(xEvent);
        } else {
            console.log("[Grammalecte API] Error: parameter is not a HTML node.");
        }
    },

    parseText: function (sText) {
        console.log("parseText");
        if (typeof(sText) === "string") {
            let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseText", sText: sText} });
            document.dispatchEvent(xEvent);
        } else {
            console.log("[Grammalecte API] Error: parameter is not a text.");
        }
    }
}

Modified gc_lang/fr/webext/content_scripts/init.js from [e1f7754754] to [83ad0c7397].

11
12
13
14
15
16
17

18
19
20
21
22
23
24
    No SharedWorker, no images allowed for now…
*/

"use strict";


function showError (e) {

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

// Chrome don’t follow the W3C specification:
// https://browserext.github.io/browserext/
let bChrome = false;
if (typeof(browser) !== "object") {







>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    No SharedWorker, no images allowed for now…
*/

"use strict";


function showError (e) {
    // console can’t display error objects from content scripts
    console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
}

// Chrome don’t follow the W3C specification:
// https://browserext.github.io/browserext/
let bChrome = false;
if (typeof(browser) !== "object") {
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
    },

    _prepareButtons: function (oOptions) {
        if (oOptions.hasOwnProperty("ui_options")) {
            this.oOptions = oOptions.ui_options;
            // textarea
            for (let xNode of document.getElementsByTagName("textarea")) {
                if (xNode.dataset.grammalecte_callbutton && document.getElementById(xNode.dataset.grammalecte_callbutton)) {
                    let xButton = document.getElementById(xNode.dataset.grammalecte_callbutton)
                    xButton.onclick = () => {
                        oGrammalecte.startGCPanel(xNode);
                    };
                    this.lButton.push(xButton);
                    this.nButton += 1;
                }
                else if (this.oOptions.textarea  &&  xNode.style.display !== "none" && xNode.style.visibility !== "hidden" && xNode.getAttribute("spellcheck") !== "false") {

                    this.lButton.push(new GrammalecteButton(this.nButton, xNode));
                    this.nButton += 1;
                }
            }
            // editable nodes
            for (let xNode of document.querySelectorAll("[contenteditable]")) {
                if (xNode.dataset.grammalecte_callbutton && document.getElementById(xNode.dataset.grammalecte_callbutton)) {
                    let xButton = document.getElementById(xNode.dataset.grammalecte_callbutton)
                    xButton.onclick = () => {
                        oGrammalecte.startGCPanel(xNode);
                    };
                    this.lButton.push(xButton);
                    this.nButton += 1;
                }
                else if (this.oOptions.editablenode  &&  xNode.style.display !== "none" && xNode.style.visibility !== "hidden") {

                    this.lButton.push(new GrammalecteButton(this.nButton, xNode));
                    this.nButton += 1;
                }
            }
        }
    },








<
<
<
<
<
<
<
<
|
>






<
<
<
<
<
<
<
<
|
>







85
86
87
88
89
90
91








92
93
94
95
96
97
98
99








100
101
102
103
104
105
106
107
108
    },

    _prepareButtons: function (oOptions) {
        if (oOptions.hasOwnProperty("ui_options")) {
            this.oOptions = oOptions.ui_options;
            // textarea
            for (let xNode of document.getElementsByTagName("textarea")) {








                if (this.oOptions.textarea  &&  xNode.style.display !== "none" && xNode.style.visibility !== "hidden" && xNode.getAttribute("spellcheck") !== "false"
                    && !(xNode.dataset.grammalecte_button  &&  xNode.dataset.grammalecte_button == "false")) {
                    this.lButton.push(new GrammalecteButton(this.nButton, xNode));
                    this.nButton += 1;
                }
            }
            // editable nodes
            for (let xNode of document.querySelectorAll("[contenteditable]")) {








                if (this.oOptions.editablenode  &&  xNode.style.display !== "none" && xNode.style.visibility !== "hidden"
                    && !(xNode.dataset.grammalecte_button  &&  xNode.dataset.grammalecte_button == "false")) {
                    this.lButton.push(new GrammalecteButton(this.nButton, xNode));
                    this.nButton += 1;
                }
            }
        }
    },

383
384
385
386
387
388
389









390
391
392
393
394
395
396
397
398



































399
400
401
402
403
404
405
            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;
        // rescan page command
        case "rescanPage":
            oGrammalecte.rescanPage();
            break;
        default:
            console.log("[Content script] Unknown command: " + sActionDone);
    }
});





































/*
    Other messages from background
*/
browser.runtime.onMessage.addListener(function (oMessage) {
    let {sActionRequest} = oMessage;







>
>
>
>
>
>
>
>
>









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
            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.contentWindow.document.body.innerText);
            } 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);
    }
});



/*
    Callable API for the webpage.
    The API script must be injected this way to be callable by the page
*/
let xScriptGrammalecteAPI = document.createElement("script");
xScriptGrammalecteAPI.src = browser.extension.getURL("content_scripts/api.js");
document.documentElement.appendChild(xScriptGrammalecteAPI);

document.addEventListener("GrammalecteCall", function (xEvent) {
    // GrammalecteCall events are dispatched by functions in the API
    try {
        let oCommand = xEvent.detail;
        switch (oCommand.sCommand) {
            case "parseNode":
                if (oCommand.xNode) {
                    oGrammalecte.startGCPanel(oCommand.xNode);
                }
                break;
            case "parseText":
                if (oCommand.sText) {
                    oGrammalecte.startGCPanel(oCommand.sText);
                }
                break;
            default:
                console.log("[Grammalecte] Event: Unknown command", oCommand.sCommand);
        }
    }
    catch (e) {
        showError(e);
    }
});



/*
    Other messages from background
*/
browser.runtime.onMessage.addListener(function (oMessage) {
    let {sActionRequest} = oMessage;

Modified gc_lang/fr/webext/manifest.json from [6de540cb7c] to [8dac256bfb].

91
92
93
94
95
96
97

98
99
100
101
102
103
104
    "lexicon_editor": {
      "suggested_key": { "default": "Ctrl+Shift+7" },
      "description": "Ouvre l’éditeur lexical"
    }
  },

  "web_accessible_resources": [

    "content_scripts/panel.css",
    "content_scripts/panel_tf.css",
    "content_scripts/panel_gc.css",
    "content_scripts/panel_lxg.css",
    "content_scripts/panel_conj.css",
    "content_scripts/message_box.css",
    "content_scripts/menu.css",







>







91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    "lexicon_editor": {
      "suggested_key": { "default": "Ctrl+Shift+7" },
      "description": "Ouvre l’éditeur lexical"
    }
  },

  "web_accessible_resources": [
    "content_scripts/api.js",
    "content_scripts/panel.css",
    "content_scripts/panel_tf.css",
    "content_scripts/panel_gc.css",
    "content_scripts/panel_lxg.css",
    "content_scripts/panel_conj.css",
    "content_scripts/message_box.css",
    "content_scripts/menu.css",