Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the User-Agent (a newbie question)

Status
Not open for further replies.

sagamw

Technical User
Nov 11, 2009
104
GR
Hi I am a newbie in JavaScript.
I am trying to change the useragent string of my browser by using a Greasemonkey script.

So far I have done something like that:
Code:
	GM_xmlhttpRequest({
		method: 'GET',
		url: window.location.href,
		headers: {
		'User-Agent': 'Mozilla/4.0 (compatible) Greasemonkey',
		'Accept': '*/*',
	},
});

But doesn't work. I'm "studying" this page for getting it work Maybe I need to do something with the Onload?
Can anyone help me make this working?
I am newbie so bare with me!
 
Hi

sagamw said:
So far I have done something like that:
I have similar code in my scripts and they work correctly.
sagamw said:
But doesn't work.
Define "doesn't work".
[ul]
[li]The function call throws exception ?[/li]
[li]No request is made ?[/li]
[li]The request never ends ?[/li]
[li]The request is correct, but with the original user agent ?[/li]
[li]Something else ?[/li]
[/ul]
sagamw said:
Maybe I need to do something with the Onload?
Only if you want to find out the response. Anyway, for user agent setting is far too late there.
sagamw said:
Can anyone help me make this working?
Probably not. But this would change if we would see your script. What you posted so far contains no bug.


Feherke.
 
Hi again.

Define "doesn't work".
I have the script as it is (no additional code) loaded in Greasemonkey. Enabled for sites like


and the site reports my "original" user-agent and not that of the script.

Anyway, for user agent setting is far too late there.

Do you mean the useragent is sent before the execution of the gm script?

How can I do it then?
 
Hi

sagamw said:
Do you mean the useragent is sent before the execution of the gm script?
The user agent is sent in the HTTP request. The [tt]onload[/tt] event handler is called when the HTTP response is available.

whatsmyuseragent.com certainly reports 'Mozilla/4.0 (compatible) Greasemonkey' when responding to the request sent with that user agent. ( I mean, in the response you are not handling, as you have no [tt]onload[/tt] event handler. )


Feherke.
 
Hi again.

whatsmyuseragent.com certainly reports 'Mozilla/4.0 (compatible) Greasemonkey' when responding to the request sent with that user agent. ( I mean, in the response you are not handling, as you have no onload event handler. )

:( Sorry! You lost me!
Do you mean that the GM script works for you?
Code:
// ==UserScript==
// @name           Set User-agent
// @namespace      [URL unfurl="true"]http://www.w3.org/1999/xhtml[/URL]
// @include        [URL unfurl="true"]http://whatsmyuseragent.com/[/URL]
// ==/UserScript==


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

You loaded in greasemonkey and when you go to the site reports "Mozilla/4.0 (compatible) Greasemonkey" as your user-agent?
That's weird. All I get is my "real" useragent.
 
Hi

sagamw said:
I am a newbie in JavaScript.
This is not related to JavaScript. This is plain HTTP.

The GreaseMonkey script is correct, your debugging is wrong.
You set the user agent in the GraseMonkey script, then visit whatsmyuseragent.com without involving the GreaseMonkey script.
Code:
[gray]// ==UserScript==[/gray]
[gray]// @name           Set User-agent[/gray]
[gray]// @namespace      [URL unfurl="true"]http://www.w3.org/1999/xhtml[/URL][/gray]
[gray]// @include        [URL unfurl="true"]http://whatsmyuseragent.com/[/URL][/gray]
[gray]// ==/UserScript==[/gray]

[COLOR=darkgoldenrod]GM_xmlhttpRequest[/color][teal]([/teal][teal]{[/teal]
  method[teal]:[/teal] [green][i]'GET'[/i][/green][teal],[/teal]
  url[teal]:[/teal] window[teal].[/teal]location[teal].[/teal]href[teal],[/teal]
  headers[teal]:[/teal] [teal]{[/teal]
    [green][i]'User-Agent'[/i][/green][teal]:[/teal] [green][i]'Mozilla/4.0 (compatible) Greasemonkey'[/i][/green][teal],[/teal]
    [green][i]'Accept'[/i][/green][teal]:[/teal] [green][i]'*/*'[/i][/green][teal],[/teal]
  [teal]}[/teal][teal],[/teal]
  onload[teal]:[/teal][b]function[/b][teal]([/teal]data[teal])[/teal][teal]{[/teal] [COLOR=darkgoldenrod]alert[/color][teal]([/teal]data[teal].[/teal]responseText[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/[\w\W]*<div id="MainBox">([\w\W]*?)<\/div>[\w\W]*/g[/fuchsia][teal],[/teal][green][i]'$1'[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/<.*?>/g[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal]))[/teal] [teal]}[/teal]
[teal]}[/teal][teal]);[/teal]
Code:
   Your User Agent: Mozilla/4.0 (compatible) Greasemonkey
    Your IP Address: 123.456.789.012
    (No Forwarded IP Given)

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top