Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust handling of userAgentData, fix WebPushDeviceAdmin #643

Merged
merged 6 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,28 +194,49 @@ For WNS, you need both the ``WNS_PACKAGE_SECURITY_KEY`` and the ``WNS_SECRET_KEY
}
return outputArray;
}
function loadVersionBrowser (userAgent) {
// If userAgentData is available, use this for browser detection

function loadVersionBrowser () {
if ("userAgentData" in navigator) {
// navigator.userAgentData is not available in
// Firefox and Safari
const uaData = navigator.userAgentData;
// Outputs of navigator.userAgentData.brands[0].brand are
// Outputs of navigator.userAgentData.brands[n].brand are e.g.
// Chrome: 'Google Chrome'
// Edge: 'Microsoft Edge'
// Opera: 'Opera'
const browser = uaData.brands[0];
const brandname = browser.brand;
// If there is a space within brandname, we only care about the right part
const browsername = brandname.substr(brandname.indexOf(' ')+1);
const browserversion = browser.version;

return {
name: browsername,
version: browserversion
let browsername;
let browserversion;
let chromeVersion = null;
for (var i = 0; i < uaData.brands.length; i++) {
let brand = uaData.brands[i].brand;
browserversion = uaData.brands[i].version;
if (brand.match(/opera|chrome|edge|safari|firefox|msie|trident/i) !== null) {
// If we have a chrome match, save the match, but try to find another match
// E.g. Edge can also produce a false Chrome match.
if (brand.match(/chrome/i) !== null) {
chromeVersion = browserversion;
}
// If this is not a chrome match return immediately
else {
browsername = brand.substr(brand.indexOf(' ')+1);
return {
name: browsername,
version: browserversion
}
}
}
}
// No non-Chrome match was found. If we have a chrome match, return it.
if (chromeVersion !== null) {
return {
name: "chrome",
version: chromeVersion
}
}
}
// Otherwise fallback to the old method via userAgent
// If no userAgentData is not present, or if no match via userAgentData was found,
// try to extract the browser name and version from userAgent
const userAgent = navigator.userAgent;
var ua = userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
Expand Down
12 changes: 11 additions & 1 deletion push_notifications/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,17 @@ class GCMDeviceAdmin(DeviceAdmin):
list_filter = ("active", "cloud_message_type")


class WebPushDeviceAdmin(DeviceAdmin):
list_display = ("__str__", "browser", "user", "active", "date_created")
list_filter = ("active", "browser")

if hasattr(User, "USERNAME_FIELD"):
search_fields = ("name", "registration_id", "user__%s" % (User.USERNAME_FIELD))
else:
search_fields = ("name", "registration_id")


admin.site.register(APNSDevice, DeviceAdmin)
admin.site.register(GCMDevice, GCMDeviceAdmin)
admin.site.register(WNSDevice, DeviceAdmin)
admin.site.register(WebPushDevice, DeviceAdmin)
admin.site.register(WebPushDevice, WebPushDeviceAdmin)