-
Notifications
You must be signed in to change notification settings - Fork 1
/
bg-initMocks.js
59 lines (51 loc) · 1.81 KB
/
bg-initMocks.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
// a workaround for oidc being dependent on global window object
const JSDOM = new jsdomModule.JSDOM;
JSDOM.reconfigure({ url: chrome.identity.getRedirectURL() });
globalThis.window = JSDOM.window;
globalThis.document = globalThis.window.document;
delete globalThis.window.localStorage;
delete globalThis.window.sessionStorage;
let storage = {};
chrome.storage.local.get(null).then((wholeStorage) => {
storage = wholeStorage;
});
chrome.storage.onChanged.addListener(updatedStorage => {
Object.keys(updatedStorage).forEach(key => {
storage[key] = updatedStorage[key].newValue;
});
});
// a workaround for oidc not supporting chrome.storage API as a cache option
globalThis.localStorage = {
setItem: (key, value) => {
console.log(`setting chrome.storage key ${key} with value ${value}`);
chrome.storage.local.set({ [`localStorage-${key}`]: value });
},
getItem: (key) => {
console.log(`getting chrome.storage key ${key}`);
return storage[`localStorage-${key}`];
},
removeItem: (key) => {
console.log(`removing chrome.storage key ${key}`);
chrome.storage.local.remove([`localStorage-${key}`]);
},
clear: () => {
console.log(`clearing chrome.storage`);
chrome.storage.local.clear();
storage = {};
},
getKeys: () => {
console.log('customStorage getKeys()');
var items = storage;
var allKeys = Object.keys(items);
console.log(allKeys);
return allKeys;
},
containsKey(key) {
console.log('customStorage containsKey()', key);
return storage[key] !== undefined;
}
}
globalThis.window.localStorage = {};
globalThis.window.localStorage = globalThis.localStorage;
globalThis.window.sessionStorage = {};
globalThis.window.sessionStorage = globalThis.localStorage;