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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
+
-
+
+
+
+
+
+
+
+
-
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
// 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!!!");
console.log("[Content script] Start");
function handleMessage2 (oRequest, xSender, sendResponse) {
console.log(`[Content script] received: ${oRequest.content}`);
change(request.myparam);
//browser.runtime.onMessage.removeListener(handleMessage);
sendResponse({response: "response from content script"});
}
function removeEverything () {
while (document.body.firstChild) {
document.body.firstChild.remove();
}
let nTadId = null;
let nWrapper = 0;
let xConjPanel = null;
let xTFPanel = null;
let xLxgPanel = null;
let xGCPanel = null;
function showError (e) {
console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
}
function wrapTextareas () {
let lNode = document.getElementsByTagName("textarea");
for (let xNode of lNode) {
createWrapper(xNode);
}
}
function createWrapper (xTextArea) {
try {
let xParentElement = xTextArea.parentElement;
let xWrapper = document.createElement("div");
xWrapper.style = "padding: 5px; color: hsl(210, 10%, 90%); background-color: hsl(210, 50%, 50%); border-radius: 3px;";
xWrapper.id = nWrapper + 1;
nWrapper += 1;
xParentElement.insertBefore(xWrapper, xTextArea);
xWrapper.appendChild(xTextArea); // move textarea in wrapper
let xToolbar = createWrapperToolbar(xTextArea);
xWrapper.appendChild(xToolbar);
}
catch (e) {
showError(e);
}
}
let sButtonStyle = "display: inline-block; padding: 0 5px; margin-left: 5px; background-color: hsl(210, 50%, 60%); border-radius: 2px; cursor: pointer;";
function createWrapperToolbar (xTextArea) {
try {
let xToolbar = document.createElement("div");
xToolbar.style = "display: flex; justify-content: flex-end; margin-top: 5px; padding: 5px 10px;";
/*let xLogo = document.createElement("img");
xLogo.src = browser.extension.getURL("img/logo-16.png"); // can’t work, due to content-script policy: https://bugzilla.mozilla.org/show_bug.cgi?id=1267027
xTitle.appendChild(xLogo);*/
xToolbar.appendChild(document.createTextNode("Grammalecte"));
let xConjButton = document.createElement("div");
xConjButton.textContent = "Conjuguer";
xConjButton.style = sButtonStyle;
xConjButton.onclick = function() {
createConjPanel();
};
xToolbar.appendChild(xConjButton);
let xTFButton = document.createElement("div");
xTFButton.textContent = "Formater";
xTFButton.style = sButtonStyle;
xTFButton.onclick = function() {
createTFPanel(xTextArea);
};
xToolbar.appendChild(xTFButton);
let xLxgButton = document.createElement("div");
xLxgButton.textContent = "Analyser";
xLxgButton.style = sButtonStyle;
xLxgButton.onclick = function() {
createLxgPanel(xTextArea);
};
xToolbar.appendChild(xLxgButton);
let xGCButton = document.createElement("div");
xGCButton.textContent = "Corriger";
xGCButton.style = sButtonStyle;
xGCButton.onclick = function() {
xPort.postMessage({sCommand: "parseAndSpellcheck", dParam: {sText: xTextArea.value, sCountry: "FR", bDebug: false, bContext: false}, dInfo: {sTextAreaId: xTextArea.id}});
};
xToolbar.appendChild(xGCButton);
return xToolbar;
}
catch (e) {
showError(e);
}
}
function createConjPanel () {
console.log("Conjugueur");
if (xConjPanel !== null) {
xConjPanel.style.display = "block";
} else {
// create the panel
xConjPanel = createPanelFrame("conj_panel", "Conjugueur");
document.body.appendChild(xConjPanel);
}
}
function createTFPanel (xTextArea) {
console.log("Formateur de texte");
if (xTFPanel !== null) {
xTFPanel.style.display = "block";
} else {
// create the panel
xTFPanel = createPanelFrame("tf_panel", "Formateur de texte");
document.body.appendChild(xTFPanel);
document.getElementById("tf_panel_content").appendChild(createTextFormatter(xTextArea));
}
}
function createLxgPanel (xTextArea) {
console.log("Lexicographe");
if (xLxgPanel !== null) {
xLxgPanel.style.display = "block";
} else {
// create the panel
xLxgPanel = createPanelFrame("lxg_panel", "Lexicographe");
document.body.appendChild(xLxgPanel);
}
}
function change (param) {
document.getElementById("title").setAttribute("background-color", "#809060");
console.log("param: " + param);
function createGCPanel (oErrors) {
console.log("Correction grammaticale");
if (xGCPanel !== null) {
xGCPanel.style.display = "block";
} else {
// create the panel
xGCPanel = createPanelFrame("gc_panel", "Correcteur");
document.body.appendChild(xGCPanel);
document.getElementById("gc_panel_content").appendChild(document.createTextNode(JSON.stringify(oErrors)));
}
}
/*
Simple message
*/
function handleMessage (oMessage, xSender, sendResponse) {
console.log("[Content script] received:");
console.log(oMessage);
document.getElementById("title").setAttribute("background-color", "#FF0000");
//change(request.myparam);
//browser.runtime.onMessage.removeListener(handleMessage);
sendResponse({response: "response from content script"});
}
browser.runtime.onMessage.addListener(handleMessage);
/*
Assign do_something() as a listener for messages from the extension.
Connexion
*/
let xPort = browser.runtime.connect({name: "content-script port"});
browser.runtime.onMessage.addListener(handleMessage2);
xPort.onMessage.addListener(function (oMessage) {
console.log("[Content script] received…");
let {sActionDone, result, dInfo, bError} = oMessage;
switch (sActionDone) {
case "getCurrentTabId":
console.log("[Content script] tab id: " + result);
nTadId = result;
break;
case "parseAndSpellcheck":
console.log(result);
createGCPanel(result);
break;
case "getListOfTokens":
console.log(result);
break;
default:
console.log("[Content script] Unknown command: " + sActionDone);
}
});
xPort.postMessage({sCommand: "getCurrentTabId", dParam: {}, dInfo: {}});
/*document.body.addEventListener("click", function () {
xPort.postMessage({greeting: "they clicked the page!"});
});*/
wrapTextareas();
|