Why does chrome.webRequest.OnBeforeRequest fire before chrome.webNavigation.onBeforeNavigate? -
i'm trying understand logic. in mind, onbeforenavigate
event should complete before hear request. find onbeforerequest
event fires first. here's sample code demonstrate mean.
test.js
function test(url) { chrome.tabs.create({ url: "" }, function (tab) { chrome.webnavigation.onbeforenavigate.addlistener(function (details) { console.log("chrome.webnavigation.onbeforenavigate hit on " + details.timestamp); }); chrome.webrequest.onbeforerequest.addlistener(function (details) { console.log("chrome.webrequest.onbeforerequest hit on " + details.timestamp); }, { tabid: tab.id, urls: ["<all_urls>"] }); chrome.tabs.update(tab.id, { url: url }); }); } test("http://www.steam.com"); // simple url 2 requests
resulting console messages:
chrome.webrequest.onbeforerequest hit on 1437083141916.896 chrome.webnavigation.onbeforenavigate hit on 1437083141916.906 chrome.webrequest.onbeforerequest hit on 1437083141940.385
manifest.json
{ "background": { "persistent": true, "scripts": [ "test.js" ] }, "manifest_version": 2, "name": "test", "permissions": [ "<all_urls>", "webnavigation", "webrequest" ], "version": "1.0" }
here view of 3 details
, in order of event fire:
// first chrome.webrequest.onbeforerequest { "frameid" : 0, "method" : "get", "parentframeid" : -1, "requestid" : "72285", "tabid" : 1312, "timestamp" : 1437083141916.896, "type" : "main_frame", "url" : "http://www.steam.com/" }, // chrome.webnavigation.onbeforenavigate { "frameid" : 0, "parentframeid" : -1, "processid" : 3567, "tabid" : 1312, "timestamp" : 1437083141916.906, "url" : "http://www.steam.com/" }, // second chrome.webrequest.onbeforerequest { "frameid" : 0, "method" : "get", "parentframeid" : -1, "requestid" : "72286", "tabid" : 1312, "timestamp" : 1437083141940.385, "type" : "image", "url" : "http://www.steam.com/images/pipebackwhite.gif" }
if think when each event firing should make sense.
onbeforerequest firing before make request server:
fires when request occur. event sent before tcp connection made , can used cancel or redirect requests.
on other side, onbeforenavigate fires before page navigates next page:
fired when navigation occur.
if think how browser works, fires request server , if it's typical get, browser navigates new page on request based off of headers. however, happens on actual request, , onbeforerequest being run before fires off server @ all.
so, make request, api says:
- a request made, before anything, should fire onbeforerequest
- ok, processed request, it's making me navigate/redirect, before that, should onbeforenavigate
Comments
Post a Comment