Skip to content

Commit

Permalink
Add Sougou Browser detection (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Sep 3, 2020
1 parent c916a52 commit a3a7a7f
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 15 deletions.
31 changes: 16 additions & 15 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

## Unreleased

- Rename `Browser::Platform#other?` to `Browser::Platform#unknown?`
- Unknown platforms now return `:unknown_platform` as the id
- Unknown devices now return `:unknown_device` as the id
- Unknown browsers now return `:unknown_browser` as the id
- All the changes above affect how `browser.meta` is composed
- Add method `Browser::Base#unknown?`
- Fix issue with `Browser::Base#safari?` matching full version
- Add Maxthon detection
- Add Google Search App detection
- Add Huawei Browser detection
- Fix Duck Duck Go browser that was being recognized as a bot
- Add Miui Browser detection
- Add `Browser::Base#qq?`
- Fix QQ detection
- Fix Alipay detection
- Rename `Browser::Platform#other?` to `Browser::Platform#unknown?`.
- Unknown platforms now return `:unknown_platform` as the id.
- Unknown devices now return `:unknown_device` as the id.
- Unknown browsers now return `:unknown_browser` as the id.
- All the changes above affect how `browser.meta` is composed.
- Add method `Browser::Base#unknown?`.
- Fix issue with `Browser::Base#safari?` matching full version.
- Add Maxthon detection.
- Add Google Search App detection.
- Add Huawei Browser detection.
- Fix Duck Duck Go browser that was being recognized as a bot.
- Add Miui Browser detection.
- Add `Browser::Base#qq?`.
- Fix QQ detection.
- Fix Alipay detection.
- Add Sougou Browser detection.

## 4.2.0

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ browser.qq?
browser.weibo?
browser.yandex?
browser.sputnik?
browser.sougou_browser?

# Get bot info
browser.bot.name
Expand Down
8 changes: 8 additions & 0 deletions lib/browser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,22 @@ def miui_browser?(expected_version = nil)
detect_version?(full_version, expected_version)
end

# Detect if browser is Maxthon.
def maxthon?(expected_version = nil)
Maxthon.new(ua).match? && detect_version?(full_version, expected_version)
end

# Detect if browser is QQ.
def qq?(expected_version = nil)
QQ.new(ua).match? && detect_version?(full_version, expected_version)
end

# Detect if browser is Sougou.
def sougou_browser?(expected_version = nil)
SougouBrowser.new(ua).match? &&
detect_version?(full_version, expected_version)
end

# Detect if browser is Google Search App
def google_search_app?(expected_version = nil)
ua =~ /GSA/ && detect_version?(full_version, expected_version)
Expand Down
2 changes: 2 additions & 0 deletions lib/browser/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
require_relative "huawei_browser"
require_relative "miui_browser"
require_relative "maxthon"
require_relative "sougou_browser"
require_relative "google_search_app"

require_relative "bot"
Expand Down Expand Up @@ -82,6 +83,7 @@ def self.matchers
HuaweiBrowser, # must be placed before Chrome and Safari
MiuiBrowser, # must be placed before Chrome and Safari
Maxthon, # must be placed before Chrome and Safari
SougouBrowser, # must be placed before Chrome and Safari
GoogleSearchApp, # must be placed before Chrome and Safari
Chrome,
Safari,
Expand Down
1 change: 1 addition & 0 deletions lib/browser/chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def match?
!miui_browser? &&
!maxthon? &&
!qq? &&
!sougou_browser? &&
!google_search_app?
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/browser/safari.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def match?
!maxthon? &&
!qq? &&
!alipay? &&
!sougou_browser? &&
!google_search_app?
end
end
Expand Down
24 changes: 24 additions & 0 deletions lib/browser/sougou_browser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Browser
class SougouBrowser < Base
def id
:sougou_browser
end

def name
"Sougou Browser"
end

# We can't get the real version on desktop device from the ua string
def full_version
ua[%r{(?:SogouMobileBrowser)/([\d.]+)}, 1] || "0.0"
end

# SogouMobileBrowser for mobile device
# SE for desktop device
def match?
ua =~ /SogouMobileBrowser/i || ua =~ / SE /
end
end
end
2 changes: 2 additions & 0 deletions test/ua.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ QQ_BROWSER_ANDROID: "Mozilla/5.0 (Linux; Android 5.1.1; SM-N9108V Build/LMY47X)
QQ_BROWSER_IOS: "Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13E238 QQ/6.3.3.432 V1_IPH_SQ_6.3.3_1_APP_A Pixel/640 Core/UIWebView NetType/WIFI Mem/47"
QQ_BROWSER_MAC: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36 QQBrowser/4.2.4753.400"
QQ_BROWSER_MAC_LITE: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14 QQBrowserLite/1.0.4"
SOUGOU_BROWSER: "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0"
SOUGOU_BROWSER_MOBILE: "Mozilla/5.0 (Linux; Android 7.1.2; Redmi 4X Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.106 Mobile Safari/537.36 AWP/2.0 SogouMSE,SogouMobileBrowser/5.28.12"
QUICKTIME: "QuickTime/7.6.8 (qtver=7.6.8;os=Windows NT 5.1Service Pack 3)"
SAFARI3: "Mozilla/5.0 (Windows; U; Windows NT 6.0; en) AppleWebKit/522.15.5 (KHTML, like Gecko) Version/3.0.3 Safari/522.15.5"
SAFARI4: "Mozilla/5.0 (Windows; U; Windows NT 6.0; fr-ch) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9"
Expand Down
41 changes: 41 additions & 0 deletions test/unit/sougou_browser_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require "test_helper"

class SougouBrowserTest < Minitest::Test
test "detects Sougou Browser on desktop device" do
browser = Browser.new(Browser["SOUGOU_BROWSER"])
assert browser.sougou_browser?
refute browser.safari?
refute browser.chrome?
assert_equal "Sougou Browser", browser.name
assert_equal :sougou_browser, browser.id
end

test "detects correct version on desktop device" do
browser = Browser.new(Browser["SOUGOU_BROWSER"])
assert_equal "0.0", browser.full_version
assert_equal "0", browser.version
end

test "detects Sougou Browser on mobile device" do
browser = Browser.new(Browser["SOUGOU_BROWSER_MOBILE"])
assert browser.sougou_browser?
assert browser.device.mobile?
refute browser.safari?
refute browser.chrome?
assert_equal "Sougou Browser", browser.name
assert_equal :sougou_browser, browser.id
end

test "detects correct version on mobile device" do
browser = Browser.new(Browser["SOUGOU_BROWSER_MOBILE"])
assert_equal "5.28.12", browser.full_version
assert_equal "5", browser.version
end

test "detects version by range on mobile device" do
browser = Browser.new(Browser["SOUGOU_BROWSER_MOBILE"])
assert browser.sougou_browser?(%w[>=5 <6])
end
end

0 comments on commit a3a7a7f

Please sign in to comment.