Index: webrequest.js |
=================================================================== |
--- a/webrequest.js |
+++ b/webrequest.js |
@@ -4,39 +4,37 @@ |
* http://mozilla.org/MPL/2.0/. |
*/ |
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]); |
chrome.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, {urls: ["http://*/*", "https://*/*"]}, ["requestHeaders", "blocking"]); |
chrome.tabs.onRemoved.addListener(forgetTab); |
var frames = {}; |
-var tabs = {}; |
function onBeforeRequest(details) |
{ |
+ if (details.tabId == -1) |
+ return {}; |
+ |
var type = details.type; |
if (type == "main_frame" || type == "sub_frame") |
- recordFrame(details.tabId, details.frameId, details.url, type == "main_frame"); |
+ recordFrame(details.tabId, details.frameId, details.parentFrameId, details.url, type == "main_frame"); |
Felix Dahlke
2012/09/18 13:23:14
type == "main_frame" is redundant here, I'd assign
|
if (type == "main_frame") |
- return; |
+ return {}; |
// Type names match Mozilla's with main_frame and sub_frame being the only exceptions. |
if (type == "sub_frame") |
type = "SUBDOCUMENT"; |
else |
type = type.toUpperCase(); |
- var documentUrl = getFrameUrl(details.tabId, details.frameId); |
- var topUrl = getTabUrl(details.tabId); |
- if (type == "SUBDOCUMENT") |
- documentUrl = getFrameUrl(details.tabId, details.parentFrameId) || topUrl; |
- |
- var filter = checkRequest(type, details.url, documentUrl, topUrl); |
+ var frame = (type != "SUBDOCUMENT" ? details.frameId : details.parentFrameId); |
+ var filter = checkRequest(type, details.tabId, details.url, frame); |
if (filter instanceof BlockingFilter) |
{ |
var collapse = filter.collapse; |
if (collapse == null) |
collapse = (localStorage["hidePlaceholders"] != "false"); |
if (collapse && type == "SUBDOCUMENT") |
return {redirectUrl: "about:blank"}; |
else if (collapse && type == "IMAGE") |
@@ -58,51 +56,61 @@ function onBeforeSendHeaders(details) |
{ |
headers.push({name: "DNT", value: "1"}); |
return {requestHeaders: headers}; |
} |
} |
return null; |
} |
-function recordFrame(tabId, frameId, frameUrl, isMain) |
+function recordFrame(tabId, frameId, parentFrameId, frameUrl, isMain) |
Felix Dahlke
2012/09/18 13:23:14
This parameter is no longer used, it could be remo
|
{ |
if (!(tabId in frames)) |
frames[tabId] = {}; |
- frames[tabId][frameId] = frameUrl; |
- |
- if (isMain) |
- tabs[tabId] = frameUrl; |
+ frames[tabId][frameId] = {url: frameUrl, parent: parentFrameId}; |
} |
function getFrameUrl(tabId, frameId) |
{ |
if (tabId in frames && frameId in frames[tabId]) |
- return frames[tabId][frameId]; |
+ return frames[tabId][frameId].url; |
return null; |
} |
-function getTabUrl(tabId) |
+function getFrameParent(tabId, frameId) |
{ |
- if (tabId in tabs) |
- return tabs[tabId]; |
- return null; |
+ if (tabId in frames && frameId in frames[tabId]) |
+ return frames[tabId][frameId].parent; |
+ return -1; |
} |
function forgetTab(tabId) |
{ |
delete frames[tabId]; |
- delete tabs[tabId]; |
} |
-function checkRequest(type, url, documentUrl, topUrl) |
+function checkRequest(type, tabId, url, frameId) |
{ |
- if (topUrl && isWhitelisted(topUrl)) |
+ if (isFrameWhitelisted(tabId, frameId)) |
return false; |
+ var documentUrl = getFrameUrl(tabId, frameId); |
if (!documentUrl) |
- documentUrl = topUrl; |
+ return false; |
var requestHost = extractHostFromURL(url); |
var documentHost = extractHostFromURL(documentUrl); |
var thirdParty = isThirdParty(requestHost, documentHost); |
return defaultMatcher.matchesAny(url, type, documentHost, thirdParty); |
} |
+ |
+function isFrameWhitelisted(tabId, frameId, type) |
+{ |
+ var parent = frameId; |
+ while (parent != -1) |
+ { |
+ var parentUrl = getFrameUrl(tabId, parent); |
+ if (parentUrl && isWhitelisted(parentUrl, type)) |
+ return true; |
+ parent = getFrameParent(tabId, parent); |
+ } |
+ return false; |
+} |