forked from jrie/flagCookies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.js
103 lines (90 loc) · 4.38 KB
/
importer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict'
// Chrome
const useChrome = typeof (browser) === 'undefined'
// --------------------------------------------------------------------------------------------------------------------------------
// Called in frontend/UI
function importSettings (evt) {
doImportOverwrite = document.querySelector('#confirmImportOverwrite').classList.contains('active')
if (evt.target.files[0] === undefined) return
const file = evt.target.files[0]
JSZip.loadAsync(file).then(function (zip) {
if (zip.files['flagCookieSettings.json'] === undefined) return
zip.files['flagCookieSettings.json'].async('string').then(async function (stringData) {
const data = JSON.parse(stringData)
if (doImportOverwrite) {
if (!useChrome) browser.storage.local.set(data)
else chrome.storage.local.set(data)
window.alert('Settings succesfully imported, you can close this window.')
return
}
if (!useChrome) {
const existingData = await browser.storage.local.get()
browser.storage.local.set(mergeData(existingData, data))
window.alert('Settings succesfully imported, you can close this window.')
return
}
chrome.storage.local.get(null, function (existingData) {
chrome.storage.local.set(mergeData(existingData, data))
window.alert('Settings succesfully imported, you can close this window.')
})
})
})
}
// --------------------------------------------------------------------------------------------------------------------------------
function mergeData (existingData, data) {
const flagCookieSettings = ['flagCookies_logged', 'flagCookies_flagGlobal', 'flagCookies_autoflag', 'flagCookies_notifications', 'flagCookies_darkTheme']
for (const key of Object.keys(data)) {
if (flagCookieSettings.indexOf(key) === -1) {
if (existingData[key] === undefined) existingData[key] = data[key]
else {
for (const domain of Object.keys(data[key])) {
if (existingData[key][domain] === undefined) existingData[key][domain] = data[key][domain]
else {
for (const setDomain of Object.keys(data[key][domain])) {
if (existingData[key][domain][setDomain] === undefined) existingData[key][domain][setDomain] = data[key][domain][setDomain]
else {
for (const cookieKey of Object.keys(data[key][domain][setDomain])) {
if (existingData[key][domain][setDomain][cookieKey] === undefined) existingData[key][domain][setDomain][cookieKey] = data[key][domain][setDomain][cookieKey]
}
}
}
}
}
}
} else {
if (key === 'flagCookies_logged') {
if (existingData[key] === undefined) existingData[key] = data[key]
else {
for (const domain of Object.keys(data[key])) {
if (existingData[key][domain] === undefined) existingData[key][domain] = data[key][domain]
else {
for (const setDomain of Object.keys(data[key][domain])) {
if (existingData[key][domain][setDomain] === undefined) existingData[key][domain][setDomain] = data[key][domain][setDomain]
else {
for (const cookieKey of Object.keys(data[key][domain][setDomain])) {
if (existingData[key][domain][setDomain][cookieKey] === undefined) existingData[key][domain][setDomain][cookieKey] = data[key][domain][setDomain][cookieKey]
}
}
}
}
}
}
} else if (key === 'flagCookies_autoFlag') {
if (existingData[key] === undefined) existingData[key] = data[key]
else {
for (const domain of Object.keys(data[key])) {
if (existingData[key][domain] === undefined) existingData[key][domain] = data[key][domain]
}
}
} else if ((key === 'flagCookies_flagGlobal' || key === 'flagCookies_notifications' || key === 'flagCookies_darkTheme')) existingData[key] = data[key]
}
}
return existingData
}
function toggleImportOverwrite (evt) {
if (!evt.target.classList.contains('active')) evt.target.classList.add('active')
else evt.target.classList.remove('active')
}
let doImportOverwrite = false
document.querySelector('#confirmImportOverwrite').addEventListener('click', toggleImportOverwrite)
document.querySelector('#importFile').addEventListener('change', importSettings)