Is there any way to detect what the browser is?

Monkey Targets Forums/HTML5/Is there any way to detect what the browser is?

Gerry Quinn(Posted 2011) [#1]
Title says it all. Of course the issue is the horrible one of no common sound format that works on all the common browsers.

A HTML or Javascript method would do at a pinch, I suppose, in that you could have different Monkey programs for different browsers. But it would be nicer to have a way of making a single file.

Super-evil method that occurs to me: HTML5 doesn't seem to crash when you try to use and play the wrong format, so maybe load and try to play two sound formats, playing both sounds in the same channel...

PlaySound( oggLaser, 1 ) 'works on Firefox and Chrome
PlaySound( mp3Laser, 1 ) 'works on IE and Chrome

But will the second command stop the sound on Firefox, or just be ignored? Have to try it out I guess.


therevills(Posted 2011) [#2]
You should be able to do it with an extern:

http://www.w3schools.com/js/js_browser.asp

I would do it like this:

Global sndExt

If browser="IE"
  sndExt = ".mp3"
Else
  sndExt = ".ogg"
End

' Load Sounds
lazerSnd:Sound

lazerSnd = LoadSound("lazer"+sndExt)



Gerry Quinn(Posted 2011) [#3]
That looks like the way to go. I know some people are using a Flash sound program but that just gives you another dependency. I can always put an option to choose the sound format in preferences if people are disguising their browser as I believe some do.

My little trick above doesn't work, by the way, in case anyone wanted to try it. Firefox doesn't crash but it hardly makes a sound, as though it tries to play them but gets cut off by the dud sound trying to play on the same channel.

It *does* actually work if you load both formats and play the loaded sounds simultaneously on separate channels. That means Chrome should be playing the same sound on two channels at once. I couldn't hear any difference when I tried it, but it just doesn't seem the right way to go, and maybe it would cause unexpected problems on some systems or browsers, or when there are more demands on resources than in my simple test program.


Gerry Quinn(Posted 2011) [#4]
Hmmm...

I added:
var browserCode = navigator.appCodeName
to main.js

..and then put an extern global browserCode:String in my Monkey source.

It reads it okay, but whatever browser I use, the browserCode is set to Mozilla.

Am I loading a default uninitialised navigator or something?


therevills(Posted 2011) [#5]
Use navigator.appName instead of navigator.appCodeName...

Monkey Code:
Strict

Import "jsExtern.js"

Extern
	Function GetBrowserName:String()="extern.getBrowserName"

Public

Function Main:Int()
	Local name:String = GetBrowserName()
	Print name
	Return 0
End


jsExtern.js
var extern = new Object();

extern.getBrowserName=function(){
	return navigator.appName;
};


[edit]
Hmmmm Chrome returns Netscape... maybe use the userAgent and you will have to look thru the string for the browser name...
[/edit]

[edit2]
Okay just found this script:
http://www.quirksmode.org/js/detect.html

So alter jsExtern.js to look like this:



And in the Monkey code you can do this:

Strict

Import "jsExtern.js"

Extern
	Function GetBrowserName:String()="extern.getBrowserName"
	Function GetBrowserVersion:String()="extern.getBrowserVersion"
	Function GetBrowserOS:String()="extern.getBrowserOS"

Public

Function Main:Int()
	Local name:String = GetBrowserName()
	Local version:String = GetBrowserVersion()
	Local os:String = GetBrowserOS()		
	Print name
	Print version
	Print os
	Return 0
End


[/edit2]


xlsior(Posted 2011) [#6]
Obtaining that info is probably that should be part of the standard framework?


therevills(Posted 2011) [#7]
standard framework


Do you mean Monkey/Mojo? If so... its debatable as it would only be for one platform.

I am going to add this to Diddy...


Bladko(Posted 2011) [#8]
i think it would be great to gather all informations regarding differences in html5 on each browse because this information is shattered on the forum or in the docs (so we can gather this as a part of docs in diddy)


Gerry Quinn(Posted 2011) [#9]
Okay, userAgent works, which should resolve my SFX dilemma! Thanks everyone for the help: Monkey forums rock!

If you just need the browser name, you can do it more simply than in therevills solution. All you need to do is edit the main.js generated by Monkey to include the line:

var browserAgent = navigator.userAgent

Then in your Monkey program include:

Extern
Global browserAgent:String

Now when you check browserAgent in your Monkey program it will be a long string which you can search for the substrings MSIE, Chrome or Firefox, or whatever other browser you want to test for. Of course when it's in Diddy we can just be lazy and use that!

As regards adding it to Mojo, maybe Mark could consider a GetOS() function that would return a string or object containing useful information about what the program is running on?


therevills(Posted 2011) [#10]
All you need to do is edit the main.js generated by Monkey

You would need to do this every time you alter your code! You are hacking the generated code... You could add it to the target template but again this is a hack. The extern way is the correct way to do it.


Gerry Quinn(Posted 2011) [#11]
I suppose... I can live with just adding it to the template though because it's going to be in every program I write anyway. Though I guess I still have to change it when Monkey is updated ;-)

Found some new bugs when testing Chrome and Firefox. DrawImage() crashes both of them if the colour is not set to white. And Firefox doesn't honour the loop sound flag. I suppose the latter is just something that we have to live with until Firefox is fixed...


impixi(Posted 2011) [#12]
This works for me:

Strict

Extern

Function getBrowserAgent:String() = "function (){return navigator.userAgent;}"

Public


Function Main:Int()

	Print getBrowserAgent()

	Return 0

End



therevills(Posted 2011) [#13]
Yep thats a cleaner solution than hacking the monkey files :)

Although I recommend reading the website I linked above and check the order of the searching of the strings within userAgent:

http://www.quirksmode.org/js/detect.html

Anyway I've added to Diddy for easy use.


Gerry Quinn(Posted 2011) [#14]
Slightly irrelevant issue:
Extern

Function getBrowserAgent:String() = "function (){return navigator.userAgent;}"

Public


Function Main:Int()


Does 'Public' cancel 'Extern'?


JIM(Posted 2011) [#15]
Yep. Each of "Extern", "Public" and "Private" cancel the others.