Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension webRequest.onBeforeRequest

I am trying to create an extension to analyse requests made on the chrome browser but I can't put it work. The alert never fires.

manifest.json

{
  "name": "Test",
  "description": "Test",
  "version": "1.0",
  "manifest_version": 2,
  "permissions": ["background", "tabs", "webRequest", "webRequestBlocking", "*://*/*"],    
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  }  
}

background.js

var callback = function(details) {
    alert("hello");
};
var filter = { "*://*/*" };
var opt_extraInfoSpec = [];

chrome.webRequest.onBeforeRequest.addListener(
        callback, filter, opt_extraInfoSpec);

Why is it my alert not firing?

like image 651
Bonomi Avatar asked Mar 17 '15 13:03

Bonomi


People also ask

How do I use onbeforerequest?

This event is triggered when a request is about to be made, and before headers are available. This is a good place to listen if you want to cancel or redirect the request. To cancel or redirect the request, first include "blocking" in the extraInfoSpec array argument to addListener() .

Can I use webRequest?

To use the webRequest API for a given host, an extension must have the "webRequest" API permission and the host permission for that host. To use the "blocking" feature, the extension must also have the "webRequestBlocking" API permission.

How do I send a post request extension in Chrome?

Type the url in the main input field and choose the method to use: GET/POST/PUT/DELETE/PATCH. Click on the arrow "Send" or press Ctrl+Enter. You'll see info about the response (time, size, type) and you'll be able to see the content response in the response section.


1 Answers

Your filter is the wrong format - it's not a valid object at all. Addtionally it needs to contain at least the 'url' property. If you wan't all URL's, use this:

var filter = {urls: ["<all_urls>"]};

Check out this for exact details on the format for the filter: https://developer.chrome.com/extensions/webRequest#type-RequestFilter

like image 168
Dan Smith Avatar answered Sep 17 '22 15:09

Dan Smith