Overview
Comment: | [fx] code clarification: use one object dedicated to communication between content-script and background |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk | fx |
Files: | files | file ages | folders |
SHA3-256: |
be4cac56bf2b44f37abc13048d7e530e |
User & Date: | olr on 2020-03-13 17:08:14 |
Other Links: | manifest | tags |
Context
2020-03-13
| ||
19:37 | [fx] Grammalecte API: parseNode -> result as JSON check-in: 15fc2e7c11 user: olr tags: trunk, fx | |
17:08 | [fx] code clarification: use one object dedicated to communication between content-script and background check-in: be4cac56bf user: olr tags: trunk, fx | |
2020-03-12
| ||
23:53 | [fr] ajustements check-in: bdd6bc1361 user: olr tags: trunk, fr | |
Changes
Modified gc_lang/fr/webext/content_scripts/init.js from [e10aed13d1] to [6fdc529374].
︙ | ︙ | |||
168 169 170 171 172 173 174 | this.oGCPanel.clear(); this.oGCPanel.show(); this.oGCPanel.showEditor(); this.oGCPanel.start(what); this.oGCPanel.startWaitIcon(); if (what && bCheckText) { let sText = this.oGCPanel.oTextControl.getText(); | < | < < < | 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | this.oGCPanel.clear(); this.oGCPanel.show(); this.oGCPanel.showEditor(); this.oGCPanel.start(what); this.oGCPanel.startWaitIcon(); if (what && bCheckText) { let sText = this.oGCPanel.oTextControl.getText(); oGrammalecteBackgroundPort.parseAndSpellcheck(sText, "__GrammalectePanel__"); } }, showMessage: function (sMessage) { this.createMessageBox(); this.oMessageBox.show(); this.oMessageBox.setMessage(sMessage); |
︙ | ︙ | |||
304 305 306 307 308 309 310 | autoRefreshOption(); /* Connexion to the background */ | > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | | | | | | | > | | | | | | > | | > | > | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | autoRefreshOption(); /* Connexion to the background */ const oGrammalecteBackgroundPort = { xConnect: browser.runtime.connect({name: "content-script port"}), /* Send messages to the background object { sCommand: the action to perform dParam: parameters necessary for the execution of the action dInfo: 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", dParam: { sText: sText, sCountry: "FR", bDebug: false, bContext: false }, dInfo: { sDestination: sDestination } }); }, parseAndSpellcheck1: function (sText, sDestination, sParagraphId) { this.xConnect.postMessage({ sCommand: "parseAndSpellcheck1", dParam: { sText: sText, sCountry: "FR", bDebug: false, bContext: false }, dInfo: { sDestination: sDestination, sParagraphId: sParagraphId } }); }, getListOfTokens: function (sText) { this.xConnect.postMessage({ sCommand: "getListOfTokens", dParam: { sText: sText }, dInfo: {} }); }, parseFull: function (sText) { this.xConnect.postMessage({ sCommand: "parseFull", dParam: { sText: sTex, sCountry: "FR", bDebug: false, bContext: false }, dInfo: {} }); }, getVerb: function (sVerb, bStart=true, bPro=false, bNeg=false, bTpsCo=false, bInt=false, bFem=false) { this.xConnect.postMessage({ sCommand: "getVerb", dParam: { sVerb: sVerb, bPro: bPro, bNeg: bNeg, bTpsCo: bTpsCo, bInt: bInt, bFem: bFem }, dInfo: { bStart: bStart } }); }, getSpellSuggestions: function (sWord, sErrorId) { this.xConnect.postMessage({ sCommand: "getSpellSuggestions", dParam: { sWord: sWord }, dInfo: { sErrorId: sErrorId } }); }, openURL: function (sURL) { this.xConnect.postMessage({ sCommand: "openURL", dParam: { "sURL": sURL }, dInfo: null }); }, openLexiconEditor: function () { this.xConnect.postMessage({ sCommand: "openLexiconEditor", dParam: null, dInfo: null }); }, restartWorker: function (nTimeDelay=10) { this.xConnect.postMessage({ sCommand: "restartWorker", dParam: { "nTimeDelay": nTimeDelay }, dInfo: {} }); }, /* Messages from the background */ listen: function () { this.xConnect.onMessage.addListener(function (oMessage) { let {sActionDone, result, dInfo, bEnd, bError} = oMessage; switch (sActionDone) { case "init": oGrammalecte.sExtensionUrl = oMessage.sUrl; oGrammalecte.listenRightClick(); oGrammalecte.createButtons(); oGrammalecte.observePage(); break; case "parseAndSpellcheck": if (dInfo.sDestination == "__GrammalectePanel__") { if (!bEnd) { oGrammalecte.oGCPanel.addParagraphResult(result); } else { oGrammalecte.oGCPanel.stopWaitIcon(); oGrammalecte.oGCPanel.endTimer(); } } break; case "parseAndSpellcheck1": if (dInfo.sDestination == "__GrammalectePanel__") { oGrammalecte.oGCPanel.refreshParagraph(dInfo.sParagraphId, result); } break; case "parseFull": // TODO break; case "getListOfTokens": if (!bEnd) { oGrammalecte.oGCPanel.addListOfTokens(result); } else { oGrammalecte.oGCPanel.stopWaitIcon(); oGrammalecte.oGCPanel.endTimer(); } break; case "getSpellSuggestions": oGrammalecte.oGCPanel.oTooltip.setSpellSuggestionsFor(result.sWord, result.aSugg, result.iSuggBlock, dInfo.sErrorId); break; case "getVerb": if (dInfo.bStart) { oGrammalecte.oGCPanel.conjugateWith(result.oVerb, result.oConjTable); } else { oGrammalecte.oGCPanel.displayConj(result.oConjTable); } break; case "workerRestarted": oGrammalecte.oGCPanel.stopWaitIcon(); oGrammalecte.oGCPanel.showMessage("Le serveur grammatical a été arrêté et relancé."); oGrammalecte.oGCPanel.endTimer(); break; /* 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; case "grammar_checker_selection": oGrammalecte.startGCPanel(result, false); // result is the selected text // selected text is sent to the GC worker in the background script. break; case "grammar_checker_iframe": console.log("[Grammalecte] selected iframe: ", result); if (document.activeElement.tagName == "IFRAME") { //console.log(document.activeElement.id); frameId given by result is different than frame.id oGrammalecte.startGCPanel(document.activeElement); } else { oGrammalecte.showMessage("Erreur. Le cadre 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; // rescan page command case "rescanPage": oGrammalecte.rescanPage(); break; default: console.log("[Content script] Unknown command: " + sActionDone); } }); }, /* 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); } else { oGrammalecte.startGCPanel(oGrammalecte.getPageText()); } break; default: console.log("[Content script] Unknown command: " + sActionDone); } }); } } oGrammalecteBackgroundPort.listen() oGrammalecteBackgroundPort.listen2() /* Callable API for the webpage. The API script must be injected this way to be callable by the page */ |
︙ | ︙ | |||
428 429 430 431 432 433 434 | console.log("[Grammalecte] Event: Unknown command", oCommand.sCommand); } } catch (e) { showError(e); } }); | < < < < < < < < < < < < < < < < < < < < < < < < | 523 524 525 526 527 528 529 | console.log("[Grammalecte] Event: Unknown command", oCommand.sCommand); } } catch (e) { showError(e); } }); |
Modified gc_lang/fr/webext/content_scripts/message_box.js from [1023251c05] to [bb68749e64].
1 2 3 4 5 | // JavaScript // Panel creator /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 | // JavaScript // Panel creator /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ /* global oGrammalecte, showError, window, document, console */ "use strict"; class GrammalecteMessageBox { constructor (sId, sTitle) { |
︙ | ︙ |
Modified gc_lang/fr/webext/content_scripts/panel.js from [b5c79a2ce5] to [98261d40f5].
1 2 3 4 5 | // JavaScript // Panel creator /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 | // JavaScript // Panel creator /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ /* global GrammalectePanel, oGrammalecte, showError, window, document, console */ "use strict"; class GrammalectePanel { constructor (sId, sTitle, nWidth, nHeight, bFlexible=true) { |
︙ | ︙ | |||
268 269 270 271 272 273 274 | hideMessage () { this.xPanelMessageBlock.style.display = "none"; this.xPanelMessageActionButton.style.display = "none"; } executeButtonAction (sActionName) { switch (sActionName) { | < < < | < < < | < < < < < < < < | 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | hideMessage () { this.xPanelMessageBlock.style.display = "none"; this.xPanelMessageActionButton.style.display = "none"; } executeButtonAction (sActionName) { switch (sActionName) { case "restartWorker": oGrammalecteBackgroundPort.restartWorker(); this.stopWaitIcon(); break; default: console.log("Action inconnue: ", sAction); } } } |
Modified gc_lang/fr/webext/content_scripts/panel_gc.js from [f1590de9e6] to [1cfb37cbb6].
1 2 3 4 | // JavaScript /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // JavaScript /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ /* global GrammalectePanel, oGrammalecte, oGrammalecteBackgroundPort, showError, window, document, console */ "use strict"; function onGrammalecteGCPanelClick (xEvent) { try { let xElem = xEvent.target; if (xElem.id) { if (xElem.id.startsWith("grammalecte_sugg")) { oGrammalecte.oGCPanel.applySuggestion(xElem.id); } else if (xElem.id === "grammalecte_tooltip_ignore") { oGrammalecte.oGCPanel.ignoreError(xElem.id); } else if (xElem.id.startsWith("grammalecte_check")) { oGrammalecte.oGCPanel.recheckParagraph(parseInt(xElem.dataset.para_num, 10)); } else if (xElem.id.startsWith("grammalecte_hide")) { xElem.parentNode.parentNode.style.display = "none"; } else if (xElem.id.startsWith("grammalecte_err") && xElem.className !== "grammalecte_error_corrected" && xElem.className !== "grammalecte_error_ignored") { oGrammalecte.oGCPanel.oTooltip.show(xElem.parentNode, xElem.id); } else if (xElem.id === "grammalecte_tooltip_url" || xElem.id === "grammalecte_tooltip_db_search") { oGrammalecteBackgroundPort.openURL(xElem.dataset.url); } else { oGrammalecte.oGCPanel.oTooltip.hide(); } } else { oGrammalecte.oGCPanel.oTooltip.hide(); } } |
︙ | ︙ | |||
107 108 109 110 111 112 113 | this.setAutoRefreshButton(); } this.xLxgButton.onclick = () => { if (!this.bWorking) { this.showLexicographer(); this.clearLexicographer(); this.startWaitIcon(); | < < | < < < < | < < | | 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | this.setAutoRefreshButton(); } this.xLxgButton.onclick = () => { if (!this.bWorking) { this.showLexicographer(); this.clearLexicographer(); this.startWaitIcon(); oGrammalecteBackgroundPort.getListOfTokens(this.oTextControl.getText()); //oGrammalecteBackgroundPort.parseFull(this.oTextControl.getText()) } }; this.xConjButton.onclick = () => { if (!this.bWorking) { this.showConjugueur(); } }; this.xLexEditButton.onclick = () => { oGrammalecteBackgroundPort.openLexiconEditor(); }; // Add tabs to menu this.xMenu.appendChild(this.xTFButton) this.xMenu.appendChild(this.xEditorButton) this.xMenu.appendChild(this.xLxgButton) this.xMenu.appendChild(this.xConjButton) this.xPanelBar.appendChild(this.xMenu); |
︙ | ︙ | |||
168 169 170 171 172 173 174 | recheckAll () { this.oTooltip.hide(); this.showEditor(); this.clear(); this.startWaitIcon(); this.resetTimer(); | < | < < < | 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | recheckAll () { this.oTooltip.hide(); this.showEditor(); this.clear(); this.startWaitIcon(); this.resetTimer(); oGrammalecteBackgroundPort.parseAndSpellcheck(this.oTextControl.getText(), "__GrammalectePanel__"); } showEditor () { this.switchContentOn(this.xGCPanelContent, this.xEditorButton); this.switchContentOff(this.xLxgPanelContent, this.xLxgButton); this.switchContentOff(this.xConjPanelContent, this.xConjButton); this.xPanel.style.background = ""; |
︙ | ︙ | |||
284 285 286 287 288 289 290 | } recheckParagraph (iParaNum) { let sParagraphId = "grammalecte_paragraph" + iParaNum; let xParagraph = this.xParent.getElementById(sParagraphId); this._blockParagraph(xParagraph); let sText = this.purgeText(xParagraph.textContent); | < | < < < | 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | } recheckParagraph (iParaNum) { let sParagraphId = "grammalecte_paragraph" + iParaNum; let xParagraph = this.xParent.getElementById(sParagraphId); this._blockParagraph(xParagraph); let sText = this.purgeText(xParagraph.textContent); oGrammalecteBackgroundPort.parseAndSpellcheck1(sText, "__GrammalectePanel__", sParagraphId); this.oTextControl.setParagraph(iParaNum, sText); this.oTextControl.write(); } refreshParagraph (sParagraphId, oResult) { // function called when results are sent by the Worker try { |
︙ | ︙ | |||
636 637 638 639 640 641 642 | updateConj (bStart=false) { let bPro = this.xParent.getElementById('grammalecte_conj_opro').checked; let bNeg = this.xParent.getElementById('grammalecte_conj_oneg').checked; let bTpsCo = this.xParent.getElementById('grammalecte_conj_otco').checked; let bInt = this.xParent.getElementById('grammalecte_conj_oint').checked; let bFem = this.xParent.getElementById('grammalecte_conj_ofem').checked; if (this.sVerb) { | | < < < < | 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | updateConj (bStart=false) { let bPro = this.xParent.getElementById('grammalecte_conj_opro').checked; let bNeg = this.xParent.getElementById('grammalecte_conj_oneg').checked; let bTpsCo = this.xParent.getElementById('grammalecte_conj_otco').checked; let bInt = this.xParent.getElementById('grammalecte_conj_oint').checked; let bFem = this.xParent.getElementById('grammalecte_conj_ofem').checked; if (this.sVerb) { oGrammalecteBackgroundPort.getVerb(this.sVerb, bStart, bPro, bNeg, bTpsCo, bInt, bFem); } } conjugateWith (oVerb, oConjTable) { // function called when results come from the Worker if (oVerb) { this.xParent.getElementById('grammalecte_conj_verb').style.color = "#999999"; |
︙ | ︙ | |||
872 873 874 875 876 877 878 | this.xParent.getElementById("grammalecte_tooltip_db_search").style.display = "inline"; this.xParent.getElementById("grammalecte_tooltip_db_search").dataset.url = "https://grammalecte.net/dictionary.php?prj=fr&lemma="+xNodeErr.textContent; } else { this.xParent.getElementById("grammalecte_tooltip_db_search").style.display = "none"; } this.clearSuggestionBlock(); this.xTooltipSuggBlock.textContent = "Recherche de graphies possibles…"; | < < < | < | 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 | this.xParent.getElementById("grammalecte_tooltip_db_search").style.display = "inline"; this.xParent.getElementById("grammalecte_tooltip_db_search").dataset.url = "https://grammalecte.net/dictionary.php?prj=fr&lemma="+xNodeErr.textContent; } else { this.xParent.getElementById("grammalecte_tooltip_db_search").style.display = "none"; } this.clearSuggestionBlock(); this.xTooltipSuggBlock.textContent = "Recherche de graphies possibles…"; oGrammalecteBackgroundPort.getSpellSuggestions(xNodeErr.textContent, xNodeErr.dataset.error_id); } this.xTooltipArrow.style.display = "block"; this.xTooltip.style.display = "block"; } catch (e) { showError(e); } |
︙ | ︙ | |||
962 963 964 965 966 967 968 | this.clear(); this.xNode = xNode; this.bResultInEvent = Boolean(xNode.dataset.grammalecte_result_via_event && xNode.dataset.grammalecte_result_via_event == "true"); this.bTextArea = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT"); this.bIframe = (xNode.tagName == "IFRAME"); if (this.bTextArea) { this.xNode.disabled = true; | | | | | | | 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 | this.clear(); this.xNode = xNode; this.bResultInEvent = Boolean(xNode.dataset.grammalecte_result_via_event && xNode.dataset.grammalecte_result_via_event == "true"); this.bTextArea = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT"); this.bIframe = (xNode.tagName == "IFRAME"); if (this.bTextArea) { this.xNode.disabled = true; this.loadText(this.xNode.value); } else if (this.bIframe) { // iframe if (!this.bResultInEvent) { oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ La zone analysée est un cadre contenant une autre page web (“iframe”). Les changements faits ne peuvent être pas répercutés dans cette zone."); } this.loadText(this.xNode.contentWindow.document.body.innerText); } else { // editable node oGrammalecte.oGCPanel.addMessageToGCPanel("❗ La zone de texte analysée est un champ textuel enrichi susceptible de contenir des éléments non textuels qui seront effacés lors de la correction."); this.loadText(this.xNode.innerText); } } setText (sText) { this.clear(); oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ Aucun champ textuel défini. Les changements ne seront pas répercutés sur la zone d’où le texte a été extrait."); this.loadText(sText); } loadText (sText) { if (typeof(sText) === "string") { this.dParagraph.clear(); let i = 0; let iStart = 0; let iEnd = 0; sText = sText.replace(/\r\n/g, "\n").replace(/\r/g, "\n").normalize("NFC"); while ((iEnd = sText.indexOf("\n", iStart)) !== -1) { |
︙ | ︙ |