Index: gc_lang/fr/webext/background.js ================================================================== --- gc_lang/fr/webext/background.js +++ gc_lang/fr/webext/background.js @@ -23,11 +23,11 @@ case "parseAndSpellcheck": case "parseAndSpellcheck1": case "getListOfTokens": console.log("Action done: " + sActionDone); if (typeof(dInfo.iReturnPort) === "number") { - let xPort = aConnx[dInfo.iReturnPort]; + 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); } @@ -62,11 +62,11 @@ /* Ports from content-scripts */ -let aConnx = []; +let dConnx = new Map(); /* Messages from the extension (not the Worker) */ @@ -96,22 +96,17 @@ browser.runtime.onMessage.addListener(handleMessage); function handleConnexion (p) { var xPort = p; - let iPortId = aConnx.length; // identifier for the port: each port can be found at aConnx[iPortId] - aConnx.push(xPort); - console.log("Port: " + p.name + ", id: " + iPortId); - console.log(xPort); + let iPortId = xPort.sender.tab.id; // identifier for the port: each port can be found at dConnx[iPortId] + console.log("tab_id: " + iPortId); + dConnx.set(iPortId, xPort); xPort.onMessage.addListener(function (oRequest) { console.log("[background] message via connexion:"); console.log(oRequest); switch (oRequest.sCommand) { - case "getCurrentTabId": - console.log(getCurrentTabId()); - xPort.postMessage({sActionDone: "getCurrentTabId", result: "getCurrentTabId()", dInfo: null, bError: false}); - break; case "parse": case "parseAndSpellcheck": case "parseAndSpellcheck1": case "getListOfTokens": oRequest.dInfo.iReturnPort = iPortId; // we pass the id of the return port to receive answer @@ -126,37 +121,38 @@ xPort.postMessage({sActionDone: "newId", result: iPortId}); } browser.runtime.onConnect.addListener(handleConnexion); - -async function getCurrentTabId () { - let xTab = await browser.tabs.getCurrent(); - return xTab.id; -} - /* Context Menu */ browser.contextMenus.create({ - id: "grammar_checking", + id: "parseAndSpellcheck", title: "Correction grammaticale", - contexts: ["selection", "editable"] + contexts: ["selection"] +}); + +browser.contextMenus.create({ + id: "getListOfTokens", + title: "Lexicographe", + contexts: ["selection"] }); browser.contextMenus.create({ - id: "lexicographer", - title: "Lexicographe", - contexts: ["selection", "editable"] + id: "whatever", + type: "separator", + contexts: ["selection"] }); browser.contextMenus.create({ id: "conjugueur_panel", title: "Conjugueur [fenêtre]", contexts: ["all"] }); + browser.contextMenus.create({ id: "conjugueur_tab", title: "Conjugueur [onglet]", contexts: ["all"] }); @@ -167,12 +163,10 @@ function onError(error) { console.log(`Error: ${error}`); } -let xConjWindow = null; -let xConjTab = null; 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 console.log(xInfo); @@ -179,28 +173,41 @@ console.log(xTab); console.log("Item " + xInfo.menuItemId + " clicked in tab " + xTab.id); console.log("editable: " + xInfo.editable + " · selected: " + xInfo.selectionText); // confusing: no way to get the node where we click?! switch (xInfo.menuItemId) { - case "grammar_checking": - break; - case "lexicographer": - if (xInfo.selectionText) { - xGCEWorker.postMessage(["getListOfTokens", {sText: xInfo.selectionText}]); - } - break; - case "conjugueur_panel": - xConjWindow = browser.windows.create({ + case "parseAndSpellcheck": { + let xPort = dConnx.get(xTab.id); + xPort.postMessage({sActionDone: "openGCPanel", result: null, dInfo: null, bEnd: false, bError: false}); + xGCEWorker.postMessage({ + sCommand: "parseAndSpellcheck", + dParam: {sText: xInfo.selectionText, sCountry: "FR", bDebug: false, bContext: false}, + dInfo: {iReturnPort: xTab.id} + }); + break; + } + case "getListOfTokens": { + let xPort = dConnx.get(xTab.id); + xPort.postMessage({sActionDone: "openLxgPanel", result: null, dInfo: null, bEnd: false, bError: false}); + xGCEWorker.postMessage({ + sCommand: "getListOfTokens", + dParam: {sText: xInfo.selectionText}, + dInfo: {iReturnPort: xTab.id}} + ); + break; + } + case "conjugueur_panel": + let xConjWindow = browser.windows.create({ url: browser.extension.getURL("panel/conjugueur.html"), type: "detached_panel", width: 710, height: 980 }); xConjWindow.then(onCreated, onError); break; case "conjugueur_tab": - xConjTab = browser.tabs.create({ + let xConjTab = browser.tabs.create({ url: browser.extension.getURL("panel/conjugueur.html") }); xConjTab.then(onCreated, onError); break; } Index: gc_lang/fr/webext/content_scripts/content_modifier.js ================================================================== --- gc_lang/fr/webext/content_scripts/content_modifier.js +++ gc_lang/fr/webext/content_scripts/content_modifier.js @@ -15,11 +15,10 @@ } const oGrammalecte = { - nTadId: null, nWrapper: 1, oConjPanel: null, oTFPanel: null, oLxgPanel: null, @@ -52,18 +51,23 @@ try { let xToolbar = createNode("div", {className: "grammalecte_wrapper_toolbar"}); let xConjButton = createNode("div", {className: "grammalecte_wrapper_button", textContent: "Conjuguer"}); xConjButton.onclick = function () { this.createConjPanel(); + //this.oConjPanel.show(); }.bind(this); let xTFButton = createNode("div", {className: "grammalecte_wrapper_button", textContent: "Formater"}); xTFButton.onclick = function () { this.createTFPanel(xTextArea); + this.oTFPanel.start(xTextArea); + this.oTFPanel.show(); }.bind(this); let xLxgButton = createNode("div", {className: "grammalecte_wrapper_button", textContent: "Analyser"}); xLxgButton.onclick = function () { this.createLxgPanel(); + this.oLxgPanel.clear(); + this.oLxgPanel.show(); this.oLxgPanel.startWaitIcon(); xPort.postMessage({ sCommand: "getListOfTokens", dParam: {sText: xTextArea.value}, dInfo: {sTextAreaId: xTextArea.id} @@ -70,12 +74,14 @@ }); }.bind(this); let xGCButton = createNode("div", {className: "grammalecte_wrapper_button", textContent: "Corriger"}); xGCButton.onclick = function () { this.createGCPanel(); - this.oGCPanel.startWaitIcon(); + this.oGCPanel.clear(); + this.oGCPanel.show(); this.oGCPanel.start(xTextArea); + this.oGCPanel.startWaitIcon(); xPort.postMessage({ sCommand: "parseAndSpellcheck", dParam: {sText: xTextArea.value, sCountry: "FR", bDebug: false, bContext: false}, dInfo: {sTextAreaId: xTextArea.id} }); @@ -96,52 +102,33 @@ } }, createConjPanel: function () { console.log("Conjugueur"); - if (this.oConjPanel !== null) { - this.oConjPanel.show(); - } else { - // create the panel + if (this.oConjPanel === null) { this.oConjPanel = new GrammalectePanel("grammalecte_conj_panel", "Conjugueur", 600, 600); this.oConjPanel.insertIntoPage(); } }, createTFPanel: function (xTextArea) { - console.log("Formateur de texte"); - if (this.oTFPanel !== null) { - this.oTFPanel.start(xTextArea); - this.oTFPanel.show(); - } else { - // create the panel + if (this.oTFPanel === null) { this.oTFPanel = new GrammalecteTextFormatter("grammalecte_tf_panel", "Formateur de texte", 800, 620, false); - this.oTFPanel.logInnerHTML(); - this.oTFPanel.start(xTextArea); + //this.oTFPanel.logInnerHTML(); this.oTFPanel.insertIntoPage(); } }, createLxgPanel: function () { - console.log("Lexicographe"); - if (this.oLxgPanel !== null) { - this.oLxgPanel.clear(); - this.oLxgPanel.show(); - } else { - // create the panel + if (this.oLxgPanel === null) { this.oLxgPanel = new GrammalecteLexicographer("grammalecte_lxg_panel", "Lexicographe", 500, 700); this.oLxgPanel.insertIntoPage(); } }, createGCPanel: function () { - console.log("Correction grammaticale"); - if (this.oGCPanel !== null) { - this.oGCPanel.clear(); - this.oGCPanel.show(); - } else { - // create the panel + if (this.oGCPanel === null) { this.oGCPanel = new GrammalecteGrammarChecker("grammalecte_gc_panel", "Grammalecte", 500, 700); this.oGCPanel.insertIntoPage(); } } } @@ -154,14 +141,10 @@ xPort.onMessage.addListener(function (oMessage) { console.log("[Content script] received…"); let {sActionDone, result, dInfo, bEnd, bError} = oMessage; switch (sActionDone) { - case "getCurrentTabId": - console.log("[Content script] tab id: " + result); - oGrammalecte.nTadId = result; - break; case "parseAndSpellcheck": console.log("[content script] received: parseAndSpellcheck"); if (!bEnd) { oGrammalecte.oGCPanel.addParagraphResult(result); } else { @@ -178,18 +161,30 @@ oGrammalecte.oLxgPanel.addListOfTokens(result); } else { oGrammalecte.oLxgPanel.stopWaitIcon(); } 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; default: console.log("[Content script] Unknown command: " + sActionDone); } }); -xPort.postMessage({ - sCommand: "getCurrentTabId", - dParam: {}, - dInfo: {} -}); - +/* + Start +*/ oGrammalecte.wrapTextareas();