Index: gc_lang/fr/webext/content_scripts/init.js ================================================================== --- gc_lang/fr/webext/content_scripts/init.js +++ gc_lang/fr/webext/content_scripts/init.js @@ -171,10 +171,25 @@ showMessage: function (sMessage) { this.createMessageBox(); this.oMessageBox.show(); this.oMessageBox.setMessage(sMessage); }, + + parseAndSpellcheck (xNode=null) { + this.startGCPanel(xNode); + let sText = ""; + if (xNode) { + sText = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT") ? xNode.value.normalize("NFC") : xNode.innerText.normalize("NFC"); + } else { + sText = this.getPageText(); + } + xGrammalectePort.postMessage({ + sCommand: "parseAndSpellcheck", + dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false}, + dInfo: (xNode) ? {sTextAreaId: xNode.id} : {} + }); + }, getPageText: function () { let sPageText = document.body.innerText; let nPos = sPageText.indexOf("__grammalecte_panel__"); if (nPos >= 0) { @@ -323,17 +338,17 @@ (Context menu are initialized in background) */ // Grammar checker commands case "grammar_checker_editable": if (oGrammalecte.xRightClickedNode !== null) { - parseAndSpellcheckEditableNode(oGrammalecte.xRightClickedNode); + oGrammalecte.parseAndSpellcheck(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": - parseAndSpellcheckPage(); + oGrammalecte.parseAndSpellcheck(); break; case "grammar_checker_selection": oGrammalecte.startGCPanel(); // selected text is sent to the GC worker in the background script. break; @@ -355,38 +370,14 @@ /* Commands received from the keyboard (shortcuts) */ case "shortcutGrammarChecker": if (xActiveNode && (xActiveNode.tagName == "TEXTAREA" || xActiveNode.tagName == "INPUT" || xActiveNode.isContentEditable)) { - parseAndSpellcheckEditableNode(xActiveNode); + oGrammalecte.parseAndSpellcheck(xActiveNode); } else { - parseAndSpellcheckPage(); + oGrammalecte.parseAndSpellcheck(); } break; default: console.log("[Content script] Unknown command: " + sActionDone); } }); - - -/* - Actions -*/ - -function parseAndSpellcheckPage () { - oGrammalecte.startGCPanel(); - xGrammalectePort.postMessage({ - sCommand: "parseAndSpellcheck", - dParam: {sText: oGrammalecte.getPageText(), sCountry: "FR", bDebug: false, bContext: false}, - dInfo: {} - }); -} - -function parseAndSpellcheckEditableNode (xNode) { - oGrammalecte.startGCPanel(xNode); - let sText = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT") ? xNode.value.normalize("NFC") : xNode.innerText.normalize("NFC"); - xGrammalectePort.postMessage({ - sCommand: "parseAndSpellcheck", - dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false}, - dInfo: {sTextAreaId: xNode.id} - }); -} Index: gc_lang/fr/webext/content_scripts/menu.js ================================================================== --- gc_lang/fr/webext/content_scripts/menu.js +++ gc_lang/fr/webext/content_scripts/menu.js @@ -1,10 +1,10 @@ // JavaScript /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ -/* global oGrammalecte, xGrammalectePort, showError, window, document */ +/* global oGrammalecte, showError, window, document */ "use strict"; class GrammalecteButton { @@ -11,68 +11,58 @@ constructor (nMenu, xNode) { this.xNode = xNode; this.xButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_main_button", textContent: " "}); this.xButton.onclick = () => { - oGrammalecte.startGCPanel(this.xNode); - xGrammalectePort.postMessage({ - sCommand: "parseAndSpellcheck", - dParam: {sText: this._getText(), sCountry: "FR", bDebug: false, bContext: false}, - dInfo: {sTextAreaId: this.xNode.id} - }); + oGrammalecte.parseAndSpellcheck(this.xNode); }; this.xButton.style.zIndex = (xNode.style.zIndex.search(/^[0-9]+$/) !== -1) ? (parseInt(xNode.style.zIndex) + 1).toString() : xNode.style.zIndex; - let xStyle = window.getComputedStyle(this.xNode); - - let xNodeInsertAfter = this.xNode; - if (document.location.host == "twitter.com" && this.xNode.classList.contains('rich-editor')) { - xNodeInsertAfter = this.xNode.parentNode; - } - this.bShadow = document.body.createShadowRoot || document.body.attachShadow; if (this.bShadow) { - let nMarginTop = -1 * (parseInt(xStyle.marginBottom.replace('px', ''), 10)); this.xShadowBtn = oGrammalecte.createNode("div", {style: "display:none;position:absolute;width:0;height:0;"}); this.xShadowBtnNode = this.xShadowBtn.attachShadow({mode: "open"}); oGrammalecte.createStyle("content_scripts/menu.css", null, this.xShadowBtnNode); this.xShadowBtnNode.appendChild(this.xButton); - this._insertAfter(this.xShadowBtn, xNodeInsertAfter, nMarginTop); + this._insert(this.xShadowBtn); } else { - let nMarginTop = -1 * (8 + parseInt(xStyle.marginBottom.replace('px', ''), 10)); if (!document.getElementById("grammalecte_cssmenu")) { oGrammalecte.createStyle("content_scripts/menu.css", "grammalecte_cssmenu", document.head); } - this._insertAfter(this.xButton, xNodeInsertAfter, nMarginTop); + this._insert(this.xButton); } - this._createListeners(); + this._listen(); } - _insertAfter (xNewNode, xReferenceNode, nMarginTop) { + _insert (xNewNode) { + // insertion + let xReferenceNode = this.xNode; + if (document.location.host == "twitter.com" && this.xNode.classList.contains('rich-editor')) { + xReferenceNode = this.xNode.parentNode; + } xReferenceNode.parentNode.insertBefore(xNewNode, xReferenceNode.nextSibling); + // offset + let nNodeMarginBottom = parseInt(window.getComputedStyle(this.xNode).marginBottom.replace('px', ''), 10); + let nMarginTop = (this.bShadow) ? -1 * nNodeMarginBottom : -1 * (8 + nNodeMarginBottom); xNewNode.style.marginTop = nMarginTop + "px"; } - _createListeners () { + _listen () { this.xNode.addEventListener('focus', (e) => { if (this.bShadow) { this.xShadowBtn.style.display = "block"; } this.xButton.style.display = "block"; }); /*this.xNode.addEventListener('blur', (e) => { - window.setTimeout(() => {this.xButton.style.display = "none";}, 300); + window.setTimeout(() => { this.xButton.style.display = "none"; }, 300); });*/ } - _getText () { - return (this.xNode.tagName == "TEXTAREA" || this.xNode.tagName == "INPUT") ? this.xNode.value.normalize("NFC") : this.xNode.innerText.normalize("NFC") - } - deleteNodes () { if (this.bShadow) { this.xShadowBtn.parentNode.removeChild(this.xShadowBtn); } else { this.xButton.parentNode.removeChild(this.xButton); } } }