From 2b9fd1f182746cd7f3d28d8ed4cfe1f59f977e16 Mon Sep 17 00:00:00 2001 From: Ray Lothian Date: Sat, 12 Sep 2020 09:33:53 +0200 Subject: [PATCH] fixes #76 --- extension/common.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/extension/common.js b/extension/common.js index be5cae1..18e509d 100644 --- a/extension/common.js +++ b/extension/common.js @@ -193,8 +193,30 @@ const ua = { userAgent: s }, prefs.parser[s]); } - // build ua string from browser defaults; - s = s.replace(/\${([^}]+)}/g, (a, b) => navigator[b]); + // build ua string from the navigator object or from a custom UAParser; + // examples: ${platform}, ${browser.version|ua-parser} + s = s.replace(/\${([^}]+)}/g, (a, b) => { + const key = (parent, keys) => { + for (const key of keys) { + parent = parent[key] || {}; + } + + return parent; + }; + + let [childs, object] = b.split('|'); + object = object || 'navigator'; + + let v; + if (object.startsWith('ua-parser')) { + const [a, b] = object.split('@'); + object = a; + + v = key((new UAParser(b || navigator.userAgent)).getResult(), childs.split('.')); + } + v = v || key(navigator, childs.split('.')); + return typeof v === 'string' ? v : 'cannot parse your ${...} replacements.'; + }); const o = {}; o.userAgent = s; o.appVersion = s