如何在 Rails 中检测浏览器类型及其版本。我想检查特定浏览器的版本,如果不需要浏览器版本,则要求用户升级它。我使用下面指定的命令,但由于它不遵循标准模式,我无法使用它。
request.env['HTTP_USER_AGENT']
Chrome out put is below
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16
Safari out put is below
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1
FireFox out put is below
Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0
Opera out put is below
Opera/9.80 (Windows NT 5.1; U; en) Presto/2.8.131 Version/11.10
Internet Explorer out put is below
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
尝试浏览器 gem。非常简单,可以解决您的目的。
您可以在这里找到宝石,非常容易使用。
注意:请小心,某些搜索引擎会将其视为一种入侵,请参阅(Google HTTP 用户代理)
if request.env['HTTP_USER_AGENT'] =~ /[^\(]*[^\)]Chrome\//
或者对于 Firefox
if request.env['HTTP_USER_AGENT'] =~ /[^\(]*[^\)]*[^\t]Firefox\//
并在此处检查,您将获得所需的一切 浏览器宝石
这是一种可以检测所有浏览器的方法,所以冷静点
def browser_detection
result = request.env['HTTP_USER_AGENT']
browser_compatible = ''
if result =~ /Safari/
unless result =~ /Chrome/
version = result.split('Version/')[1].split(' ').first.split('.').first
browser_compatible = 'Application is not functional for Safari version\'s '+version if version.to_i < 5
else
version = result.split('Chrome/')[1].split(' ').first.split('.').first
browser_compatible = 'Application is not functional for Chrome version\'s '+version if version.to_i < 10
end
elsif result =~ /Firefox/
version = result.split('Firefox/')[1].split('.').first
browser_compatible = 'Application is not functional for Firefox version\'s '+version if version.to_i < 5
elsif result =~ /Opera/
version = result.split('Version/')[1].split('.').first
browser_compatible = 'Application is not functional for Opera version\'s '+version if version.to_i < 11
elsif result =~ /MSIE/
version = result.split('MSIE')[1].split(' ').first
browser_compatible = 'Application is not functional for Microsoft Internet Explorer version\'s '+version if version.to_i < 9
end
browser_compatible
end
还有 useragent gem:
<% user_agent = UserAgent.parse(request.env["HTTP_USER_AGENT"]) %>
App: <%= user_agent.application %> # Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:22.0)
Browser: <%= user_agent.browser %> # Firefox
Version: <%= user_agent.version %> # 22.0
Platform: <%= user_agent.platform %> # Macintosh
Mobile: <%= user_agent.mobile? %> # False
OS: <%= user_agent.os %> # OS X 10.8
你所做的实际上就是做事的方式。现在,您可以使用正则表达式处理用户代理信息,在 Firefox、Chrome 或您喜欢的任何其他浏览器或版本上查找匹配项。
虽然这是一个前端解决方案,但我更喜欢检查所需的特定功能,例如 Flexbox。您可以使用类似 Modernizr 的东西,或者只需进行简单的 JavaScript 检查即可(https://stackoverflow.com/a/27047981/179311)。
if(!("flexWrap" in document.documentElement.style)) {
// IE 10, Chrome 20 and Firefox 27 and anything older will hit this.
alert('your browser is out of date');
// maybe this is preferred
// window.location = '/browser-unsupported'
}
从
Rails 7.2
开始,有一个内置的方法来检查浏览器类型及其版本。
例如:
class ApplicationController < ActionController::Base
# Allow only browsers natively supporting webp images, web push, badges, import maps, CSS nesting + :has
allow_browser versions: :modern
end
class ApplicationController < ActionController::Base
# All versions of Chrome and Opera will be allowed, but no versions of "internet explorer" (ie). Safari needs to be 16.4+ and Firefox 121+.
allow_browser versions: { safari: 16.4, firefox: 121, ie: false }
end
class MessagesController < ApplicationController
# In addition to the browsers blocked by ApplicationController, also block Opera below 104 and Chrome below 119 for the show action.
allow_browser versions: { opera: 104, chrome: 119 }, only: :show
end