-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
261 lines (224 loc) · 9.11 KB
/
background.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
//var pattern = /(.*\/)([\d]+)([\/]?[.\w]{0,5}$)/; //if the URL ends in a number, which is preceeded by a forward slash, and optionally followed by a slash and/or optionally followed by an extension of up to 5 characters (including decimal)
var patterns;
var defaultDelta;
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());
}
}
function validateUrl(tab){
loadSavedPatterns();
for(var i in patterns){
if(patterns[i].enabled.valueOf() && tab.url.match(patterns[i].objRegex)){
console.log("URL: <" + tab.url + "> matched regular expression: " + patterns[i].strRegex);
return true;
}else if(!patterns[i].enabled.valueOf()){
console.log("URL: <" + tab.url + "> matched DISABLED regular expression: " + patterns[i].strRegex);
}else{
console.log("URL: <" + tab.url + "> DID NOT match regular expression: " + patterns[i].strRegex);
}
}
return false;
}
function checkForUrlMatch(tabId, changeInfo, tab) {
var isGoodUrl;// = tab.url.match(pattern);
isGoodUrl = validateUrl(tab);
//tab.url.match(/github\.com/);
// If the URL of the tab contains the string github.com
if (isGoodUrl) {
//show page action on this tab
chrome.pageAction.show(tabId);
console.log("Adding page action to tab #",tabId," with URL: ",tab.url);
}
else
{
chrome.pageAction.hide(tabId);
console.log("Removing page action from tab: ", tabId);
}
};
//Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForUrlMatch);
//document.addEventListener('DOMContentLoaded', checkForUrlMatch);//do this so that if the script is just loaded, the current tab will be checked immediately.
console.log("hello?");
if(localStorage["delta"]==undefined || Number.isNaN(localStorage["delta"])){
defaultDelta=1;
console.log("Delta value could not be loaded from storage in background... default: " + defaultDelta);
}else{
defaultDelta=Number(localStorage["delta"]);
console.log("Delta value loaded from storage in background: " + defaultDelta);
}
function loadSavedPatterns(){
if(localStorage["options"]==null || localStorage["options"]==undefined){
console.log("Options array was "+(localStorage["options"]==null ? "null" : "undefined") + " on entry in background.");
patterns = new Array();
/**
The below code is all from options.js:loadDefaults()
*/
patterns[0] = new objOption("()([\\d]{1,16})($)", true);
patterns[1] = new objOption("(.*\\/[\\d]*?)([\\d]{1,16})([\\/]?[.\\w]{0,5}$)", true);
localStorage["options"]=JSON.stringify(patterns);
console.log("Default options loaded and saved.");
/** */
}
else{
console.log("Options array was found saved in background!");
var temp = JSON.parse(localStorage["options"]);
patterns = new Array();
for(var i in temp){
console.log("Parsing object["+i+"]: " + temp[i]);
patterns[i] = new objOption(temp[i].strRegex, temp[i].enabled.valueOf());
}
}
}
function getSavedPadOption(){
if(localStorage["pad"]==null || localStorage["pad"]==undefined){
console.log("Pad option was "+(localStorage["pad"]==null ? "null" : "undefined") + " on load in background.");
localStorage["pad"]="ask";//default option
}
return (localStorage["pad"]);
}
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse){
console.log("Message received..");
if(request.msg == "increment"){
if(request.validate && request.validate=="no" && request.tab){
changeURL(defaultDelta, request.tab, (request.pad?request.pad=='yes':null));
}else if(sender.tab && validateUrl(sender.tab)){
changeURL(defaultDelta, sender.tab, (request.pad?request.pad=='yes':null));
}else if(!sender.tab){
console.error("Sender was not a tab, background could not verify increment request.");
}else{
console.log("Tab with invalid URL tried to ask for increment service w/validation from background. Failed.");
}
}else if(request.msg == "decrement"){
if(request.validate && request.validate=="no" && request.tab){
changeURL(-1*defaultDelta, request.tab, (request.pad?request.pad=='yes':null));
}else if(sender.tab && validateUrl(sender.tab)){
changeURL(-1*defaultDelta, sender.tab, (request.pad?request.pad=='yes':null));
}else if(!sender.tab){
console.error("Sender was not a tab, background could not verify decrement request.");
}else{
console.log("Tab with invalid URL tried to ask for decrement service w/validation from background. Failed.");
}
}else if(request.msg == "updateDelta"){
defaultDelta=request.delta;
console.log("Default delta value updated to: " + defaultDelta);
}else{
console.log("ERROR: unknown message sent. Contents: " + request.msg);
return;
}
}
);
function changeURL(delta, tab, pad){
//chrome.tabs.getSelected(null, function(tab){
console.log("INCREMENT/DECREMENT by: "+delta+" in background, from URL: "+tab.url);
if(!patterns || patterns=='undefined'){
console.log("Current tab needs to load defaults for background script.. hasn't had the chance yet!");
loadSavedPatterns();
}
var oldurl = tab.url;
var newurl = null;
var alerted=false;
for(var i in patterns){
var r = new RegExp(patterns[i].strRegex);
console.log("Regex to check: " + r);
newurl= oldurl.replace(r, function(fullMatch, pre, num, post){
console.log("Full match: " + fullMatch);
console.log("Pre: " + pre);
console.log("Int parsed for incrementation was: " + num);
console.log("Post: " + post);
if(Number(num)==Number.NaN){
console.log("Err: No number found to increment using regex: "+ patterns[i]);
return fullMatch;
}
else{
console.log("Int parsed for incrementation was (as a number): " + Number(num));
}
if(Number(num) + delta < 0){
console.error("Cannot handle negative numbers...");
if(!alerted){//don't bother whining about a negative number if we already did
alert("inCHROMEnter cannot produce negative numbers in URLs, because future string parsing will not interpret the negative sign. Sorry!");
alerted = true;
}
return fullMatch;
}
//old strategy
//return pre + (Number(num)+delta) + post;
//new strategy needed so that leading zeroes are respected
//09->10
//09->08
//10->09 or 10->9 (user's choice...) selected by 'pad' argument
//if pad is null, then we need to ask user with an alert
//otherwise, they chose somewhere and we can assume they knew what they wanted
newLength = String(Number(num)+delta).length;
oldLength = String(num).length;
newString = String(Number(num)+delta)
if(newLength < oldLength){
console.log("Considering leading zeroes.");
//may need to consider leading zeroes...
exp = Math.pow(10,oldLength-1)
if(Number(exp + delta + (Number(num) % exp ))==(Number(num)+delta)){
console.log("\tDigit boundary crossed.");
//number rolled over from 10->9 or 100->99 or 1000->999, for example
if(pad==null){
tryToLoadPad=getSavedPadOption();
if(tryToLoadPad=="ask"){
pad=confirm("Do you want to add a leading zero to decremented number?");
}else{
pad=(tryToLoadPad=="yes"?true:false);
console.log("\tDefault pad option from options page being used: " + pad);
}
}
if(pad){
console.log("\tPadding... old: " + newString);
for(i=0; i<(oldLength-newLength);i++){
newString="0".concat(newString);
}
console.log("\tPadded... new: " + newString);
}
}else{
//did not roll over a digit boundary. this is a case like
//09->08, etc.
//just do it automatically
console.log("\tAuto Padding... old: " + newString);
for(i=0; i<(oldLength-newLength);i++){
newString="0".concat(newString);
}
console.log("\tAuto Padded... new: " + newString);
}
}
console.log("Padding considered, new url:" + pre + newString + post);
return pre + newString + post;
});
if(newurl!=null && newurl!=oldurl){
console.log("New URL: " + newurl);
chrome.tabs.update(tab.tabId, {url: newurl});
return;
}
else if(newurl==oldurl){
console.log("No match...or negative number was generated.");
}
}
console.log("******ERROR: NO regex matched!");
//});
}
/*
console.log("Background.. checking addition. 3072397517134438 + 1 = " + (3072397517134438 + 1));
console.log("Background.. checking addition. 30723975171344384 + 1 = " + (30723975171344384 + 1));
console.log("Background.. checking addition. 307239751713443840 + 1 = " + (307239751713443840 + 1));
console.log("Background.. checking addition. Max value: " + Number.MAX_VALUE);
var x = new Number(307239751713443840 + 1);
console.log("Background.. checking addition. x = " + x.toString());
x = new Number(307239751713443840);
x=x+1;
console.log("Background.. checking addition. x = " + x.toString());*/