Instantly share code, notes, and snippets.
Created
July 4, 2016 20:13
-
Save bobsilverberg/b77f8de3b6bc1c64b3898f1bd48d1372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require.scopes["popupBlocker"] = (function() | |
{ | |
"use strict"; | |
var exports = {}; | |
var defaultMatcher = require("matcher").defaultMatcher; | |
var BlockingFilter = require("filterClasses").BlockingFilter; | |
var _tempVar19 = require("url"); | |
var stringifyURL = _tempVar19.stringifyURL; | |
var isThirdParty = _tempVar19.isThirdParty; | |
var extractHostFromFrame = _tempVar19.extractHostFromFrame; | |
var checkWhitelisted = require("whitelisting").checkWhitelisted; | |
var logRequest = require("devtools").logRequest; | |
var loadingPopups = Object.create(null); | |
function hasLoadingPopups() | |
{ | |
return Object.keys(loadingPopups).length > 0; | |
} | |
function forgetPopup(tabId) | |
{ | |
delete loadingPopups[tabId]; | |
if (!hasLoadingPopups()) | |
{ | |
chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequest); | |
chrome.webNavigation.onCompleted.removeListener(onCompleted); | |
chrome.tabs.onRemoved.removeListener(forgetPopup); | |
} | |
} | |
function getFrame(tabId, processId, frameId, callback) | |
{ | |
chrome.webNavigation.getFrame( | |
{ | |
tabId: tabId, | |
processId: processId, | |
frameId: frameId | |
}, function(details) | |
{ | |
var _tempVar20 = details; | |
var url = _tempVar20.url; | |
var parentFrameId = _tempVar20.parentFrameId; | |
var frame = { | |
url: new URL(url), | |
parent: null | |
}; | |
if (parentFrameId != -1) | |
{ | |
getFrame(tabId, processId, parentFrameId, function(parentFrame) | |
{ | |
frame.parent = parentFrame; | |
callback(frame); | |
}); | |
} | |
else | |
{ | |
callback(frame); | |
} | |
}.bind(this)); | |
} | |
function checkPotentialPopup(tabId, popup) | |
{ | |
var urlObj = new URL(popup.url || "about:blank"); | |
var urlString = stringifyURL(urlObj); | |
var documentHost = extractHostFromFrame(popup.sourceFrame); | |
var thirdParty = isThirdParty(urlObj, documentHost); | |
var specificOnly = !!checkWhitelisted(popup.sourcePage, popup.sourceFrame, RegExpFilter.typeMap.GENERICBLOCK); | |
var filter = defaultMatcher.matchesAny(urlString, RegExpFilter.typeMap.POPUP, documentHost, thirdParty, null, specificOnly); | |
if (filter instanceof BlockingFilter) | |
{ | |
chrome.tabs.remove(tabId); | |
} | |
logRequest(popup.sourcePage, urlString, "POPUP", documentHost, thirdParty, null, specificOnly, filter); | |
} | |
function onBeforeRequest(details) | |
{ | |
var popup = loadingPopups[details.tabId]; | |
if (popup) | |
{ | |
popup.url = details.url; | |
if (popup.sourceFrame) | |
{ | |
checkPotentialPopup(details.tabId, popup); | |
} | |
} | |
} | |
function onCompleted(details) | |
{ | |
if (details.frameId == 0 && details.url != "about:blank") | |
{ | |
forgetPopup(details.tabId); | |
} | |
} | |
function onSourceFrameRetrieved(tabId, frame) | |
{ | |
var popup = loadingPopups[tabId]; | |
if (popup) | |
{ | |
if (checkWhitelisted(popup.sourcePage, frame)) | |
{ | |
forgetPopup(tabId); | |
} | |
else | |
{ | |
popup.sourceFrame = frame; | |
checkPotentialPopup(tabId, popup); | |
} | |
} | |
} | |
chrome.webNavigation.onCreatedNavigationTarget.addListener(function(details) | |
{ | |
if (!hasLoadingPopups()) | |
{ | |
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest, | |
{ | |
urls: ["<all_urls>"], | |
types: ["main_frame"] | |
}); | |
chrome.webNavigation.onCompleted.addListener(onCompleted); | |
chrome.tabs.onRemoved.addListener(forgetPopup); | |
} | |
loadingPopups[details.tabId] = { | |
url: details.url, | |
sourcePage: new ext.Page( | |
{ | |
id: details.sourceTabId | |
}), | |
sourceFrame: null | |
}; | |
getFrame(details.sourceTabId, details.sourceProcessId, details.sourceFrameId, onSourceFrameRetrieved.bind(null, details.tabId)); | |
}); | |
return exports; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment