Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/requestBlocker.js

Issue 29737568: Issue 4580 - Removed ext.webRequest.onBeforeRequest (Closed)
Patch Set: Created March 30, 2018, 11:18 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ext/background.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/requestBlocker.js
===================================================================
--- a/lib/requestBlocker.js
+++ b/lib/requestBlocker.js
@@ -90,19 +90,53 @@
}
}
-ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) =>
+browser.webRequest.onBeforeRequest.addListener(details =>
{
+ // Never block top-level documents.
+ if (details.type == "main_frame")
+ return;
+
+ // Filter out requests from non web protocols. Ideally, we'd explicitly
+ // specify the protocols we are interested in (i.e. http://, https://,
+ // ws:// and wss://) with the url patterns, given below, when adding this
+ // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket
+ // protocol and is causing an error if it is given.
+ let url = new URL(details.url);
+ if (url.protocol != "http:" && url.protocol != "https:" &&
+ url.protocol != "ws:" && url.protocol != "wss:")
+ return;
kzar 2018/04/05 11:26:26 Please could you add the braces here, to be consis
Sebastian Noack 2018/04/05 16:04:09 See below.
+
+ // Firefox (only) allows to intercept requests sent by the browser
+ // and other extensions. We don't want to block these.
+ if (details.originUrl)
+ {
+ let originUrl = new URL(details.originUrl);
+ if (originUrl.protocol == "chrome:" ||
+ originUrl.protocol == "moz-extension:")
+ return;
kzar 2018/04/05 11:26:26 Please can you add the braces back, since the rule
Sebastian Noack 2018/04/05 16:04:09 Since when is this the rule? This is the first tim
Manish Jethani 2018/04/06 14:12:48 As far back as I can remember (which is not too fa
+ }
+
+ let frame = ext.getFrame(
kzar 2018/04/05 11:26:26 Seems like previously we didn't call getFrame if d
Sebastian Noack 2018/04/05 16:04:09 getFrame() just returns undefined if there is not
+ details.tabId,
+ // We are looking for the frame that contains the element which
+ // has triggered this request. For most requests (e.g. images) we
+ // can just use the request's frame ID, but for subdocument requests
+ // (e.g. iframes) we must instead use the request's parent frame ID.
+ details.type == "sub_frame" ? details.parentFrameId : details.frameId
+ );
+
+ let page = null;
let docDomain = null;
let sitekey = null;
+ let thirdParty = false;
let specificOnly = false;
- let thirdParty = false;
- let urlString = stringifyURL(url);
+ if (frame)
+ {
+ page = new ext.Page({id: details.tabId});
- if (frame && page)
- {
if (checkWhitelisted(page, frame))
- return true;
+ return;
docDomain = extractHostFromFrame(frame);
sitekey = getKey(page, frame);
@@ -111,20 +145,21 @@
RegExpFilter.typeMap.GENERICBLOCK);
}
- let mappedType = resourceTypes.get(type) || "OTHER";
-
+ let urlString = stringifyURL(url);
+ let type = resourceTypes.get(details.type) || "OTHER";
let filter = defaultMatcher.matchesAny(
- urlString, RegExpFilter.typeMap[mappedType],
+ urlString, RegExpFilter.typeMap[type],
docDomain, thirdParty, sitekey, specificOnly
);
setTimeout(onBeforeRequestAsync, 0, page, urlString,
- mappedType, docDomain,
+ type, docDomain,
thirdParty, sitekey,
specificOnly, filter);
- return !(filter instanceof BlockingFilter);
-});
+ if (filter instanceof BlockingFilter)
+ return {cancel: true};
+}, {urls: ["<all_urls>"]}, ["blocking"]);
port.on("filters.collapse", (message, sender) =>
{
« no previous file with comments | « ext/background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld