Index: gc_lang/fr/webext/background.js ================================================================== --- gc_lang/fr/webext/background.js +++ gc_lang/fr/webext/background.js @@ -1,49 +1,59 @@ // Background "use strict"; +function showError (e) { + console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); +} /* Worker (separate thread to avoid freezing Firefox) */ let xGCEWorker = new Worker("gce_worker.js"); xGCEWorker.onmessage = function (e) { // https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent - switch (e.data[0]) { - case "grammar_errors": - console.log("GRAMMAR ERRORS"); - console.log(e.data[1].aGrammErr); - browser.runtime.sendMessage({sCommand: "grammar_errors", aGrammErr: e.data[1].aGrammErr}); - break; - case "spelling_and_grammar_errors": - console.log("SPELLING AND GRAMMAR ERRORS"); - console.log(e.data[1].aSpellErr); - console.log(e.data[1].aGrammErr); - break; - case "text_to_test_result": - browser.runtime.sendMessage({sCommand: "text_to_test_result", sResult: e.data[1]}); - break; - case "fulltests_result": - console.log("TESTS RESULTS"); - browser.runtime.sendMessage({sCommand: "fulltests_result", sResult: e.data[1]}); - break; - case "options": - console.log("OPTIONS"); - console.log(e.data[1]); - break; - case "tokens": - console.log("TOKENS"); - console.log(e.data[1]); - break; - case "error": - console.log("ERROR"); - console.log(e.data[1]); - break; - default: - console.log("Unknown command: " + e.data[0]); + try { + switch (e.data[0]) { + case "grammar_errors": + console.log("GRAMMAR ERRORS"); + console.log(e.data[1].aGrammErr); + browser.runtime.sendMessage({sCommand: "grammar_errors", aGrammErr: e.data[1].aGrammErr}); + break; + case "spelling_and_grammar_errors": + console.log("SPELLING AND GRAMMAR ERRORS"); + console.log(e.data[1].aSpellErr); + console.log(e.data[1].aGrammErr); + break; + case "text_to_test_result": + browser.runtime.sendMessage({sCommand: "text_to_test_result", sResult: e.data[1]}); + break; + case "fulltests_result": + console.log("TESTS RESULTS"); + browser.runtime.sendMessage({sCommand: "fulltests_result", sResult: e.data[1]}); + break; + case "options": + console.log("OPTIONS"); + console.log(e.data[1]); + break; + case "tokens": + console.log("TOKENS"); + console.log(e.data[1]); + browser.browserAction.setPopup({popup: "panel/main.html"}); + browser.runtime.sendMessage({sCommand: "show_tokens", oResult: e.data[1]}); + break; + case "error": + console.log("ERROR"); + console.log(e.data[1]); + break; + default: + console.log("Unknown command: " + e.data[0]); + } + } + catch (e) { + showError(e); } }; xGCEWorker.postMessage(["init", {sExtensionPath: browser.extension.getURL("."), sOptions: "", sContext: "Firefox"}]); @@ -101,8 +111,11 @@ // 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; } }); Index: gc_lang/fr/webext/panel/main.js ================================================================== --- gc_lang/fr/webext/panel/main.js +++ gc_lang/fr/webext/panel/main.js @@ -1,10 +1,12 @@ // Main panel "use strict"; - +/* + Common functions +*/ function showError (e) { console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); } function showPage (sPageName) { @@ -28,10 +30,18 @@ } catch (e) { showError(e); } } + +function startWaitIcon () { + document.getElementById("waiticon").hidden = false; +} + +function stopWaitIcon () { + document.getElementById("waiticon").hidden = true; +} /* Events */ @@ -81,18 +91,96 @@ /* Messages received */ function handleMessage (oMessage, xSender, sendResponse) { - //console.log(xSender); + console.log(xSender); switch(oMessage.sCommand) { + case "show_tokens": + console.log("show tokens"); + addParagraphOfTokens(oMessage.oResult); + break; case "text_to_test_result": - document.getElementById("tests_result").textContent = oMessage.sResult; + showTestResult(oMessage.sResult); break; case "fulltests_result": - document.getElementById("tests_result").textContent = oMessage.sResult; + showTestResult(oMessage.sResult); break; } sendResponse({sCommand: "none", sResult: "done"}); } browser.runtime.onMessage.addListener(handleMessage); + + +/* + + DEDICATED FUNCTIONS + +*/ + + +/* + Test page +*/ +function showTestResult (sText) { + document.getElementById("tests_result").textContent = sText; +} + + +/* + Lexicographer page +*/ + +function addSeparator (sText) { + if (document.getElementById("tokens_list").textContent !== "") { + let xElem = document.createElement("p"); + xElem.className = "separator"; + xElem.textContent = sText; + document.getElementById("tokens_list").appendChild(xElem); + } +} + +function addMessage (sClass, sText) { + let xNode = document.createElement("p"); + xNode.className = sClass; + xNode.textContent = sText; + document.getElementById("tokens_list").appendChild(xNode); +} + +function addParagraphOfTokens (lElem) { + try { + let xNodeDiv = document.createElement("div"); + xNodeDiv.className = "paragraph"; + for (let oToken of lElem) { + xNodeDiv.appendChild(createTokenNode(oToken)); + } + document.getElementById("tokens_list").appendChild(xNodeDiv); + } + catch (e) { + showError(e); + } +} + +function createTokenNode (oToken) { + let xTokenNode = document.createElement("div"); + xTokenNode.className = "token " + oToken.sType; + let xTokenValue = document.createElement("b"); + xTokenValue.className = oToken.sType; + xTokenValue.textContent = oToken.sValue; + xTokenNode.appendChild(xTokenValue); + let xSep = document.createElement("s"); + xSep.textContent = " : "; + xTokenNode.appendChild(xSep); + if (oToken.aLabel.length === 1) { + xTokenNode.appendChild(document.createTextNode(oToken.aLabel[0])); + } else { + let xTokenList = document.createElement("ul"); + for (let sLabel of oToken.aLabel) { + let xTokenLine = document.createElement("li"); + xTokenLine.textContent = sLabel; + xTokenList.appendChild(xTokenLine); + } + xTokenNode.appendChild(xTokenList); + } + return xTokenNode; +}