Skip to content

Commit ddd101c

Browse files
committed
Fix popup blocking (gorhill#11)
Should now close filtered popups
1 parent 5c852b1 commit ddd101c

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

platform/safari/vapi-background.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,14 @@
384384
if(!vAPI.tabs.popupCandidate || !e.target || e.url === "about:blank") {
385385
return;
386386
}
387-
var url = e.url,
388-
tabId = vAPI.tabs.getTabId(e.target);
389-
var details = {
390-
targetURL: url,
391-
targetTabId: tabId.toString(),
392-
openerTabId: vAPI.tabs.popupCandidate
393-
};
387+
var targetUrl = e.url,
388+
targetTabId = vAPI.tabs.getTabId(e.target).toString(),
389+
openerTabId = vAPI.tabs.popupCandidate;
394390
vAPI.tabs.popupCandidate = false;
395-
if(vAPI.tabs.onPopup(details)) {
391+
if(vAPI.tabs.onPopupUpdated(targetTabId, openerTabId, targetUrl)) {
396392
e.preventDefault();
397-
if(vAPI.tabs.stack[details.openerTabId]) {
398-
vAPI.tabs.stack[details.openerTabId].activate();
393+
if(vAPI.tabs.stack[openerTabId]) {
394+
vAPI.tabs.stack[openerTabId].activate();
399395
}
400396
}
401397
}, true);
@@ -1067,24 +1063,18 @@
10671063
var blockVerdict = onHeadersReceivedClient(e.message);
10681064
if(blockVerdict && blockVerdict.responseHeaders) {
10691065
e.message = false;
1070-
}
1071-
else {
1066+
} else {
10721067
e.message = true;
10731068
}
10741069
return;
10751070
}
10761071
switch(e.message.type) {
10771072
case "popup":
10781073
var openerTabId = vAPI.tabs.getTabId(e.target).toString();
1079-
var shouldBlock = !!vAPI.tabs.onPopup({
1080-
targetURL: e.message.url,
1081-
targetTabId: "preempt",
1082-
openerTabId: openerTabId
1083-
});
1084-
if(shouldBlock) {
1074+
var shouldBlock = !!vAPI.tabs.onPopupUpdated("preempt", openerTabId, e.message.url);
1075+
if (shouldBlock) {
10851076
e.message = false;
1086-
}
1087-
else {
1077+
} else {
10881078
vAPI.tabs.popupCandidate = openerTabId;
10891079
e.message = true;
10901080
}
@@ -1103,13 +1093,11 @@
11031093
if(blockVerdict && blockVerdict.cancel) {
11041094
e.message = false;
11051095
return;
1106-
}
1107-
else {
1096+
} else {
11081097
e.message = true;
11091098
return;
11101099
}
11111100
}
1112-
return;
11131101
};
11141102
safari.application.addEventListener("message", onBeforeRequestAdapter, true);
11151103
};

src/js/tab.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -698,18 +698,22 @@ vAPI.tabs.onPopupUpdated = (function() {
698698
);
699699
};
700700

701-
return function(targetTabId, openerTabId) {
701+
return function(targetTabId, openerTabId, targetURL) {
702+
var preempt = targetTabId === 'preempt';
703+
702704
// Opener details.
703705
var tabContext = µb.tabContextManager.lookup(openerTabId);
704706
if ( tabContext === null ) { return; }
705707
var openerURL = tabContext.rawURL;
706708
if ( openerURL === '' ) { return; }
707709

708710
// Popup details.
709-
tabContext = µb.tabContextManager.lookup(targetTabId);
710-
if ( tabContext === null ) { return; }
711-
var targetURL = tabContext.rawURL;
712-
if ( targetURL === '' ) { return; }
711+
if ( targetURL === undefined ) {
712+
tabContext = µb.tabContextManager.lookup(targetTabId);
713+
if ( tabContext === null ) { return; }
714+
targetURL = tabContext.rawURL;
715+
if ( targetURL === '' ) { return; }
716+
}
713717

714718
// https://github.com/gorhill/uBlock/issues/341
715719
// Allow popups if uBlock is turned off in opener's context.
@@ -774,6 +778,11 @@ vAPI.tabs.onPopupUpdated = (function() {
774778
µb.updateBadgeAsync(openerTabId);
775779
}
776780

781+
if ( preempt ) {
782+
// Block before tab is created
783+
return true;
784+
}
785+
777786
// It is a popup, block and remove the tab.
778787
if ( popupType === 'popup' ) {
779788
µb.unbindTabFromPageStats(targetTabId);

0 commit comments

Comments
 (0)