forked from gorhill/httpswitchboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
183 lines (136 loc) · 5.96 KB
/
start.js
File metadata and controls
183 lines (136 loc) · 5.96 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
/*******************************************************************************
httpswitchboard - a Chromium browser extension to black/white list requests.
Copyright (C) 2013 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/httpswitchboard
*/
// ORDER IS IMPORTANT
/******************************************************************************/
HTTPSB.turnOn();
/******************************************************************************/
function onTabCreated(tab) {
// Can this happen?
if ( tab.id < 0 || !tab.url || tab.url === '' ) {
return;
}
// https://github.com/gorhill/httpswitchboard/issues/303
// This takes care of rebinding the tab to the proper page store
// when the user navigate back in his history.
HTTPSB.bindTabToPageStats(tab.id, tab.url);
}
chrome.tabs.onCreated.addListener(onTabCreated);
/******************************************************************************/
function onTabUpdated(tabId, changeInfo, tab) {
// Can this happen?
if ( !tab.url || tab.url === '' ) {
return;
}
// https://github.com/gorhill/httpswitchboard/issues/303
// This takes care of rebinding the tab to the proper page store
// when the user navigate back in his history.
if ( changeInfo.url ) {
HTTPSB.bindTabToPageStats(tabId, tab.url);
}
// rhill 2013-12-23: Compute state after whole page is loaded. This is
// better than building a state snapshot dynamically when requests are
// recorded, because here we are not afflicted by the browser cache
// mechanism.
// rhill 2014-03-05: Use tab id instead of page URL: this allows a
// blocked page using HTTPSB internal data URI-based page to be properly
// unblocked when user un-blacklist the hostname.
// https://github.com/gorhill/httpswitchboard/issues/198
if ( changeInfo.status === 'complete' ) {
var pageStats = HTTPSB.pageStatsFromTabId(tabId);
if ( pageStats ) {
pageStats.state = HTTPSB.computeTabState(tabId);
}
}
}
chrome.tabs.onUpdated.addListener(onTabUpdated);
/******************************************************************************/
function onTabRemoved(tabId) {
// Can this happen?
if ( tabId < 0 ) {
return;
}
HTTPSB.unbindTabFromPageStats(tabId);
}
chrome.tabs.onRemoved.addListener(onTabRemoved);
/******************************************************************************/
// Bind a top URL to a specific tab
function onBeforeNavigateCallback(details) {
// Don't bind to a subframe
if ( details.frameId > 0 ) {
return;
}
// console.debug('onBeforeNavigateCallback() > "%s" = %o', details.url, details);
HTTPSB.bindTabToPageStats(details.tabId, details.url);
}
chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigateCallback);
/******************************************************************************/
// Load everything
HTTPSB.load();
/******************************************************************************/
// rhill 2013-11-24: bind behind-the-scene virtual tab/url manually, since the
// normal way forbid binding behind the scene tab.
// https://github.com/gorhill/httpswitchboard/issues/67
HTTPSB.createPageStats(HTTPSB.behindTheSceneURL);
HTTPSB.pageUrlToTabId[HTTPSB.behindTheSceneURL] = HTTPSB.behindTheSceneTabId;
HTTPSB.tabIdToPageUrl[HTTPSB.behindTheSceneTabId] = HTTPSB.behindTheSceneURL;
/******************************************************************************/
// Initialize internal state with maybe already existing tabs
chrome.tabs.query({ url: '<all_urls>' }, function(tabs) {
var i = tabs.length;
// console.debug('HTTP Switchboard > preparing to bind %d tabs', i);
while ( i-- ) {
HTTPSB.bindTabToPageStats(tabs[i].id, tabs[i].url);
}
// Tabs are now bound to url stats stores, therefore it is now safe
// to handle net traffic.
HTTPSB.webRequest.checklist('tabsBound');
});
/******************************************************************************/
// Browser data jobs
(function() {
var jobCallback = function() {
var httpsb = HTTPSB;
if ( !httpsb.userSettings.clearBrowserCache ) {
return;
}
httpsb.clearBrowserCacheCycle -= 15;
if ( httpsb.clearBrowserCacheCycle > 0 ) {
return;
}
httpsb.clearBrowserCacheCycle = httpsb.userSettings.clearBrowserCacheAfter;
httpsb.browserCacheClearedCounter++;
chrome.browsingData.removeCache({ since: 0 });
// console.debug('clearBrowserCacheCallback()> chrome.browsingData.removeCache() called');
};
HTTPSB.asyncJobs.add('clearBrowserCache', null, jobCallback, 15 * 60 * 1000, true);
})();
/******************************************************************************/
// Automatic update of non-user assets
// https://github.com/gorhill/httpswitchboard/issues/334
(function() {
var httpsb = HTTPSB;
var jobDone = function(details) {
if ( details.changedCount === 0 ) {
return;
}
httpsb.loadUpdatableAssets();
};
var jobCallback = function() {
httpsb.assetUpdater.update(null, jobDone);
};
httpsb.asyncJobs.add('autoUpdateAssets', null, jobCallback, httpsb.updateAssetsEvery, true);
})();
/******************************************************************************/