0

I was wondering if there are any javascript objects that are unique to all versions of Google Chrome.

I know there's the window.chrome, but that was only implemented in Chrome 15, which was rather recent. I am wondering if there's any objects (or multiple objects in conjunction) that are unique only to Google Chrome.

The reason why is I am building a web app that requires Chrome because it is "Chrome/Google"-themed/based. I don't want users using Firefox or IE to look at a Google Chrome-looking app, and (because I do this myself sometimes), I don't want to sniff the user agent because it can be spoofed.

Any help would be much appreciated!

ModernDesigner
  • 6,737
  • 8
  • 30
  • 40
  • 3
    Are you truly that worried about showing a Chrome-themed app to a non-Chrome user who is savvy enough to change the browser's UA string? Is the objective of your project to increase usage of the Chrome browser? (That is, if I get to your site and *desperately* want to see it, but I don't have Chrome, it is your prerogative to force me to download Chrome?) What if I'm using a fork of Chromium with identical rendering/JS functionality but vastly different UI ascetics? – apsillers Feb 04 '13 at 21:31
  • Good luck. I totally agree with apsillers - I use 4 different browsers on a daily basis .. don't *break* generality for the specific. –  Feb 04 '13 at 21:35
  • @apsillers Good point on if a person is savvy enough to spoof their user agent, then I shouldn't be concerned, however, I am not trying in any way to sway users into downloading Chrome, (however if everyone actually liked Chrome that would be lovely), and personally, 50% of the time I do prefer firefox. Usually, I hate browser discrimination and use shims and Modernizr if I have a problem with something in that concern. But for this time, it's pure experimental (for now), and I'm just now learning the basics of javascript. – ModernDesigner Feb 04 '13 at 21:40
  • @ModernDesigner: Then use feature detection for that experiment. – Bergi Feb 04 '13 at 21:42
  • @Bergi Normally, yes, I would, BUT because my app doesn't rely on the latest and greatest HTML5 and CSS3 features, I would just like (personally) for my Chrome-themed app to be viewed in Google Chrome. (Not to say that it doesn't use HTML5 and CSS3, but that's not the core dependency). And look, if object detection is *really* frowned upon by you guys lately, then I just won't do it. But I was still curious. – ModernDesigner Feb 04 '13 at 21:46
  • 1
    The problem is simply that you're trying to do browser detection, and browser detection is fundamentally flawed. At the most basic level, you have philosophical questions of identity: if I make a fork of Chromium that behaves just like Chrome (but has, say, a custom UI or some other trivial change), do you want to allow that browser? It would be kind of crazy to say "no" -- the site would work perfectly in this almost-Chrome browser. Now say I made a Chromium fork that's a little more divergent, or I hacked away at Firefox source to make it work just like Chrome -- do you allow those browsers? – apsillers Feb 04 '13 at 21:55
  • 1
    [con't] Your solution is "we'll call a browser 'Chome-ish enough' if it supports some particular Chrome-specific object". This practice (i.e., browser detection) is just a kludgy kind of feature detection, wherein you test for features that are totally unrelated to the operation of your app (like UA string, or some browser-specific object). You *think* that these unrelated features imply the existence of other important features, but you have no way of know that unless you actually test for those important features. If you directly tested features, then, voila: you're doing feature detection. – apsillers Feb 04 '13 at 21:58

2 Answers2

2

Why don't you check if the browser is Chrome like this?

var isChrome = /chrome/.test(navigator.userAgent.toLowerCase()); 

(original answer here)

Community
  • 1
  • 1
Elliot Bonneville
  • 46,945
  • 19
  • 86
  • 120
  • I edited my question to say "*..., and (because I do this myself sometimes), I don't want to sniff the user agent because it can easily be spoofed.*" – ModernDesigner Feb 04 '13 at 21:28
  • 3
    @ModernDesigner: And a property couldn't be spoofed? – the system Feb 04 '13 at 21:31
  • @thesystem That's a fair point, but possibly the behavior of some host objects couldn't be spoofed (e.g., persisting after a `delete`) without recompiling the browser. – apsillers Feb 04 '13 at 21:36
  • @thesystem Well in that case, *then* I would completely agree with apsillers and what he said earlier: I shouldn't concern myself with that if the user is tech-savvy enough to actually do something like that. But I am still in the opinion that UA-spoofing is actually a lot easier for the average person to do. – ModernDesigner Feb 04 '13 at 21:42
  • @apsillers: Making properties non-configurable (non-`delete`able) is easy today. – Bergi Feb 04 '13 at 21:43
  • @ModernDesigner: Could be. – the system Feb 04 '13 at 21:44
  • 1
    @Bergi Fair point, it's a bad example, but I think my larger point about host objects still stands (though I'm not 100% on that, either.) – apsillers Feb 04 '13 at 21:46
  • 1
    @apsillers: Yeah, I think it would take a V8 quirk to really be reliable *(or closer to it anyway)*. There's a V8 bug where non-enumerable properties become enumerable when they shadow an enumerable property in the prototype chain. [Here's a browser test](http://jsfiddle.net/2npUv/) based on that bug. Not sure how long it's been around, but it's been quite a while. Maybe since the beginning. – the system Feb 04 '13 at 21:47
2

The correct way to test for chome using feature detection is as follows:

var isChrome = !(Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0) && 'WebkitTransform' in document.documentElement.style;

The second part of the query is checking to see if you are webkit, as all webkit browsers have their CSS vendor properties start with Webkit (capital W).

The second part checks to see if you are safari and negates the second part. Safari is the only browser with a 'Constructor'.

dreamcrash
  • 36,542
  • 23
  • 64
  • 87
Victoria French
  • 686
  • 4
  • 8
  • Awesome! Thanks! I knew about the Safari detection but I never thought to test for Webkit then make sure it's not Safari. Thanks again. – ModernDesigner Feb 06 '13 at 18:23
  • I published code that I've been using to do this for ie stuff. It's now open source and called Defunctr. It rides on Modernizr. http://nuget.org/packages/Defunctr/ http://github.com/victoriafrench/defunctr – Victoria French Feb 06 '13 at 19:42