fnando / browser Goto Github PK
View Code? Open in Web Editor NEWDo some browser detection with Ruby. Includes ActionController integration.
License: MIT License
Do some browser detection with Ruby. Includes ActionController integration.
License: MIT License
I am using this gem quite a bit and would love to help out if you dont have the time to work on it
Hello,
I need an isolated and reliable behavior in my controller when I see that the browser is Safari/Mac, because of bugs that I need to workaround.
I see that this might be intended in
http://github.com/fnando/browser/blob/master/test/browser_test.rb#L184
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7
browser.mac? true
browser.safari? true
browser.chrome? true
I think that browser.safari? should return false.
Great plugin, thank you.
With the current order of the NAMES keys, when you check browser.name you get back platform instead of browser name for Android Chrome (returns 'Android', not 'Chrome') and iPhone Safari (returns 'iPhone', not 'Safari').
Could I suggest the order to be as follows (I'm not doing a pull requests as I'm not sure how this will affect your overall plugin - I'm just testing it out and am locally monkey-patching it as follows:
NAMES = {
:blackberry => "BlackBerry",
:chrome => "Chrome",
:core_media => "Apple CoreMedia",
:firefox => "Firefox",
:ie => "Internet Explorer",
:android => "Android", # android above safari otherwise default android browser shows up as 'safari'
:safari => "Safari",
:ipad => "iPad",
:iphone => "iPhone",
:ipod => "iPod Touch",
:nintendo => "Nintendo",
:opera => "Opera",
:phantom_js => "PhantomJS",
:psp => "PlayStation Portable",
:playstation => "PlayStation",
:quicktime => "QuickTime",
:xbox => "Xbox",
# This must be last item, since Ruby 1.9+ has ordered keys.
:other => "Other",
}
Currently every IE browser reports that it is in compatibility view. You can demonstrate this simply by adding negative assertions to the tests for ie8 and 9 (non-compatibility view). For instance
def test_detect_ie9
@browser.ua = IE9
assert_equal "Internet Explorer", @browser.name
assert @browser.ie?
assert @browser.ie9?
assert @browser.capable?
assert !@browser.compatibility_view?
assert_equal "9.0", @browser.full_version
assert_equal "9", @browser.version
end
Simply adding the negative assertion to this existing test as seen on line 8 causes the test to fail.
Also, as an aside. I think that ie browsers in compatibility mode should report the compatible version (e.g. ie 7) instead of reporting the real version as they currently do. Having it report ie8 or 9 currently gives the impression that the browser is much more capable than it is. In compatibility mode it essentially operates like the old/broken ie7 and the browser gem should report it as such.
I'm running it on Rails 3.1.3, does it support it?
Hi,
I first want to say that this gem is awesome! Thank you!
And, Here is my question.
I noticed android tablet browsers weren't marked as mobile browsers. And according to #17 , tablet browsers are not marked as mobile browsers.
But, iPad is flagged as both mobile ( because it's user agent string contains "mobile" ) and tablet.
Is this a bug or intended behavior?
Shouldn't they(ipad and android tablet) return same values for mobile? method?
Sample Android UA string:
Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; NOOK BNTV250A Build/GINGERBREAD 1.4.3) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Safari/533.1
Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; SAMSUNG-SGH-I497 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30
It works fine on iPhone and some android phones, but it doesn't work with the Motorala Xoom and with Opera mobile.
Would be grate to detect mobile browser platform.
For instance to show [Get from AppStore] or [Get on Google play] buttons for exact platform.
Thanks
hi, i just set up a facebook vote page on my website and i wanted to use browser.platform in order to detect facebook computer and facebook user.
So i set up something like this "redirect_to my_path if if browser.platform != :other" but it doesnt seem to work
Is the project still alive? I'm noticing that in the last 4 months a lot has happened in the network but isn't merged into the gem (IE10, Win version, etc...)
In order to keep maintaining this great gem I would suggest getting Travis test working (then you know is a merge request is breaking) and code climate (for coverage).
If desired we (my company) are willing to take on some of this work, as we are using this gem quite a lot.
The "browser" helper method is not available when using Rails 3.2.13.
Any plan to release of new version in the near future? The rubygems version hasn't been updated since February and quite a few changes have been made since then!
Thanks,
Nicolas
the gem is not detecting a broswer to be IE prior to ie9
For this user agent from the android platform:
browser = Browser.new
browser.user_agent = "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SCH-I535 Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30\n"
[6] pry(main)> browser.android?
=> true
which is correct, but also
[16] pry(main)> browser.safari?
=> true
which is less than useful if you are trying to keep old Safari browsers off the platform but allow android browsers.
Is this intentional?
Seems like some changes were made in a9302f1 which were meant so solve thread safety issues, but these changes were not enough.
The problem is that @env
is stored, and this creates global mutable state, since the middleware is a singleton. call
must not have any dependency on state in the middleware that can change between requests, otherwise it is not thread safe.
The problem is this line:
browser/lib/browser/middleware.rb
Line 15 in c527b32
I'll work on sending a pull request for this.
PROBLEM:
Microsoft Word does a 'pre-request' on any URL that is clicked inside a document. It then returns a new new URL (after resolving any redirects, etc) to the system's browser of choice. Thus a simple middleware such as
use Browser::Middleware do
redirect_to "/upgrade" unless browser.modern?
end
would cause Word to receive a 302 redirect to /upgrade, and then pass "/upgrade" as the URL to your otherwise modern browser, giving you a false "Browser Not Supported".
The UA from Word on my machine appears as
Mozilla/5.0 (Macintosh; Intel Mac OS X) Word/14.44.0]
Could you add an #msword?
meta, so we can next if browser.msword?
or add it to the modern list, handle it, etc.
HOW TO REPRODUCE:
http://localhost:3000
IE 11 is fairly new, and here's what it sends as its UA string (strange):
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
So the plugin recognizes it as "other other0 windows", which is obviously wrong. Hence, browser.modern? test fails as well.
The current Kindle detection is using:
# Detect if browser is Kindle.
def kindle?
!!(ua =~ /Kindle/)
end
If you look at the user agent strings defined at https://developer.amazon.com/sdk/fire/specifications.html this will only work for a quite only generation 1 Kindle. It is best to match on "Silk" which is the Kindle browser.
Please add when you get a chance - Thank you!
It would be pretty sweet to be able to do something like:
#IE 8 an below...
browser.ie? && browser.version <= 8
Internet Explorer version 11 is not properly detected currently. We have users reporting it so even though IE11 is in beta we'd like it to be properly detected.
The problem is the user agent has changed slightly and of course that browser only goes up to 10 :)
Would you accept a pull request to fix this as we are already working on the proper UA recognition anyway?
It'd be nice to see feature parity and detection parity with WhichBrowser. WhichBrowser has a wealth of detection information that could be imported into this gem. Note its use of HTTP heathers to aid detections and its capability for OS detection and device detection.
add a check for windows mobile devices
So the middleware does not fire.
I observed this on a real IE8 browser just now.
There's logic right now to consider empty user agent strings to be bots. I not sure if this really make sense for the following reasons:
Browser.new.bot? #=> true
This isn't really expected, nor true.def firefox_lt_v4?
browser.firefox? && browser.version < "4"
end
Hello,
it would be cool to have a browser object directly in a controller action, fetching automatically the required info from request.
Internet Explorer 9 doesn't support xhr2(http://caniuse.com/xhr2). Should it be considered a modern browser?
Further reading: http://people.mozilla.org/~prouget/ie9/
I have a project that also has a class named 'Browser' so unfortunately I can't use this library without adding some jank. I've forked it and will put it under the 'Brwsr' namespace for now but I wanted to run it by you to see your thoughts, perhaps a better name?
Tried the following code:
browser = Browser.new(:ua => request.env['HTTP_USER_AGENT'])
user.last_user_agent = request.env['HTTP_USER_AGENT']
user.last_os_name = browser.platform.to_s unless browser.nil?
user.last_browser_name = browser.id.to_s unless browser.nil?
user.last_browser_version = browser.full_version.to_s unless browser.nil?
with the following user agent string (i.e. HTTP_USER_AGENT)
Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; SGH-i917)
The values for browser.platform, browser.id and browser.full_version are nil
Is there any reason why DuckDuckBot is listed in search_engines.yml, but not listed in bots.yml? I think that bots.yml has to include all records from search_engines.yml.
We recently started logging unsupported browser versions so we can decide what we need to test and explicitly support. We've had a number of (I believe) false positives from Samsung devices. Here are the user-agent strings we've seen so far:
Mozilla/5.0 (Linux; Android 4.4.2; en-gb; SAMSUNG GT-I9195/I9195XXUCNEA Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36
Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG SM-G900T Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.6 Chrome/28.0.1500.94 Mobile Safari/537.36
Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG SPH-L720 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36
Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG-SGH-I337 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36
Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG-SM-N900A Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36
Because of the way version
and full_version
work in the browser gem, it's returning 1.5 or 1.6: extracted from the "Version/..." part of the UA string:
irb(main):003:0> browser = Browser.new(ua: 'Mozilla/5.0 (Linux; Android 4.4.2; en-gb; SAMSUNG GT-I9195/I9195XXUCNEA Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36')
=> #<Browser:0x007f81a063a3c0 @user_agent="Mozilla/5.0 (Linux; Android 4.4.2; en-gb; SAMSUNG GT-I9195/I9195XXUCNEA Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36", @accept_language="">
irb(main):004:0> browser.full_version
=> "1.5"
I'm not really sure what that version number refers to, but it's confusing our logic a bit, because we're looking for a combination of the browser type and version, and that version is neither the Android version, nor the Chrome version. The id of this UA reports as android
and both android?
and chrome?
return true:
irb(main):005:0> browser.id
=> :android
irb(main):006:0> browser.meta
=> ["android", "mobile", "modern", "linux", "webkit"]
irb(main):007:0> browser.android?
=> true
irb(main):008:0> browser.chrome?
=> true
I don't own a Samsung device, so I'm not sure what the right thing to do here is, but I think that the browser in use on Android 4.4 devices is actually Chrome, so this gem should be reporting the Chrome version.
Alternatively, since the id comes back as Android, it could report the correct Android version (4.4.2).
I'm happy to write a pull request for this, but I'm not sure what the best way to proceed with this is. I'm hoping to get some feedback here from Samsung users.
PROBLEM:
Nearly all command-line clients appear as 'other' and have no [easy] means of being distinguished.
Much like you have a YAML list of bots and search engines, create a YAML file for non-interactive agents, such as curl, wget, MS Word. Rather than creating metas for each, they could be lumped into something like a #non_interactive?
or #not_a_browser?
meta. This would be a broader solution to Issue #112
HOW TO REPRODUCE:
curl http://localhost:3000
wget http://localhost:3000
sample: QuickTime/7.6.8 (qtver=7.6.8;os=Windows NT 5.1Service Pack 3)
This is on Safari Windows when QuickTime is installed and fetching HTML 5 audio/video tags.
Browser: Internet Explorer 10
default:
browser.version: 10
request.env['HTTP_USER_AGENT']: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
compatibility view:
browser.version: 7
request.env['HTTP_USER_AGENT']: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; Tablet PC 2.0; .NET4.0C; .NET4.0E)
I need a method to determine the real version browser.
Maybe add some method for this?
I think for that you can check the layout engine (Trident version).
503 - Internal Server Error
Error Message:
undefined method `chrome?' for nil:NilClass
I have never come across this error before but several developers I work with have. I'm not sure what causes the error but I'm guessing that it is machine based since only certain clients receive this error. I know its not much to go off of but any ideas would be great. Usually a restart of the computer or server solves the issue.
This article warns us of the dangers of in app browsers.
Certain platforms (IOS) contain information about the app the is being used. It would be great if we could ask if the browser is "embedded" and maybe even which app it is.
What do you think?
I have used this gem in development mode just by adding it to the Gemfile and by calling the browser method in a controller. A simple controller method like
def index
@popup = ['Android', 'iPad', 'iPhone', 'iPod Touch'].include?(browser.name)
end
in the main_controller.rb works perfectly in development. Rails version is 3.2.14.
However, when deploying to the production server, I get the following:
NameError (undefined local variable or method `browser' for #<MainController:0x00000004d30b90>):
app/controllers/main_controller.rb:4:in `index'
When calling the previously shown index method.
I've checked everything I've deployed, and the gem appears in the Gemfile, Gemfile.lock and is installed as well. I've also tried to restart Unicorn, but to no avail.
Any help?
2.0.0p247 :002 > b = Browser.new(ua: 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko')
=> #<Browser:0x007f90d8da1c98 @user_agent="User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko", @accept_language="">
2.0.0p247 :003 > b.version
=> "0"
2.0.0p247 :004 > b.ie?
=> false
2.0.0p247 :005 > b.name
=> "Other"
2.0.0p247 :006 >
Sample: "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
Is this gem tested on 1.8.7 ? I see the parser choking here:
https://github.com/fnando/browser/blob/master/lib/browser.rb#L84
but there might be other incompatibilities also.
UA: Apple Mac OS X v10.6.4 CoreMedia v1.0.0.10F569
browser.mac? true
browser.safari? false
browser.chrome? false
Can we add it to the user agent list? This is for HTML 5 audio/video.
Or maybe add a browser.coremedia?
Is it possible to provide an example in the README
for how to test a redirection based on UA?
My current tests return 200 and not the expected redirect. Assume there is an index
route and upgrade_browser
actions.
Implementation:
describe FooController do
context 'agent testing' do
controller(FooController) do
def index
render nothing: true
end
end
context 'Reject non-modern browsers and IE 9' do
it 'should redirect to the upgrade page' do
request.env['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'
get :index
expect(response).to redirect_to :upgrade_browser
end
end
context 'Do not reject non-modern browsers IE10+' do
it 'should redirect to the upgrade page' do
request.env['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0; EIE10;ENUSMSN)'
get :index
expect(response).not_to redirect_to :upgrade_browser
end
end
end
end
The documentation for the behavior of .bot?() in master (and thus on the github page for the project) is not correct for the current release (0.5), which caused a bit of pain when trying to figure out why all of our tests are acting as if they're replying to bots when the HTTP_USER_AGENT is not set.
Hello,
Does this gem work with Rails 4 and Ruby 2.x?
I've added the gem, run a bundle install (I've also tried a bundle update). Browser 0.6.0 is showing as installed. I've tried restarting the web server too, but I cannot get:
require "browser"
or references to browser or Browser.new to work:
Browser.new
NameError: uninitialized constant Browserbrowser
NameError: undefined local variable or method `browser' for main:Object
My gemfile includes:
gem 'browser'
I'm sure I'm missing something stupid, but before I go crazy here I figured I would check to see if anybody has gotten this gem working in the latest versions of Rails/Ruby?
Hello,
Thanks for the gem, it's been very useful to me.
I'm having a couple of issues on the ipad and need to NOT treat it as a mobile platform. How would you suggest I do that?
Thanks again!
Alex
I am using this gem along with Rails. I have the following in my config/initializers/browser.rb
use Browser::Middleware do
redirect_to "/upgrade" unless browser.modern?
end
And when I am not using a modern browser, this results in a redirect loop. The gem doesn't seem to omit the upgrade path view from the browser check.
It looks like some of the browser check methods are doing some degree of filtering... e.g.,
def safari?
ua =~ /Safari/ && ua !~ /Chrome|CriOS|PhantomJS/
end
But is this functionality intended to just double-check the user-agent, or is it designed to filter against bots that may return multiple browser name responses?
Is there a way to utilize the combinations in such a way to estimate whether the browser is human or bot, e.g.,
def notbot?
(browser.webkit? || browser.firefox? || browser.ie? || browser.opera? || browser.safari? || browser.chrome?) && !browser.compatibility_view?
end
I'm using an initializer to redirect users not using a modern browser to /upgrade. The redirect is working as expected, however, none of the stylesheets will load on the page.
So I am using this in an initializer file:
Rails.configuration.middleware.use Browser::Middleware do
redirect_to '/upgrade' unless browser.modern?
end
The redirect works just fine. However, when I attempt to view the page in IE8 or IE7, none of the stylesheets will load. I can add inline css and it works fine, but the assets will not load.
If I disabled the gem, and refresh the page, the stylesheets load as normal.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.