0

I want to block resources (like JavaScript- or CSS-files), before they are fully loaded. The condition, if resources are blocked, depends on their content. Actually, there is no solution for doing this, or?

What i know: Webextensions are similar to Chrome extensions, so they have also the webRequest.onBeforeRequest listener. The listener allows to block files:

function cancel(requestDetails) {
  console.log("Canceling: " + requestDetails.url);
  return {cancel: true};
}

chrome.webRequest.onBeforeRequest.addListener(
  cancel,
  {urls: ["<all_urls>"], types: ["script"]},
  ["blocking"]
);

Until this point, everything is fine. There should also be the requestBody option, so i can make my decision based on the content of the file. But Mozilla Doc says:

  1. Firefox does not support the "requestBody" option.

Oh, bad story, not cool. So i need an asynchronous XHR request, to get the URLs data:

function cancel(requestDetails) {
   return {cancel: getUrlContent(requestDetails.url, function(result) {
      if(condition) { console.log(true); } else { console.log(false); }
   })};
}

// asynchron XHR request
function getUrlContent(url, callback) { [...] }

=> This doesn't work. the return {cancel: X}; accepts normal functions, like return {cancel: doSth()};, but no asynchron callbacks. I have no option to "return" the result to the cancel JSON.

In other stackoverflow questions, i read about nsIContentPolicy, which is part of
XPCOM. I didn't found a hint, example or description, how i can use this in the Firefox Webextension environment. Is this possible? And if yes, how?

If both things are not possible, how i can do such an extension? Do i have to use the deprecated XPCOM without Webextension?

Zeussi
  • 571
  • 1
  • 9
  • 22
  • See http://www.html5rocks.com/en/tutorials/service-worker/introduction/ – guest271314 May 29 '16 at 00:31
  • @guest271314: Thanks for your answer. It looks like a nice tool. I tried to register a service worker, but it failed. If i put it into the background script, it says, the connection is insecure. I added the service worker JavaScript file, which will be called after registration to "web_accessible_resources". If i put it into the content script, it says the same error. Did i miss something or where i have to register it? Does it need extra permissions? Or doesn't it work in the extension context, because its no HTTPS, just local? The URL is about moz-extension://long-hash/... – Zeussi May 29 '16 at 13:49
  • _"because its no HTTPS, just local?"_ Yes – guest271314 May 29 '16 at 13:59
  • @guest271314: So the result is, that it doesn't work (actually) for Webextensions? – Zeussi May 29 '16 at 14:11
  • Can you use `https:` protocol? Not sure what you mean by "block resources"? – guest271314 May 29 '16 at 14:22
  • @guest271314: I want to develope a Firefox Addon, which listens to each website a user opens. So i don't know, which website and which resources are loaded. A resource is for me a html page, a javascript- or css-file, and so on. If i call developer.mozilla.org, it loads jquery.js and Google Analytics (GA). I want to read the JS content of GA, and if it has for my condition "bad content", i want to block it, that means, i have to control the network connections before they are in DOM like service worker seems to do. But the service worker is in the Firefox-Addon locally, there is no https :( – Zeussi May 29 '16 at 14:41
  • See https://developer.mozilla.org/en-US/docs/Using_workers_in_extensions – guest271314 May 29 '16 at 15:05
  • It looks like `requestBody` will land in an upcoming version of Firefox (currently appears to be 50): https://bugzilla.mozilla.org/show_bug.cgi?id=1201979. Which is to say, you can probably test in Beta now, since 49 was released this week. – hoosteeno Sep 21 '16 at 20:36
  • @hoosteeno, Yes, you are right, i will test it. This "problem" seems to be solved by itself soon and the question getting out of date. You can answer this question with this solution, then i will mark it as "best answer". – Zeussi Sep 22 '16 at 13:54

1 Answers1

1

It looks like requestBody will land in an upcoming version of Firefox (currently appears to be 50): bugzilla.mozilla.org/show_bug.cgi?id=1201979. Which is to say, you can probably test in Beta now, since 49 was released this week.

hoosteeno
  • 644
  • 5
  • 17
  • 1
    Note: this is **request** body, not response body. You can't cancel based on the response (it's too late). – Xan Sep 26 '16 at 14:47