简体   繁体   中英

Greasemonkey Spoof User Agent

In a website I visit, there is a bit of code that checks the User Agent and if it is not IE, it will redirect to another website.

if( ! window.navigator.appVersion.match(/MSIE ([0-9]+.)[A-Za-z0-9.]+;/) || new Number(parseFloat (RegExp.$1)) < 5.5 ){
    window.location.replace("/iedownload.htm");
}

Is there a way to make a quick greasemonkey script to spoof the User Agent? I tried

GM_xmlhttpRequest({
  method: 'GET',
  url: window.location.href,
  headers: {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 9.0;) Greasemonkey',
    'Accept': '*/*',
  },
});

But it did not work.

The easy way is to not use GreaseMonkey for this and to use an add-on that modifies the UserAgent before the request is sent. If the check is server-side, this won't work.

But anyway, you could try what Max suggests here .

So, something like this:

var fakeAppVersion = function () {
   return "MSIE 9.0;";
};
if (Object.defineProperty) {    
  Object.defineProperty(navigator, "appVersion", {
     get: fakeAppVersion
  });
} else if (Object.prototype.__defineGetter__) {
     navigator.__defineGetter__("appVersion", fakeAppVersion);
}

Remember to use // @run-at document-start

Edit: That might not work I suppose if the check is done before that snippet has a chance to run. In that case, you could use the GM_xmlhttpRequest like you tried, but have it open in a new tab. [You'll probably need to disable the popup blocker for this site I guess].

Greasemonkey has gotten fairly annoying with security (at least on firefox, where I'm testing) Brock's solved the issue here . Modifying his code to suit your needs, I got this (which works AFAIK):

if (window.location.href != 'about:blank') {

  GM_xmlhttpRequest({
    method: 'GET',
    url: window.location.href,
    headers: {
      'User-Agent': 'Mozilla/4.0 (compatible; MSIE 9.0;) Greasemonkey',
      'Accept': '*/*',
    },
    onload: function(response){
      var data = response.responseText;
      addJS_Node (null, null, fireNewTab, data, fireNewTab );     
    } 
  })        

  function fireNewTab (data) {
      var newTab = window.open ('about:blank', '_blank');
      newTab.addEventListener (
          "load",
          function () {           
              //--- Now process the popup/tab, as desired.
              var destDoc = newTab.document;
              destDoc.open ();
              destDoc.write (decodeURI(data));
              destDoc.close ();          
          },
          false
      );
  }    

  function addJS_Node (text, s_URL, funcToRun, data, runOnLoad) {
      var D                                   = document;
      var scriptNode                          = D.createElement ('script');      
      if (runOnLoad) {
          scriptNode.addEventListener ("load", runOnLoad, false);
      }
      scriptNode.type                         = "text/javascript";
      if (text)       scriptNode.textContent  = text;
      if (s_URL)      scriptNode.src          = s_URL;
      if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')("' + encodeURI(data) +'")';

      var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
      targ.appendChild (scriptNode);
  }
}

You will need to // @grant GM_xmlhttpRequest ofcourse..

You tried an ajax request.

You missed the onload function.

It's possible to retrieve the content of the page, just like you did. But, you can't navigate through the page that way.

You could try to create a userscript to override window.location.replace , but it may have side effects too.

unsafeWindow.location.replace = function () {};

It will also depend on the version of Greasemonkey that you are using.

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