问题
We have a client that wants their content on Google TV (via the Chrome Browser), but is legally restricted from allowing it on the "regular web". So we want to detect Google TV Browser via Javascript, and if it is not, disallow playing the content.
The first thought was to check the user agent string, but apparently they don't like that because it would be too easy for a user to change their user agent string on their web browser so it pretends to be the Google TV browser.
Is there something else we could check for that is a little harder (or at least less obvious) to fake? I know any solution can be hacked around if someone tries hard enough, and that is ok.
回答1:
There may be some things you can do since you only want to ensure that you are detecting who is NOT using a GoogleTV browser. Certainly these can be spoofed but they are quite a bit harder than just using a different user-agent string.
Basically, the idea would be to test for certain JavaScript object support (see this page: http://www.javascriptkit.com/javatutors/objdetect3.shtml for some ideas). An example of this would be testing for the presence of window.opera
which would indicate that the browser was indeed Opera and definitely NOT a GoogleTV.
To that end, I've written a small fiddle example that cannot be faked by user-agent switching in all the modern browsers and requires a more sophisticated mechanism to bypass. I tested and it does pass on a Google TV.
http://jsfiddle.net/XBT4w/2/
回答2:
I don't think there are any better ways then what Google suggests:
var userAgent = navigator.userAgent;
function isGoogleTvBrowser(useragent) {
return Boolean(useragent.match(/(Large Screen)|GoogleTV/i));
}
Although obviously, the user agent string can be faked extremely easily.
回答3:
Anything you can come up with, especially in JavaScript, is going to be very easy to fake, because one can trivially (with console) paste code that will inject any value into any location.
来源:https://stackoverflow.com/questions/12538143/detect-google-tv-without-using-user-agent-string