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
|
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
132
133
134
135
136
|
+
-
+
+
+
+
+
+
+
+
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
-
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
// Modify page
import { echo } from "../mymodule";
/*
JS sucks (again, and again, and again, and again…)
Not possible to load content from within the extension:
https://bugzilla.mozilla.org/show_bug.cgi?id=1267027
No SharedWorker, no images allowed for now…
*/
"use strict";
echo("CONTENT SCRIPRT!!!");
function handleMessage2 (oRequest, xSender, sendResponse) {
console.log(`[Content script] received: ${oRequest.content}`);
change(request.myparam);
function showError (e) {
console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
}
function createNode (sType, oAttr, oDataset=null) {
try {
let xNode = document.createElement(sType);
Object.assign(xNode, oAttr);
if (oDataset) {
Object.assign(xNode.dataset, oDataset);
}
return xNode;
}
catch (e) {
//browser.runtime.onMessage.removeListener(handleMessage);
sendResponse({response: "response from content script"});
}
function removeEverything () {
while (document.body.firstChild) {
document.body.firstChild.remove();
}
}
showError(e);
}
}
/*
function loadImage (sContainerClass, sImagePath) {
let xRequest = new XMLHttpRequest();
xRequest.open('GET', browser.extension.getURL("")+sImagePath, false);
xRequest.responseType = "arraybuffer";
xRequest.send();
let blobTxt = new Blob([xRequest.response], {type: 'image/png'});
let img = document.createElement('img');
img.src = (URL || webkitURL).createObjectURL(blobTxt); // webkitURL is obsolete: https://bugs.webkit.org/show_bug.cgi?id=167518
Array.filter(document.getElementsByClassName(sContainerClass), function (oElem) {
oElem.appendChild(img);
});
}
*/
const oGrammalecte = {
nWrapper: 0,
lWrapper: [],
oTFPanel: null,
oLxgPanel: null,
oGCPanel: null,
wrapTextareas: function () {
let lNode = document.getElementsByTagName("textarea");
for (let xNode of lNode) {
this.lWrapper.push(new GrammalecteWrapper(this.nWrapper, xNode));
this.nWrapper += 1;
}
},
function change (param) {
document.getElementById("title").setAttribute("background-color", "#809060");
console.log("param: " + param);
createTFPanel: function () {
if (this.oTFPanel === null) {
this.oTFPanel = new GrammalecteTextFormatter("grammalecte_tf_panel", "Formateur de texte", 800, 620, false);
//this.oTFPanel.logInnerHTML();
this.oTFPanel.insertIntoPage();
}
},
createLxgPanel: function () {
if (this.oLxgPanel === null) {
this.oLxgPanel = new GrammalecteLexicographer("grammalecte_lxg_panel", "Lexicographe", 500, 700);
this.oLxgPanel.insertIntoPage();
}
},
document.getElementById("title").setAttribute("background-color", "#FF0000");
createGCPanel: function () {
if (this.oGCPanel === null) {
this.oGCPanel = new GrammalecteGrammarChecker("grammalecte_gc_panel", "Grammalecte", 500, 700);
this.oGCPanel.insertIntoPage();
}
}
}
/*
Assign do_something() as a listener for messages from the extension.
Connexion to the background
*/
let xGrammalectePort = browser.runtime.connect({name: "content-script port"});
browser.runtime.onMessage.addListener(handleMessage2);
xGrammalectePort.onMessage.addListener(function (oMessage) {
let {sActionDone, result, dInfo, bEnd, bError} = oMessage;
switch (sActionDone) {
case "parseAndSpellcheck":
if (!bEnd) {
oGrammalecte.oGCPanel.addParagraphResult(result);
} else {
oGrammalecte.oGCPanel.stopWaitIcon();
}
break;
case "parseAndSpellcheck1":
oGrammalecte.oGCPanel.refreshParagraph(dInfo.sParagraphId, result);
break;
case "getListOfTokens":
if (!bEnd) {
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);
}
});
/*
Start
*/
oGrammalecte.wrapTextareas();
|