Repository navigation
Expand file tree
/
Copy pathbg.js
More file actions
185 lines (150 loc) · 4.69 KB
/
Copy pathbg.js
File metadata and controls
185 lines (150 loc) · 4.69 KB
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
;(function(){
// -------------------------------------------------------------------------- //
// Background page starts from here.
// -------------------------------------------------------------------------- //
const SETTINGS_KEY = 'settings';
const ENABLED_KEY = 'enabled';
var notifications = chrome.notifications;
var tabs = chrome.tabs;
const Interceptor = function() {
this.notes = [];
this._enabled = false;
};
Interceptor.prototype.init = async function() {
const filter = {
urls: [ "*://*/*" ]
};
this.reload();
this._enabled = await this._getEnabled();
this._syncButtonState();
// chrome.browserAction.onClicked.addListener(this._clearAll.bind(this));
chrome.browserAction.onClicked.addListener(this._toggleEnabled.bind(this));
chrome.webRequest.onBeforeRequest.addListener(this.handle.bind(this), filter);
return this;
};
// Interceptor.prototype._clearAll = function(tab) {
// this.notes.forEach(note => note.cancel());
// };
Interceptor.prototype.handle = function(details) {
var url = details.url;
if (!this._enabled) {
return;
}
if (!this._matchRules(url)) {
return;
}
if (details.tabId > -1) {
var tab = tabs.get(
details.tabId,
function(tab) {
if (!tab) {
return;
}
this._notify(tab.url, url);
}.bind(this)
);
} else {
this._notify('NO TAB URL', url);
}
};
Interceptor.prototype._notify = function(page_url, request_url) {
// Double html decode.
var ta = document.createElement("textarea");
var val = document.createTextNode(decodeURIComponent(request_url));
ta.appendChild(val);
ta.innerHTML = ta.innerText;
if (!prompt(ta.innerText, ta.innerText)) {
return;
}
// TODO not used
// var notification = notifications
// .createNotification(
// 'icon.png',
// page_url,
// request_url
// );
// this.notes.push(notification);
// notification.onclose = function() { this._remove(notification); }.bind(this);
// notification.show();
// this.updateCount();
};
Interceptor.prototype._remove = function(note) {
for (var i = 0; i < this.notes.length; i++) {
if (this.notes[i] === note) {
this.notes.splice(i, 1);
}
}
this.updateCount();
};
Interceptor.prototype._matchRules = function(url) {
for (var i = 0; i < this.rules.length; i++) {
if (this.rules[i].test(url)) {
return true;
}
}
return false;
};
Interceptor.prototype.updateCount = function(diff) {
var count = this.notes.length;
if (count === 0) {
chrome.browserAction.setBadgeText({ "text": "" });
} else {
chrome.browserAction.setBadgeBackgroundColor({ "color": [255,51,51, 255]});
chrome.browserAction.setBadgeText({ "text": " " + count + " " });
}
};
Interceptor.prototype._getSettings = function() {
return new Promise((resolve) => {
chrome.storage.local.get([ SETTINGS_KEY ], (result) => {
let settings = result[SETTINGS_KEY];
if (!settings || !settings.rules) {
settings = {
rules: []
};
}
resolve(settings);
});
});
};
Interceptor.prototype._getEnabled = function() {
return new Promise((resolve) => {
chrome.storage.local.get([ ENABLED_KEY ], (result) => {
let enabled = result[ENABLED_KEY];
enabled = typeof enabled === 'boolean' ? enabled : true;
resolve(enabled);
});
});
};
Interceptor.prototype.reload = async function() {
const settings = await this._getSettings();
this.rules = settings.rules.map(this._createRule.bind(this));
};
Interceptor.prototype._createRule = function(template) {
return new RegExp(template, "gi");
};
Interceptor.prototype._toggleEnabled = function() {
this._enabled = !this._enabled;
this._syncButtonState();
this._saveEnabled();
};
Interceptor.prototype._saveEnabled = function() {
return new Promise((resolve) => {
chrome.storage.local.set({ [ENABLED_KEY]: this._enabled }, resolve);
});
};
Interceptor.prototype._syncButtonState = function() {
if (this._enabled) {
chrome.browserAction.setBadgeText({ 'text': '' });
} else {
chrome.browserAction.setBadgeBackgroundColor({ 'color': '#E84C3D' });
chrome.browserAction.setBadgeText({ 'text': 'off' });
}
};
// Initialization.
var onload = setTimeout(init, 0);
function init() {
const faviconer = new Interceptor();
faviconer.init();
window.faviconer = faviconer;
}
}());