Firefox extension: How to intercept the requested url conditionally and block it?

前端 未结 2 1126
醉酒成梦
醉酒成梦 2021-01-07 01:20

In my Firefox extension I want to intercept the url that the browser is requesting and block the request entirely if some condition matches

How can I intercept URL b

2条回答
  •  旧巷少年郎
    2021-01-07 01:43

    An other possible solution :

    Here is an other implementation as modules example from HTTPS-Everywhere

    Init function :

      init: function() {
        // start observing all http requests
        Services.obs.addObserver(httpNowhere, "http-on-modify-request", false);
      },
    

    Observer function :

    observe: function(subject, topic, data) {
    var request = subject.QueryInterface(Ci.nsIHttpChannel);
      if (topic == "http-on-modify-request") {
        if (request.URI.spec == "xxx.example.com") {
          request.redirectTo("yyy.example.com");
        }
        else {
          request.cancel(Components.results.NS_ERROR_ABORT);
        }
      }
    },
    

    Example addons :

    HTTPS-Nowhere - https://github.com/cwilper/http-nowhere

    HTTPS-Everywhere - https://github.com/EFForg/https-everywhere

    Migrating your extension to chrome :

    i answered your question for chrome in this page : Chrome Extension : How to intercept requested urls?

提交回复
热议问题