Skip to content

Commit 75f78c0

Browse files
authored
More robust handling of userAgentData, fix WebPushDeviceAdmin (#643)
* webpush add Edge * webpush - add edge endpoint * change edge endpoint * update README Show another, easier approach to extract the browsername and browser version * more robust handling ofh userAgentData Relevant userAgentData ist not always stored in navigator.userAgentData.brands[0] It is necessary to loop through all available navigator.userAgentData.brands and try to find the relevant entry. In case of Edge it is also necessary to ignore any matches regarding chrome... addresses #640 * Fix WebpushDeviceAdmin Field "device_id" is not available for WebPushDevices. Instead add "browser" to ListView and "registration_id" to search_fields
1 parent 56995b1 commit 75f78c0

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

README.rst

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,49 @@ For WNS, you need both the ``WNS_PACKAGE_SECURITY_KEY`` and the ``WNS_SECRET_KEY
194194
}
195195
return outputArray;
196196
}
197-
function loadVersionBrowser (userAgent) {
198-
// If userAgentData is available, use this for browser detection
197+
198+
function loadVersionBrowser () {
199199
if ("userAgentData" in navigator) {
200200
// navigator.userAgentData is not available in
201201
// Firefox and Safari
202202
const uaData = navigator.userAgentData;
203-
// Outputs of navigator.userAgentData.brands[0].brand are
203+
// Outputs of navigator.userAgentData.brands[n].brand are e.g.
204204
// Chrome: 'Google Chrome'
205205
// Edge: 'Microsoft Edge'
206206
// Opera: 'Opera'
207-
const browser = uaData.brands[0];
208-
const brandname = browser.brand;
209-
// If there is a space within brandname, we only care about the right part
210-
const browsername = brandname.substr(brandname.indexOf(' ')+1);
211-
const browserversion = browser.version;
212-
213-
return {
214-
name: browsername,
215-
version: browserversion
207+
let browsername;
208+
let browserversion;
209+
let chromeVersion = null;
210+
for (var i = 0; i < uaData.brands.length; i++) {
211+
let brand = uaData.brands[i].brand;
212+
browserversion = uaData.brands[i].version;
213+
if (brand.match(/opera|chrome|edge|safari|firefox|msie|trident/i) !== null) {
214+
// If we have a chrome match, save the match, but try to find another match
215+
// E.g. Edge can also produce a false Chrome match.
216+
if (brand.match(/chrome/i) !== null) {
217+
chromeVersion = browserversion;
218+
}
219+
// If this is not a chrome match return immediately
220+
else {
221+
browsername = brand.substr(brand.indexOf(' ')+1);
222+
return {
223+
name: browsername,
224+
version: browserversion
225+
}
226+
}
227+
}
228+
}
229+
// No non-Chrome match was found. If we have a chrome match, return it.
230+
if (chromeVersion !== null) {
231+
return {
232+
name: "chrome",
233+
version: chromeVersion
234+
}
216235
}
217236
}
218-
// Otherwise fallback to the old method via userAgent
237+
// If no userAgentData is not present, or if no match via userAgentData was found,
238+
// try to extract the browser name and version from userAgent
239+
const userAgent = navigator.userAgent;
219240
var ua = userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
220241
if (/trident/i.test(M[1])) {
221242
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];

push_notifications/admin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,17 @@ class GCMDeviceAdmin(DeviceAdmin):
133133
list_filter = ("active", "cloud_message_type")
134134

135135

136+
class WebPushDeviceAdmin(DeviceAdmin):
137+
list_display = ("__str__", "browser", "user", "active", "date_created")
138+
list_filter = ("active", "browser")
139+
140+
if hasattr(User, "USERNAME_FIELD"):
141+
search_fields = ("name", "registration_id", "user__%s" % (User.USERNAME_FIELD))
142+
else:
143+
search_fields = ("name", "registration_id")
144+
145+
136146
admin.site.register(APNSDevice, DeviceAdmin)
137147
admin.site.register(GCMDevice, GCMDeviceAdmin)
138148
admin.site.register(WNSDevice, DeviceAdmin)
139-
admin.site.register(WebPushDevice, DeviceAdmin)
149+
admin.site.register(WebPushDevice, WebPushDeviceAdmin)

0 commit comments

Comments
 (0)