-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
318 lines (298 loc) · 12.6 KB
/
popup.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const defaultCat = "general" // Default category.
let dataDict = {};
function doInCurrentTab(tabCallback) {
// Get the current tab id.
chrome.tabs.query(
{ currentWindow: true, active: true },
function (tabArray) { tabCallback(tabArray[0]) }
)
}
function inject(text) {
// Function that injects text into detected inputs.
var txt = document.querySelectorAll("input, textarea, p")
for (var i=0;i<txt.length;i++) {
if (txt[i].type !== 'file') {
txt[i].textContent = txt[i].textContent + text
}
}
}
function createMacroElement(macroText) {
// Create the button element of a macro.
var activeTabId
doInCurrentTab(function(tab) {
activeTabId = tab.id
})
var custoMacro = document.createElement('button')
custoMacro.textContent = macroText
custoMacro.innerText = macroText.slice(0, 65)
custoMacro.title = macroText
custoMacro.id = "macro"
custoMacro.addEventListener('click', function() {
// Execute the inject(text) function when the macro is clicked.
chrome.scripting.executeScript({
target: { tabId: activeTabId },
func: inject,
args: [macroText]
})
})
// Add the button to the "macroContainer" div.
var macroContainer = document.getElementById('macroContainer')
macroContainer.appendChild(custoMacro)
}
function changeCat(catName) {
// Change current category.
// When clicked, delete all #cat and #macro buttons.
var elementsCat = document.querySelectorAll('#cat')
var elementsMacro = document.querySelectorAll('#macro')
elementsCat.forEach(function(element) {
element.parentNode.removeChild(element)
})
elementsMacro.forEach(function(element) {
element.parentNode.removeChild(element)
})
window.value = catName // Change current category.
chrome.storage.local.set({'actualCat': window.value}) // Save category.
loadElements() // Recreate categories and macros.
}
function createCatElement(catName) {
// Creation of a category button element.
var activeTabId
doInCurrentTab(function (tab) {
activeTabId = tab.id
})
var newCat = document.createElement('button')
newCat.textContent = catName
newCat.innerText = catName.slice(0, 65)
newCat.title = catName
newCat.id = "cat"
// Execute the changeCat(catName) function when the button is clicked.
newCat.addEventListener('click', function() {changeCat(catName)})
// Add the button to the "macroContainer" div.
var macroContainer = document.getElementById('macroContainer')
macroContainer.appendChild(newCat)
}
function loadElements() {
// Function that loads and displays elements from the localstorage.
// Load current category.
chrome.storage.local.get(['actualCat'], function(result) {
if (result.actualCat == undefined) { // The category has not been saved.
// Load the default category and save it in the localstorage.
chrome.storage.local.set({'actualCat': defaultCat})
window.value = defaultCat
} else {
window.value = result.actualCat
}
// Refresh current category display.
if (window.value == defaultCat) {
document.getElementById("catStatus").textContent = ""
} else {
document.getElementById("catStatus").textContent = window.value.slice(0, 12)
}
// Load categories and macros from localstorage.
chrome.storage.local.get(['macroDict'], function(result) {
var macroDict = JSON.parse(result.macroDict)
// Browse macro dictionary categories.
for (var cle of Object.keys(macroDict)) {
// Display categories only in the default category.
if (window.value == defaultCat && cle !== defaultCat) {
(function(cle) { // Create the category button.
createCatElement(cle)
})(cle)
}
if (cle == window.value) {
// Browse macros in the current category.
for (var i = 0; i < macroDict[cle].length; i++) { (function(macroText) {
createMacroElement(macroText) // Create the macro button.
})(macroDict[cle][i])}
}
}
})
})
}
// When the extension is open.
document.addEventListener('DOMContentLoaded', function() {
var activeTabId
doInCurrentTab(function (tab) {
activeTabId = tab.id
})
// Load the macro dictionary and create it if not already defined.
chrome.storage.local.get(['macroDict'], function(result) {
if (result.macroDict == "{}" || result.macroDict == undefined) {
// If the dictionary is empty or does not exist,
// create it by adding the default category.
var macroDict = {}
macroDict[defaultCat] = []
chrome.storage.local.set({'macroDict': JSON.stringify(macroDict)})
} else { // If the dictionary exists, load it normally.
var macroDict = JSON.parse(result.macroDict)
if (!macroDict.hasOwnProperty(defaultCat)) {
// If the dictionary does not contain the default category, add it.
macroDict[defaultCat] = []
chrome.storage.local.set({'macroDict': JSON.stringify(macroDict)})
}
}
// Updated contextual menus.
chrome.contextMenus.removeAll()
for (var cat of Object.keys(macroDict)) {
// Creation of a parent menu for each macro category.
var catMenu = chrome.contextMenus.create({
title: cat,
contexts: ["all"]
})
for (var i = 0; i < macroDict[cat].length; i++) {
// Create a sub-menu for each macro.
(function(cat, i) {
chrome.contextMenus.create({
title: macroDict[cat][i].slice(0, 65),
parentId: catMenu,
contexts: ["all"],
onclick: function() {
// Execute the inject(text) function when the menu is clicked.
console.log(macroDict[cat][i])
chrome.scripting.executeScript({
target: { tabId: activeTabId },
func: inject,
args: [macroDict[cat][i]]
})
}
})
})(cat, i)
}
}
// Add a separator if text is selected.
chrome.contextMenus.create({
id: "sep1",
type: "separator",
contexts: ["all"],
})
// Context menu to create a macro with the selected text.
chrome.contextMenus.create({
id: "createMacro",
title: 'Create a macro "%s"',
contexts: ["selection"],
onclick: function(info) {
// Create a macro with the selected text.
// Remove spaces and line breaks at the end of selected text.
var macroText = info.selectionText.trimRight().replace(/\n$/, '')
// Check that the text is not empty and that it doesn't already exist in the same category.
if (macroText && !macroDict[window.value].includes(macroText)) {
createMacroElement(macroText)
// Add the macro to the macro dictionary and save it in the localstorage.
macroDict[window.value].push(macroText)
chrome.storage.local.set({'macroDict': JSON.stringify(macroDict)})
}
}
})
// Add a separator if text is selected.
chrome.contextMenus.create({
id: "sep2",
type: "separator",
contexts: ["selection"],
})
// Right-click context menu to copy a macro.
chrome.contextMenus.create({
id: "copyContent",
title: "Copy macro",
contexts: ["all"],
onclick: function() { // Copy the selected button.
var bouton = document.activeElement
navigator.clipboard.writeText(bouton.title)
}
})
// Right-click context menu to delete macros.
chrome.contextMenus.create({
id: "deleteButton",
title: "Delete button",
contexts: ["all"],
onclick: function() { // Delete the selected button.
var bouton = document.activeElement
// Delete the selected category.
if (bouton.id == "cat" && bouton.title !== defaultCat) {
delete macroDict[bouton.title]
bouton.remove()
// Delete the selected macro.
} else if (bouton.id == "macro") {
var iboutoSupr = macroDict[window.value].indexOf(bouton.title)
if (iboutoSupr !== -1) {
macroDict[window.value].splice(iboutoSupr, 1)
}
bouton.remove()
}
chrome.storage.local.set({'macroDict': JSON.stringify(macroDict)})
}
})
chrome.contextMenus.create({
id: "sep3",
type: "separator",
contexts: ["all"],
})
var catMenu = chrome.contextMenus.create({
title: "Import / Export macros",
contexts: ["all"]
})
// Context menu to copy macro dictionary to json.
chrome.contextMenus.create({
id: "copyDict",
title: "Copy macros to clipboard",
contexts: ["all"],
parentId: catMenu,
onclick: function() {
chrome.storage.local.get(['macroDict'], function(result) {
navigator.clipboard.writeText(result.macroDict)
})
}
})
// Context menu to load the macro dictionary in json.
chrome.contextMenus.create({
id: "loadDict",
title: "Load macros from input",
contexts: ["all"],
parentId: catMenu,
onclick: function() {
var macroDict = document.getElementById("TextInput").value;
try { // Tests whether macroDict is a valid JSON.
JSON.parse(macroDict);
chrome.storage.local.set({'macroDict': macroDict});
loadElements();
} catch (e) {
console.error("Error: The value is not a valid JSON.");
}
}
})
})
loadElements() // Load items from localstorage.
})
// Create a new macro if the New macro button is clicked.
document.getElementById('addMacro').addEventListener('click', function() {
var macroText = document.getElementById("TextInput").value
macroText = macroText.trimRight().replace(/\n$/, '') // Delete spaces and line breaks at the end.
chrome.storage.local.get('macroDict', function (result) {
var macroDict = JSON.parse(result.macroDict)
// Check that the text is not empty and that it doesn't already exist in the same category.
if (macroText && !macroDict[window.value].includes(macroText)) {
createMacroElement(macroText)
// Add the macro to the macro dictionary and save it in the localstorage.
macroDict[window.value].push(macroText)
chrome.storage.local.set({'macroDict': JSON.stringify(macroDict)})
}
})
})
// Create a new macro category if the New Category button is clicked.
document.getElementById('addCategoryButton').addEventListener('click', function() {
var catName = document.getElementById("TextInput").value
catName = catName.trimRight().replace(/\n$/, '') // Delete spaces and line breaks at the end.
chrome.storage.local.get('macroDict', function (result) {
var macroDict = JSON.parse(result.macroDict)
// Check that the text is not empty and that a category with the same name does not already exist.
if (catName && !macroDict.hasOwnProperty(catName)) {
createCatElement(catName)
// Add the category to the macro dictionary and save it in localstorage.
macroDict[catName] = []
chrome.storage.local.set({'macroDict': JSON.stringify(macroDict)})
}
})
})
// Load default category if back button is clicked.
document.getElementById('racineCat').addEventListener('click', function () {
changeCat(defaultCat)
})