简体   繁体   English

将移动浏览器检测,规则选择添加到规则集中

[英]adding mobile browser detection, rule selection, into a ruleset

I would like to add functionality to a ruleset that fires a distinct rule based on whether or not the browser is mobile or not. 我想向一个规则集添加功能,该规则集根据浏览器是否可移动来触发不同的规则。 (one rule fires for a standard browser, a different rule fires for a mobile browser) I know that the browser detection can be done any number of ways, but my first inclination would be with javascript. (一个规则适用于标准浏览器,不同的规则适用于移动浏览器)我知道浏览器检测可以通过多种方式完成,但我的第一个倾向是使用javascript。

Any thoughts on how to start with this? 关于如何从此开始有什么想法?

You can use the useragent object , like this: 您可以使用useragent对象 ,如下所示:

rule detect_agent {
    select when pageview ".*"
    pre {
        browser_name = useragent:browser_name();
        browser_version = useragent:browser_version();
        os = useragent:os();
        os_type = useragent:os_type();
        os_version = useragent:os_version();
        full_useragent = useragent:string();
        message = <<
            <p><strong>Information about your browser:</strong></br />
            <em>Browser name:</em> #{browser_name}</br />
            <em>Browser version:</em> #{browser_version}</br />
            <em>Operating system:</em> #{os}</br />
            <em>OS type:</em> #{os_type}</br />
            <em>OS version:</em> #{os_version}</br /></p>
            <p>#{full_useragent}</p>
        >>;
    }
    append("body", message);
}

You might have to do some parsing of your own, though, since the browser_name and os may or may not be correct. 但是,由于browser_nameos可能正确或可能不正确,因此您可能必须自己进行一些解析。 Here's what it looks like in Chrome on a Mac (you can test it using this URL in any browser): 这是在Mac上的Chrome中的外观(您可以在任何浏览器中使用此URL对其进行测试):

Chrome,Mac

Here's what it looks like in Safari on an iPad: 这是在iPad上的Safari中的外观:

Safari,iPad

Do some research into what the UserAgent strings look like for the browsers you care about. 对您关心的浏览器的UserAgent字符串的外观进行一些研究。 Then you can use the useragent:string() function together with match() to determine what to do with it. 然后,您可以将useragent:string()函数与match()一起使用,以确定如何处理它。 (If you want an example of how to do that, let me know.) (如果您想举一个例子,请告诉我。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM