Grammalecte  Check-in [9bc8d9a943]

Overview
Comment:[fx] find the first editable node for context menu and shortcuts too
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | fx
Files: files | file ages | folders
SHA3-256: 9bc8d9a943ea31b919795eefde225830f22f64bc63219ca8b7a255b8235b3d43
User & Date: olr on 2020-04-09 07:04:54
Other Links: manifest | tags
Context
2020-04-09
11:10
[fr] ajustements check-in: 8075dc7ae3 user: olr tags: trunk, fr
07:04
[fx] find the first editable node for context menu and shortcuts too check-in: 9bc8d9a943 user: olr tags: trunk, fx
2020-04-08
20:12
[fx] button: node examination update check-in: f16babf55d user: olr tags: trunk, fx
Changes

Modified gc_lang/fr/webext/background.js from [6e8ab066e7] to [3958c7eb7d].

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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"] });
// Rescan page
browser.contextMenus.create({ id: "separator_rescan",           type: "separator",                                  contexts: ["editable"] });


browser.contextMenus.onClicked.addListener(function (xInfo, xTab) {
    // xInfo = https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextMenus/OnClickData
    // xTab = https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/Tab
    // confusing: no way to get the node where we click?!
    switch (xInfo.menuItemId) {







<
<







338
339
340
341
342
343
344


345
346
347
348
349
350
351
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"] });




browser.contextMenus.onClicked.addListener(function (xInfo, xTab) {
    // xInfo = https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextMenus/OnClickData
    // xTab = https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/Tab
    // confusing: no way to get the node where we click?!
    switch (xInfo.menuItemId) {

Modified gc_lang/fr/webext/content_scripts/init.js from [56313a38a6] to [d79ff895de].

170
171
172
173
174
175
176













177
178
179
180
181
182
183
            }
            return xNode;
        }
        catch (e) {
            showError(e);
        }
    },














    getCaretPosition: function (xElement) {
        // JS awfulness again.
        // recepie from https://stackoverflow.com/questions/4811822/get-a-ranges-start-and-end-offsets-relative-to-its-parent-container
        let nCaretOffsetStart = 0;
        let nCaretOffsetEnd = 0;
        let xSelection = window.getSelection();







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







170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
            }
            return xNode;
        }
        catch (e) {
            showError(e);
        }
    },

    findOriginEditableNode: function (xNode) {
        if (!xNode) {
            return null;
        }
        if (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT" || xNode.tagName == "IFRAME") {
            return xNode;
        }
        const findNode = function (xNode) {
            return (!xNode.parentNode.isContentEditable) ? xNode : findNode(xNode.parentNode);
        }
        return findNode(xNode);
    },

    getCaretPosition: function (xElement) {
        // JS awfulness again.
        // recepie from https://stackoverflow.com/questions/4811822/get-a-ranges-start-and-end-offsets-relative-to-its-parent-container
        let nCaretOffsetStart = 0;
        let nCaretOffsetEnd = 0;
        let xSelection = window.getSelection();
274
275
276
277
278
279
280







281
282
283
284
285
286
287
        Send messages to the background
        object {
            sCommand: the action to perform
            oParam: parameters necessary for the execution of the action
            oInfo: all kind of informations that needs to be sent back (usually to know where to use the result)
        }
    */







    parseAndSpellcheck: function (sText, sDestination) {
        this.xConnect.postMessage({
            sCommand: "parseAndSpellcheck",
            oParam: { sText: sText, sCountry: "FR", bDebug: false, bContext: false },
            oInfo: { sDestination: sDestination }
        });
    },







>
>
>
>
>
>
>







287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
        Send messages to the background
        object {
            sCommand: the action to perform
            oParam: parameters necessary for the execution of the action
            oInfo: all kind of informations that needs to be sent back (usually to know where to use the result)
        }
    */

    checkConnection: function () {
        if (!this.xConnect) {
            this.xConnect = browser.runtime.connect({name: "content-script port"});
        }
    },

    parseAndSpellcheck: function (sText, sDestination) {
        this.xConnect.postMessage({
            sCommand: "parseAndSpellcheck",
            oParam: { sText: sText, sCountry: "FR", bDebug: false, bContext: false },
            oInfo: { sDestination: sDestination }
        });
    },
396
397
398
399
400
401
402

403
404
405
406
407
408
409
410
                /*
                    Commands received from the context menu
                    (Context menu are initialized in background)
                */
                // Grammar checker commands
                case "grammar_checker_editable":
                    if (oGrammalecte.xRightClickedNode !== null) {

                        oGrammalecte.startGCPanel(oGrammalecte.xRightClickedNode);
                    } else {
                        oGrammalecte.showMessage("Erreur. Le node 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;
                case "grammar_checker_page":
                    oGrammalecte.startGCPanel(oGrammalecte.getPageText());
                    break;







>
|







416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
                /*
                    Commands received from the context menu
                    (Context menu are initialized in background)
                */
                // Grammar checker commands
                case "grammar_checker_editable":
                    if (oGrammalecte.xRightClickedNode !== null) {
                        let xNode = oGrammalecte.findOriginEditableNode(oGrammalecte.xRightClickedNode);
                        oGrammalecte.startGCPanel(xNode);
                    } else {
                        oGrammalecte.showMessage("Erreur. Le node 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;
                case "grammar_checker_page":
                    oGrammalecte.startGCPanel(oGrammalecte.getPageText());
                    break;
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

    /*
        Other messages from background
    */
    listen2: function () {
        browser.runtime.onMessage.addListener(function (oMessage) {
            let {sActionRequest} = oMessage;
            let xActiveNode = document.activeElement;
            switch (sActionRequest) {
                /*
                    Commands received from the keyboard (shortcuts)
                */
                case "shortcutGrammarChecker":
                    if (xActiveNode && (xActiveNode.tagName == "TEXTAREA" || xActiveNode.tagName == "INPUT" || xActiveNode.isContentEditable)) {
                        oGrammalecte.startGCPanel(xActiveNode);







|







449
450
451
452
453
454
455
456
457
458
459
460
461
462
463

    /*
        Other messages from background
    */
    listen2: function () {
        browser.runtime.onMessage.addListener(function (oMessage) {
            let {sActionRequest} = oMessage;
            let xActiveNode = oGrammalecte.findOriginEditableNode(document.activeElement);
            switch (sActionRequest) {
                /*
                    Commands received from the keyboard (shortcuts)
                */
                case "shortcutGrammarChecker":
                    if (xActiveNode && (xActiveNode.tagName == "TEXTAREA" || xActiveNode.tagName == "INPUT" || xActiveNode.isContentEditable)) {
                        oGrammalecte.startGCPanel(xActiveNode);

Modified gc_lang/fr/webext/content_scripts/menu.js from [56c56fae1e] to [60324e1838].

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
               || (xNode.tagName == "IFRAME" && this._bIframe) )
             && !(xNode.dataset.grammalecte_button  &&  xNode.dataset.grammalecte_button == "false") ) {
            // textarea or iframe
            this.accept(xNode)
        }
        else if (xNode.isContentEditable && this._bEditableNode) {
            // editable node
            const findOriginEditableNode = function (xNode) {
                return (!xNode.parentNode.isContentEditable) ? xNode : findOriginEditableNode(xNode.parentNode);
            }
            xNode = findOriginEditableNode(xNode);
            if ((xNode.tagName == "P" || xNode.tagName == "DIV") && !(xNode.dataset.grammalecte_button && xNode.dataset.grammalecte_button == "false")) {
                this.accept(xNode);
            } else {
                this.reject();
            }
        } else {
            this.reject();







<
<
<
|







52
53
54
55
56
57
58



59
60
61
62
63
64
65
66
               || (xNode.tagName == "IFRAME" && this._bIframe) )
             && !(xNode.dataset.grammalecte_button  &&  xNode.dataset.grammalecte_button == "false") ) {
            // textarea or iframe
            this.accept(xNode)
        }
        else if (xNode.isContentEditable && this._bEditableNode) {
            // editable node



            xNode = oGrammalecte.findOriginEditableNode(xNode);
            if ((xNode.tagName == "P" || xNode.tagName == "DIV") && !(xNode.dataset.grammalecte_button && xNode.dataset.grammalecte_button == "false")) {
                this.accept(xNode);
            } else {
                this.reject();
            }
        } else {
            this.reject();