-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontent.js
More file actions
80 lines (52 loc) · 2.58 KB
/
content.js
File metadata and controls
80 lines (52 loc) · 2.58 KB
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
const extVersion = chrome.runtime.getManifest().version;
const inject = async (branch, version) => {
console.log('[GooseMod for Web] Injecting...');
window.gmExtension = version;
const branchURLs = {
release: 'https://api.goosemod.com/inject.js',
dev: 'https://raw.githubusercontent.com/GooseMod/GooseMod/dist-dev/index.js',
local: 'http://localhost:1234/index.js'
};
console.log('[GooseMod for Web] Branch =', branch);
console.log('[GooseMod for Web] JS Url =', branchURLs[branch]);
const js = await (await fetch(branchURLs[branch])).text(); // JSON.parse(localStorage.getItem('goosemodCoreJSCache'));
const el = document.createElement('script');
el.appendChild(document.createTextNode(js));
document.body.appendChild(el);
console.log('[GooseMod for Web] Injected fetched JS');
};
// Extension Storage (v10)
const storageCache = {};
chrome.storage.local.get(null, (data) => {
Object.assign(storageCache, data);
if (Object.keys(storageCache).length === 0 && Object.keys(localStorage).find((x) => x.toLowerCase().startsWith('goosemod'))) { // Nothing stored in Extension storage and something GM in localStorage - migrate from LS to Ext
const gmKeys = Object.keys(localStorage).filter((x) => x.toLowerCase().startsWith('goosemod'));
const setObj = {};
for (const k of gmKeys) {
setObj[k] = localStorage.getItem(k);
localStorage.removeItem(k);
}
console.log('[GooseMod For Web] Migrated from localStorage to Extension', setObj);
Object.assign(storageCache, setObj);
chrome.storage.local.set(setObj);
}
const el = document.createElement('script');
el.appendChild(document.createTextNode(`(${inject.toString()})(${JSON.stringify(storageCache['goosemodUntetheredBranch'] || 'release')}, ${JSON.stringify(extVersion)})`));
document.body.appendChild(el);
});
document.addEventListener('gmes_get', ({ }) => {
document.dispatchEvent(new CustomEvent('gmes_get_return', {
// Firefox requires cloneInto as doesn't like sending objects from content -> page
detail: typeof cloneInto === 'undefined' ? storageCache : cloneInto(storageCache, window)
}));
});
document.addEventListener('gmes_set', ({ detail: { key, value }}) => {
storageCache[key] = value; // Repopulate cache with updated value
const obj = {}; // Create object for set
obj[key] = value;
chrome.storage.local.set(obj); // Actually store change
});
document.addEventListener('gmes_remove', ({ detail: { key }}) => {
delete storageCache[key]; // Repopulate cache with updated value
chrome.storage.local.remove(key); // Actually store change
});