-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
117 lines (97 loc) · 3.22 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
var storage = chrome.storage.local;
var pluginOptions = {
options: {
enabled: false,
routes: []
},
saveSettings: function() {
storage.set({'options': this.options}, function() {
var notification = webkitNotifications.createNotification(
'bulldozer48.png',
'Saved!',
'Your settings were saved...'
);
notification.show();
});
},
loadSettings: function(f) {
var self = this;
storage.get('options', function(data) {
if (data.hasOwnProperty("options")) {
self.options = data.options;
}
f();
});
},
attachEnable: function() {
var self = this;
$('#pluginEnabled').change(function() {
self.options.enabled = $(this).is(':checked');
self.saveSettings();
});
self.loadSettings(function() {
if (self.options.enabled){
$('#pluginEnabled').prop("checked", true);
} else {
$('#pluginEnabled').prop("checked", false);
}
});
},
attachRoutes: function() {
var self = this;
var loadRoutes = function() {
var routes = [];
if (self.options.routes) {
for(var i = 0; i < self.options.routes.length; i++) {
var val = self.options.routes[i];
routes.push(val[0] + "," + val[1]);
}
}
$("#redirects").val(routes.join('\n'));
};
$('#save_redirects').on('click', function(e) {
self.options.routes = [];
var redirects = $('#redirects').val().split('\n');
for(var i = 0; i < redirects.length; i++) {
parts = redirects[i].split(',');
if (parts.length == 2) {
self.options.routes.push([parts[0].trim(), parts[1].trim()]);
}
}
self.saveSettings();
loadRoutes();
return false;
});
self.loadSettings(loadRoutes);
},
setupRequestInterceptor: function() {
var self = this;
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (self.options.enabled) {
var redirectTo = null;
for(var i = 0; i < self.options.routes.length; i++) {
var val = self.options.routes[i];
if (details.url.search(val[0]) != -1) {
redirectTo = details.url.replace(val[0], val[1]);
}
}
if (redirectTo != null) {
console.log("Redirecting: " + details.url + " to " + redirectTo);
return {redirectUrl: redirectTo};
}
}
return null;
},
{urls: ["<all_urls>"]},
["blocking"]);
},
attachControls: function() {
this.attachEnable();
this.attachRoutes();
}
};
$(function(){
pluginOptions.attachControls();
pluginOptions.setupRequestInterceptor();
});