简体   繁体   中英

Change GET REQUEST header of an iframe via browser extension

after some serious googleing AFAIK, there is no way to change http request header of an iframe. Since that is VERY important for our application, is there a way to do it via browser extensions (Firefox would be enough)?

Thanks.

So after a long while...

1) Install Mozilla SDK: Addons SDK

Follow instructions to create extension directory structure

2) In your lib/main.js script:

 17 var {Cc, Ci} = require("chrome");
 18 var data = require("sdk/self").data;
 19 var _header = "";
 20 
 21 
 22 // Define observer
 23 var httpRequestObserver =
 24 {
 25     observe: function(subject, topic, data) {
 26         if (topic == "http-on-modify-request") {
 27             var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
 28             httpChannel.setRequestHeader("User-Agent", _header, false);
 29         }
 30     }
 31 };
 32 
 33 // Register observer
 34 var observerService = Cc["@mozilla.org/observer-service;1"]
 35                                 .getService(Ci.nsIObserverService);
 36 
 37 observerService.addObserver(httpRequestObserver, "http-on-modify-request", false);
 38 
 39 var pageMod = require("sdk/page-mod");
 40 
 41 pageMod.PageMod({
 42     include: ["*"],
 43     contentScriptFile: data.url("content.js"),
 44     onAttach: function(worker) {
 45         worker.on("message", function(message) {
 46             _header = message;
 47         }); 
 48     }   
 49 });

3) Then in your data/content.js short snippet:

  1 document.defaultView.addEventListener("message", function(event) {
  2 
  3     //alert(event.data);
  4 
  5     self.postMessage(event.data);
  6 
  7 }, false);

4) And finally in your page's script you just make simple call:

window.postMessage("CONST_STR;" + data.foo + ";" + data.bar, "*");

Since it's pretty complex beast, one should really go and RTFM for better understanding, otherwise if you can just pass with this, even better, you'll won't have an urge for drug or drink.

Hope it helps someone, somewhere...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM