WebExtension: Block request after response header received in Firefox and Chrome Extension

Well. Firefox finally admitted that Chrome’s extension system is better than it’s XUL or Add-on SDK.

So from Firefox 45+, it started to support WebExtension. And now, it even does not allow any extension that is not built with WebExtension API.

This is a good news, because if you want your extension run on both Chrome and Firefox. you won’t need to build twice.

However, even Firefox is trying to support all of the WebExtension API. There are still some differences between Chrome and Firefox.

Recently I got this one.

I was trying to block some requests with the WebRequest API after response header just received. In Chrome, you can easily do it with an onHeadersReceived listener. like this:

 chrome.webRequest.onHeadersReceived.addListener(
    function (details) {
        let block = THE CONDITIONS THAT YOU WANT TO USE;
        return {cancel: block};

    },
    {urls: ["URL PATTERN"], types: ["TYPES"]},
    ["blocking"]); 

But in Firefox, this is not gonna work. Because in Firefox, you can not block a request after the response headers received, you can only redirect it. So you need to to this:

 chrome.webRequest.onHeadersReceived.addListener(
    function (details) {
        let block = THE CONDITIONS THAT YOU WANT TO USE;
        return {cancel: block, redirectUrl: "://127.0.0.1"};

    },
    {urls: ["URL PATTERN"], types: ["TYPES"]},
    ["blocking"]); 

with the redirectUrl: “://127.0.0.1”, you will redirect it to a local address, and means block it.

Firefox Addon: How to use gmail.js in Firefox Addon SDK

Well, gmail.js is a very nice script that if you want to work with Gmail (developing browser extensions for Gmail).

The author says that gmail.js is designed for Chrome extension development. There is also many examples about how to use it in Chrome extension. But, how to use it in Firefox addon development?

If you inject it directly in the page, you will got an error: GLOBAL is not defined.

it is because the gmail.js uses 2 variables in the Gmail page, GLOBAL  and also VIEW_DATA. If you inject it directly by pageMode, it can not access any page script.

So you need to modify the gmail.js, in line 6 and 7 (if line number is different, just search it ). By using unsafeWindow , you can access the Gmail page variable:
change

api.tracker.globals =GLOBALS;

to
api.tracker.globals = unsafeWindow.GLOBALS;

change

api.tracker.view_data = typeof VIEW_DATA !== "undefined" ? VIEW_DATA : [];

to

api.tracker.view_data = typeof unsafeWindow.VIEW_DATA !== "undefined" ? unsafeWindow.VIEW_DATA : [];

Then you can use the gmail.js without error.

Actually in Chrome extension, you can not inject gmail.js with content script decoration in manifest.json directly, because in that way  you can not access GLOBAL too.

You need to use another content script to inject it  with add <script> block in the page, here  is the code snap

window.onload = function () {

    //inject jQuery
    var jq = document.createElement('script');
    jq.src = chrome.extension.getURL("lib/jquery-2.1.1.min.js");
    document.body.appendChild(jq)

    //inject gmail.js
    var sm = document.createElement('script');
    sm.src = chrome.extension.getURL("lib/gmail.js");
    document.body.appendChild(sm);
    //inject the script that actual do your things 
    var ct = document.createElement('script');
    ct.src = chrome.extension.getURL("content.js");
    document.body.appendChild(ct);
}

Then declare this file as content script in the manifest.json. and also declare all 3 scripts in web_accessible_resources.

Chrome: How to use extension that not from web store

From  version 35,  Chrome will disable extensions that not come from Chrome web store. It do makes your Chrome safer,  But it also bring some problems for some users.  That is, If you are using a extension that you don’t want to upload it to the Chrome web store, you can not use it anymore. This extension will be disabled and can not be enabled:

Well, here is how to re-enable extensions like this (not from Chrome Web Store):

  1. navigate to chrome://extensions page. delete the disabled extension by clicking the trash icon
  2. find your extension file that ends with .crx or .zip extension.
  3. right click on the file, use 7-zip software(or any other extract tool) with “Extract to…” command to extract the extension
  4. drag and drop the extracted FOLDER to the chrome://extensions page.
  5. Confirm the permissions  confirmation information
  6. You are done.

You can also manage your extensions with my Extension
One Click Extension Manager -No Ads