From b99fd158bb93e913ae476b492d0884bb22da578b Mon Sep 17 00:00:00 2001 From: Ray Lothian Date: Tue, 5 Nov 2019 14:28:10 +0000 Subject: [PATCH] import and export based on http://add0n.com/useragent-switcher.html#IDComment1079854533 --- extension/data/options/index.html | 5 ++++ extension/data/options/index.js | 50 +++++++++++++++++++++++++++++++ extension/data/popup/index.html | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/extension/data/options/index.html b/extension/data/options/index.html index b24c35d..ad31549 100644 --- a/extension/data/options/index.html +++ b/extension/data/options/index.html @@ -70,6 +70,11 @@ + +

+ + +

diff --git a/extension/data/options/index.js b/extension/data/options/index.js index d4c25fb..3edb623 100644 --- a/extension/data/options/index.js +++ b/extension/data/options/index.js @@ -135,3 +135,53 @@ document.getElementById('help').addEventListener('click', () => { url: chrome.runtime.getManifest().homepage_url }); }); + +// export +document.getElementById('export').addEventListener('click', () => { + chrome.storage.local.get(null, prefs => { + const text = JSON.stringify(prefs, null, ' '); + const blob = new Blob([text], {type: 'application/json'}); + const objectURL = URL.createObjectURL(blob); + Object.assign(document.createElement('a'), { + href: objectURL, + type: 'application/json', + download: 'useragent-switcher-preferences.json' + }).dispatchEvent(new MouseEvent('click')); + setTimeout(() => URL.revokeObjectURL(objectURL)); + }); +}); +// import +document.getElementById('import').addEventListener('click', () => { + const input = document.createElement('input'); + input.style.display = 'none'; + input.type = 'file'; + input.accept = '.json'; + input.acceptCharset = 'utf-8'; + + document.body.appendChild(input); + input.initialValue = input.value; + input.onchange = readFile; + input.click(); + + function readFile() { + if (input.value !== input.initialValue) { + const file = input.files[0]; + if (file.size > 100e6) { + console.warn('100MB backup? I don\'t believe you.'); + return; + } + const reader = new FileReader(); + reader.onloadend = event => { + input.remove(); + const json = JSON.parse(event.target.result); + chrome.storage.local.clear(() => { + chrome.storage.local.set(json, () => { + window.close(); + chrome.runtime.reload(); + }); + }); + }; + reader.readAsText(file, 'utf-8'); + } + } +}); diff --git a/extension/data/popup/index.html b/extension/data/popup/index.html index 656fe33..41ce446 100644 --- a/extension/data/popup/index.html +++ b/extension/data/popup/index.html @@ -78,7 +78,7 @@ userAgent

- +