diff --git a/extension/common.js b/extension/common.js index 72dfce2..164c0a3 100644 --- a/extension/common.js +++ b/extension/common.js @@ -21,6 +21,7 @@ const prefs = { parser: {}, // maps ua string to a ua object, log: false }; +window.prefs = prefs; // access from popup const log = (...args) => prefs.log && console.log(...args); @@ -40,11 +41,20 @@ expand.rules = {}; chrome.storage.local.get(prefs, ps => { Object.assign(prefs, ps); - expand(); - chrome.tabs.query({}, ts => { - ts.forEach(t => tabs[t.id] = t.windowId); - ua.update(); + // update prefs.ua from the managed storage + chrome.storage.managed.get({ + ua: '' + }, rps => { + if (!chrome.runtime.lastError && rps.ua) { + prefs.ua = rps.ua; + } + expand(); + chrome.tabs.query({}, ts => { + ts.forEach(t => tabs[t.id] = t.windowId); + ua.update(); + }); }); + if (chrome.browserAction.setBadgeBackgroundColor) { // FF for Android chrome.browserAction.setBadgeBackgroundColor({ color: prefs.color diff --git a/extension/data/popup/index.js b/extension/data/popup/index.js index 33f5b42..253192a 100644 --- a/extension/data/popup/index.js +++ b/extension/data/popup/index.js @@ -156,7 +156,6 @@ document.addEventListener('DOMContentLoaded', () => fetch('./map.json').then(r = document.querySelector('#os optgroup:last-of-type').appendChild(f2); chrome.storage.local.get({ - 'ua': '', 'popup-browser': 'Chrome', 'popup-os': 'Windows', 'popup-sort': 'descending' @@ -165,10 +164,12 @@ document.addEventListener('DOMContentLoaded', () => fetch('./map.json').then(r = document.getElementById('os').value = prefs['popup-os']; document.getElementById('sort').value = prefs['popup-sort']; - const ua = prefs.ua || navigator.userAgent; - update(ua); - document.getElementById('ua').value = ua; - document.getElementById('ua').dispatchEvent(new Event('input')); + chrome.runtime.getBackgroundPage(bg => { + const ua = bg.prefs.ua || navigator.userAgent; + update(ua); + document.getElementById('ua').value = ua; + document.getElementById('ua').dispatchEvent(new Event('input')); + }); }); })); diff --git a/extension/manifest.json b/extension/manifest.json index 1d40bdf..4cac5c6 100755 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -28,6 +28,9 @@ "128": "data/icons/active/128.png", "256": "data/icons/active/256.png" }, + "storage": { + "managed_schema": "schema.json" + }, "background":{ "scripts":[ "ua-parser.min.js", diff --git a/extension/schema.json b/extension/schema.json new file mode 100644 index 0000000..7a6a1da --- /dev/null +++ b/extension/schema.json @@ -0,0 +1,9 @@ +{ + "type": "object", + "properties": { + "ua": { + "title": "Custom User-Agent String", + "type": "string" + } + } +}