Index: gc_lang/fr/webext/background.js ================================================================== --- gc_lang/fr/webext/background.js +++ gc_lang/fr/webext/background.js @@ -177,17 +177,17 @@ Context Menu */ // Editable content browser.contextMenus.create({ - id: "getListOfTokensForEditable", + id: "rightClickLxgEditableNode", title: "Lexicographe (zone de texte)", contexts: ["editable"] }); browser.contextMenus.create({ - id: "parseAndSpellcheckForEditable", + id: "rightClickGCEditableNode", title: "Correction grammaticale (zone de texte)", contexts: ["editable"] }); browser.contextMenus.create({ @@ -196,17 +196,17 @@ contexts: ["editable"] }); // Page browser.contextMenus.create({ - id: "getListOfTokensForPage", + id: "rightClickLxgPage", title: "Lexicographe (page)", contexts: ["all"] }); browser.contextMenus.create({ - id: "parseAndSpellcheckForPage", + id: "rightClickGCPage", title: "Correction grammaticale (page)", contexts: ["all"] }); browser.contextMenus.create({ @@ -215,17 +215,17 @@ contexts: ["all"] }); // Selected text browser.contextMenus.create({ - id: "getListOfTokensForSelection", + id: "getListOfTokensFromSelectedText", title: "Lexicographe (sélection)", contexts: ["selection"] }); browser.contextMenus.create({ - id: "parseAndSpellcheckForSelection", + id: "parseAndSpellcheckSelectedText", title: "Correction grammaticale (sélection)", contexts: ["selection"] }); browser.contextMenus.create({ @@ -264,22 +264,43 @@ 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) { - case "parseAndSpellcheck": - parseAndSpellcheckSelectedText(xTab.id, xInfo.selectionText); + // editable node + // page + case "rightClickGCEditableNode": + case "rightClickLxgEditableNode": + case "rightClickGCPage": + case "rightClickLxgPage": + sendCommandToTab(xInfo.menuItemId, xTab.id); + break; + // selected text + case "rightClickGCSelectedText": + sendCommandToTab("rightClickGCSelectedText", xTab.id); + xGCEWorker.postMessage({ + sCommand: "parseAndSpellcheck", + dParam: {sText: xInfo.selectionText, sCountry: "FR", bDebug: false, bContext: false}, + dInfo: {iReturnPort: iTab} + }); break; - case "getListOfTokens": - getListOfTokensFromSelectedText(xTab.id, xInfo.selectionText); + case "rightClickLxgSelectedText": + sendCommandToTab("rightClickLxgSelectedText", xTab.id); + xGCEWorker.postMessage({ + sCommand: "getListOfTokens", + dParam: {sText: xInfo.selectionText}, + dInfo: {iReturnPort: iTab} + }); break; + // conjugueur case "conjugueur_window": openConjugueurWindow(); break; case "conjugueur_tab": openConjugueurTab(); break; + // rescan page case "rescanPage": let xPort = dConnx.get(xTab.id); xPort.postMessage({sActionDone: "rescanPage"}); break; default: @@ -315,32 +336,13 @@ dOptions = helpers.mapToObject(dOptions); } browser.storage.local.set({"gc_options": dOptions}); } -function parseAndSpellcheckSelectedText (iTab, sText) { - // send message to the tab - let xTabPort = dConnx.get(iTab); - xTabPort.postMessage({sActionDone: "openGCPanel", result: null, dInfo: null, bEnd: false, bError: false}); - // send command to the worker - xGCEWorker.postMessage({ - sCommand: "parseAndSpellcheck", - dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false}, - dInfo: {iReturnPort: iTab} - }); -} - -function getListOfTokensFromSelectedText (iTab, sText) { - // send message to the tab - let xTabPort = dConnx.get(iTab); - xTabPort.postMessage({sActionDone: "openLxgPanel", result: null, dInfo: null, bEnd: false, bError: false}); - // send command to the worker - xGCEWorker.postMessage({ - sCommand: "getListOfTokens", - dParam: {sText: sText}, - dInfo: {iReturnPort: iTab} - }); +function sendCommandToTab (sCommand, iTab) { + let xTabPort = dConnx.get(iTab); + xTabPort.postMessage({sActionDone: sCommand, result: null, dInfo: null, bEnd: false, bError: false}); } function openConjugueurTab () { if (bChrome) { browser.tabs.create({ 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 @@ -96,10 +96,25 @@ if (this.oGCPanel === null) { this.oGCPanel = new GrammalecteGrammarChecker("grammalecte_gc_panel", "Grammalecte", 500, 700); this.oGCPanel.insertIntoPage(); } }, + + startGCPanel: function (xNode=null) { + oGrammalecte.createGCPanel(); + oGrammalecte.oGCPanel.clear(); + oGrammalecte.oGCPanel.show(); + oGrammalecte.oGCPanel.start(xNode); + oGrammalecte.oGCPanel.startWaitIcon(); + }, + + startLxgPanel: function () { + oGrammalecte.createLxgPanel(); + oGrammalecte.oLxgPanel.clear(); + oGrammalecte.oLxgPanel.show(); + oGrammalecte.oLxgPanel.startWaitIcon(); + }, createNode: function (sType, oAttr, oDataset=null) { try { let xNode = document.createElement(sType); Object.assign(xNode, oAttr); @@ -130,10 +145,11 @@ */ let xGrammalectePort = browser.runtime.connect({name: "content-script port"}); xGrammalectePort.onMessage.addListener(function (oMessage) { let {sActionDone, result, dInfo, bEnd, bError} = oMessage; + let sText = ""; switch (sActionDone) { case "parseAndSpellcheck": if (!bEnd) { oGrammalecte.oGCPanel.addParagraphResult(result); } else { @@ -151,25 +167,59 @@ } break; case "getSpellSuggestions": oGrammalecte.oGCPanel.oTooltip.setSpellSuggestionsFor(result.sWord, result.aSugg, dInfo.sErrorId); break; - // Design WTF: context menus are made in background, not in content-script. - // Commands from context menu received here to initialize panels - case "openGCPanel": - oGrammalecte.createGCPanel(); - oGrammalecte.oGCPanel.clear(); - oGrammalecte.oGCPanel.show(); - oGrammalecte.oGCPanel.start(); - oGrammalecte.oGCPanel.startWaitIcon(); - break; - case "openLxgPanel": - oGrammalecte.createLxgPanel(); - oGrammalecte.oLxgPanel.clear(); - oGrammalecte.oLxgPanel.show(); - oGrammalecte.oLxgPanel.startWaitIcon(); - break; + /* + Commands received from the context menu + (Context menu are initialized in background) + */ + // Grammar checker commands + case "rightClickGCEditableNode": + oGrammalecte.startGCPanel(xRightClickedNode); + sText = (xRightClickedNode.tagName == "TEXTAREA") ? xRightClickedNode.value : xRightClickedNode.textContent; + xGrammalectePort.postMessage({ + sCommand: "parseAndSpellcheck", + dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false}, + dInfo: {sTextAreaId: xRightClickedNode.id} + }); + break; + case "rightClickGCPage": + oGrammalecte.startGCPanel(); + xGrammalectePort.postMessage({ + sCommand: "parseAndSpellcheck", + dParam: {sText: "je test", sCountry: "FR", bDebug: false, bContext: false}, + dInfo: {sTextAreaId: xRightClickedNode.id} + }); + break; + case "rightClickGCSelectedText": + oGrammalecte.startGCPanel(); + // selected text is sent to the GC worker in the background script. + break; + // Lexicographer commands + case "rightClickLxgEditableNode": + oGrammalecte.startLxgPanel(); + sText = (xRightClickedNode.tagName == "TEXTAREA") ? xRightClickedNode.value : xRightClickedNode.textContent; + xGrammalectePort.postMessage({ + sCommand: "getListOfTokens", + dParam: {sText: sText}, + dInfo: {sTextAreaId: xRightClickedNode.id} + }); + break; + case "rightClickLxgPage": + oGrammalecte.startLxgPanel(); + xGrammalectePort.postMessage({ + sCommand: "getListOfTokens", + dParam: {sText: "Je teste."}, + dInfo: {sTextAreaId: xRightClickedNode.id} + }); + break; + case "rightClickLxgSelectedText": + oGrammalecte.startLxgPanel(); + // 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); 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 @@ -36,14 +36,11 @@ } // lexicographe let xLxgButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_item", textContent: "Lexicographe"}); xLxgButton.onclick = () => { this.switchMenu(); - oGrammalecte.createLxgPanel(); - oGrammalecte.oLxgPanel.clear(); - oGrammalecte.oLxgPanel.show(); - oGrammalecte.oLxgPanel.startWaitIcon(); + oGrammalecte.startLxgPanel(); xGrammalectePort.postMessage({ sCommand: "getListOfTokens", dParam: {sText: sText}, dInfo: {sTextAreaId: xNode.id} }); @@ -51,14 +48,11 @@ xMenu.appendChild(xLxgButton); // Grammar checker let xGCButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_item", textContent: "Correction grammaticale"}); xGCButton.onclick = () => { this.switchMenu(); - oGrammalecte.createGCPanel(); - oGrammalecte.oGCPanel.start(xNode); - oGrammalecte.oGCPanel.show(); - oGrammalecte.oGCPanel.startWaitIcon(); + oGrammalecte.startGCPanel(xNode); xGrammalectePort.postMessage({ sCommand: "parseAndSpellcheck", dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false}, dInfo: {sTextAreaId: xNode.id} });