Skip to content

Commit c6793ef

Browse files
committed
code review re. gorhill#2067
1 parent 8c3da95 commit c6793ef

2 files changed

Lines changed: 51 additions & 38 deletions

File tree

src/js/start.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ var onAllReady = function() {
7979

8080
//quickProfiler.stop(0);
8181

82-
vAPI.onLoadAllCompleted();
8382
µb.contextMenu.update(null);
8483
µb.firstInstall = false;
8584

86-
vAPI.net.onBeforeReady = null;
87-
vAPI.messaging.broadcast({ what: 'ublockOrigin-readyState-complete' });
85+
vAPI.net.onReady();
8886
};
8987

9088
/******************************************************************************/

src/js/traffic.js

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,59 @@ var exports = {};
3333

3434
/******************************************************************************/
3535

36+
// https://github.com/gorhill/uBlock/issues/2067
37+
// Experimental: Suspend tabs until uBO is fully ready.
38+
39+
vAPI.net.onReady = function() {
40+
if ( µBlock.hiddenSettings.suspendTabsUntilReady !== true ) {
41+
vAPI.onLoadAllCompleted();
42+
}
43+
var fn = onBeforeReady;
44+
onBeforeReady = null;
45+
if ( fn !== null ) {
46+
fn('ready');
47+
}
48+
};
49+
50+
var onBeforeReady = (function() {
51+
var suspendedTabs = new Set();
52+
53+
var forceReloadSuspendedTabs = function() {
54+
var iter = suspendedTabs.values(),
55+
entry;
56+
for (;;) {
57+
entry = iter.next();
58+
if ( entry.done ) { break; }
59+
vAPI.tabs.reload(entry.value);
60+
}
61+
};
62+
63+
return function(tabId) {
64+
if (
65+
vAPI.isBehindTheSceneTabId(tabId) ||
66+
µBlock.hiddenSettings.suspendTabsUntilReady !== true
67+
) {
68+
return;
69+
}
70+
if ( tabId === 'ready' ) {
71+
forceReloadSuspendedTabs();
72+
return;
73+
}
74+
suspendedTabs.add(tabId);
75+
return true;
76+
};
77+
})();
78+
79+
/******************************************************************************/
80+
3681
// Intercept and filter web requests.
3782

3883
var onBeforeRequest = function(details) {
84+
var tabId = details.tabId;
85+
if ( onBeforeReady !== null && onBeforeReady(tabId) ) {
86+
return { cancel: true };
87+
}
88+
3989
// Special handling for root document.
4090
// https://github.com/chrisaljoudi/uBlock/issues/1001
4191
// This must be executed regardless of whether the request is
@@ -46,7 +96,6 @@ var onBeforeRequest = function(details) {
4696
}
4797

4898
// Special treatment: behind-the-scene requests
49-
var tabId = details.tabId;
5099
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
51100
return onBeforeBehindTheSceneRequest(details);
52101
}
@@ -138,13 +187,6 @@ var onBeforeRequest = function(details) {
138187
/******************************************************************************/
139188

140189
var onBeforeRootFrameRequest = function(details) {
141-
if (
142-
vAPI.net.onBeforeReady instanceof Function &&
143-
vAPI.net.onBeforeReady(details) === true
144-
) {
145-
return { cancel: true };
146-
}
147-
148190
var tabId = details.tabId,
149191
requestURL = details.url,
150192
µb = µBlock;
@@ -642,33 +684,6 @@ var headerIndexFromName = function(headerName, headers) {
642684

643685
/******************************************************************************/
644686

645-
// https://github.com/gorhill/uBlock/issues/2067
646-
// Experimental: Suspend tabs until uBO is fully ready.
647-
648-
vAPI.net.onBeforeReady = function(details) {
649-
if ( µBlock.hiddenSettings.suspendTabsUntilReady !== true ) {
650-
return;
651-
}
652-
var pageURL = details.url;
653-
if ( /^https?:\/\//.test(pageURL) === false ) {
654-
return;
655-
}
656-
if (
657-
details.tabId === -1 ||
658-
details.type !== 'main_frame' ||
659-
details.frameId !== 0
660-
) {
661-
return;
662-
}
663-
vAPI.tabs.replace(
664-
details.tabId,
665-
vAPI.getURL('document-suspended.html?url=') + encodeURIComponent(pageURL)
666-
);
667-
return true;
668-
};
669-
670-
/******************************************************************************/
671-
672687
vAPI.net.onBeforeRequest = {
673688
urls: [
674689
'http://*/*',

0 commit comments

Comments
 (0)