-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.js
76 lines (69 loc) · 2.57 KB
/
options.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
// Saves options to chrome.storage
function saveOptions() {
var settingsArray = [];
var rows = document.getElementsByClassName("row");
for (var i = 0; i < rows.length; i++){
var entry = [];
var rowChildren = rows[i].childNodes;
var findValue;
var replaceValue;
for (var c = 0; c < rowChildren.length; c++){
if(rowChildren[c].className == "find"){
findValue = rowChildren[c].value;
}
if(rowChildren[c].className == "replace"){
replaceValue = rowChildren[c].value
}
}
entry.push(findValue, replaceValue);
settingsArray.push(entry);
}
chrome.storage.sync.set({
"textReplaceSettings": settingsArray
}, function () {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function () {
status.textContent = '';
}, 5000);
});
}
function getOptions(){
chrome.storage.sync.get("textReplaceSettings", function(options){
var settings = options.textReplaceSettings;
for(var i = 0; i < settings.length; i++){
//For the first, insert it into the already existing input
if (i == 0){
document.getElementById("find0").value = settings[i][0];
document.getElementById("replace0").value = settings[i][1];
}
else {
insertRow(settings[i])
}
}
});
}
function insertRow(entry) {
var html = document.createElement('div');
html.classList.add("row");
html.innerHTML = [
'<label>Find:</label><input class="find" placeholder="Text to Find..." value="' + entry[0] + '"/>',
'<label>Replace with:</label><input class="replace" placeholder="Replace found text with..." value="' + entry[1] +'"/>',
'<button class="btn btn-danger removeRow""></button>'].join("\n");
var lastDiv = document.getElementById("rowContainer").lastChild;
lastDiv.parentNode.insertBefore(html,lastDiv);
//Now add the event listener to the new button
html.lastChild.addEventListener('click', function(){
removeRow(html);
})
}
function addEmptyRow(){
insertRow(["",""]);
}
function removeRow(row){
row.parentElement.removeChild(row);
}
document.addEventListener('DOMContentLoaded', getOptions);
document.getElementById("addRow").addEventListener('click', addEmptyRow);
document.getElementById('save').addEventListener('click', saveOptions);