1
votes

im developing a little firefox addon with the addon-sdk provided by mozilla. The addon should work on only one specific website and it needs to block a js-file from this website. I'm searching for hours on how to block such a request.

Hopefully someone knows the answer

1
Blocking requests is something you would normally do by creating an XPCOM component implementing nsIContentPolicy. It is very non-trivial however and the SDK doesn't give you any tools for that. - Wladimir Palant

1 Answers

1
votes

Yeah, you'd have to do this mostly by hand. The SDK isn't going to help you much at all here but it is somewhat possible.

This is along the lines of what you'd need to do. Note that this isn't tested and won't work out of the box but just to give you an idea of what components are involved and where to find more resources.

const { Cc, Ci, Cm, components } = require("chrome");
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
const CategoryManager = Cc["@mozilla.org/categorymanager;1"]
                                .getService(Ci.nsICategoryManager);

function PolicyComponent() { }  

PolicyComponent.prototype = {  
  desc:             "My nsIContentPolicy XPCOM Component",  
  classID:          components.ID("{3ffd2f60-3784-11e1-b86c-0800200c9a66}"),  
  contractID:       "@abc.def.com/policycomp;1",
  QueryInterface:   XPCOMUtils.generateQI([Ci.nsIContentPolicy]),

  shouldLoad: function(contentType, contentLocation, requestOrigin, aContext, mimeTypeGuess, extra) {
    if (contentLocation.spec != BLOCKED_JS) { return return Ci.nsIContentPolicy.ACCEPT; }
    else { return Ci.nsIContentPolicy.REJECT_REQUEST; }
  },
  shouldProcess: function() {
    return CI.nsIContentPolicy.ACCEPT;
  }
}

var pc = new PolicyComponent()

// Register the Interface
Cm.QueryInterface(Ci.nsIComponentRegistrar).registerFactory(pc.uuid, pc.desc, pc.contractID, pc);

// Add the content policy 
CategoryManager.addCategoryEntry("content-policy",pc.className,pc.contractID, true, true);  // not sure you should replace (last true statement)

See this post for more: What is missing in my nsIContentPolicy Firefox/IceWeasel extension XPCOMponent implementation for the shouldLoad to be called?

Also take a look at these docs: https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads#Content_Policy