-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
151 lines (127 loc) · 4.18 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
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
var options, delta;
function loadDefaults(){
loadDefaultOptions();
loadDefaultDelta();
loadDefaultPad();
saveOptions();
}
function loadDefaultOptions(){
options[0] = new objOption("()([\\d]{1,16})($)", true);
options[1] = new objOption("(.*\\/[\\d]*?)([\\d]{1,16})([\\/]?[.\\w]{0,5}$)", true);
console.log("Default options loaded");
setCheckboxes();
console.log("Initial checkbox values set");
}
function loadDefaultDelta(){
delta =1;
chrome.extension.sendMessage({msg:"updateDelta", delta:delta});
setDeltaBox();
}
function loadDefaultPad(){
localStorage["pad"]="ask";//default option
document.getElementById("pad").value = localStorage["pad"];
}
function onScriptStart(){
somethingFoundMissing=false;//only re-save things if localStorage is missing something
if(localStorage["options"]==null || localStorage["options"]==undefined){
console.log("Options array was "+(localStorage["options"]==null ? "null" : "undefined") + " on entry.");
options = new Array();
loadDefaultOptions();
somethingFoundMissing=true;
}
else{
console.log("Options array was found saved!");
var temp = JSON.parse(localStorage["options"]);
console.log(temp);
options = new Array();
for(var i in temp){
console.log("Parsing object["+i+"]: " + temp[i]);
options[i] = new objOption(temp[i].strRegex, temp[i].enabled.valueOf());
}
console.log(options);
setCheckboxes();
}
if(localStorage["delta"]==undefined || Number.isNaN(localStorage["delta"])){
loadDefaultDelta();
console.log("Delta value could not be loaded... default: " + delta);
somethingFoundMissing=true;
}else{
delta=Number(localStorage["delta"]);
console.log("Delta value loaded: " + delta);
setDeltaBox();
}
if(localStorage["pad"]==null || localStorage["pad"]==undefined){
console.log("Pad option was "+(localStorage["pad"]==null ? "null" : "undefined") + " on load.");
loadDefaultPad();
somethingFoundMissing=true;
}else{
document.getElementById("pad").value = localStorage["pad"];
}
if(somethingFoundMissing){
saveOptions();
}
}
function saveOptions(){
for(i=0; i<options.length;i++){
var obj;
if(obj=document.getElementById("default"+i)){
//object exists
options[i].setEnabled(obj.checked==true);
console.log("Saving option " + i + " as " + options[i].isEnabled());
}
}
localStorage["options"] = JSON.stringify(options);
//collect padding option
padSelect = document.getElementById("pad");
localStorage["pad"] = padSelect.options[padSelect.selectedIndex].value;
console.log("Pad value saved: " + localStorage["pad"]);
var test1 = Number(document.getElementById("textDelta").value);
var test2 = Number.isNaN(test1);
if(!test2 && test1 > 0){
delta = Number(document.getElementById("textDelta").value);
localStorage["delta"] = delta;
chrome.extension.sendMessage({msg:"updateDelta", delta:delta});
console.log("Delta value saved: " + localStorage["delta"]);
}else{
setDeltaBox();
alert("Please enter in a valid, positive number for new delta value. Delta value has not been changed. All regex choices have been saved, though.");
}
}
function setCheckboxes(){
for(i=0; i<options.length;i++){
var obj;
if(obj=document.getElementById("default"+i)){
if(options[i].isEnabled()){
obj.checked=true;
console.log("Setting option " + i + " as checked");
}
else{
obj.checked=false;
console.log("Setting option " + i + " as unchecked");
}
}
}
}
function setDeltaBox(){
document.getElementById("textDelta").value=delta;
}
function objOption(regex, enabled){
this.strRegex = regex;
this.objRegex = new RegExp(regex);
this.enabled = new Boolean(enabled);
this.isEnabled = isEnabled;
function isEnabled(){
return this.enabled.valueOf();
}
this.setEnabled=setEnabled;
function setEnabled(en){
console.log("Set enabled for option("+this.strRegex+") called with value: " + en);
this.enabled = new Boolean(en);
console.log(" Now, it is: " + this.enabled.valueOf());
}
}
document.addEventListener('DOMContentLoaded', onScriptStart);
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('btnSave').addEventListener('click', saveOptions);
document.getElementById('btnRestore').addEventListener('click', loadDefaults);
});