-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreload.js
More file actions
116 lines (95 loc) · 3.48 KB
/
Copy pathpreload.js
File metadata and controls
116 lines (95 loc) · 3.48 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Sphere
//
// Copyright (C) LiveG. All Rights Reserved.
// Copying is not a victimless crime. Anyone caught copying LiveG software may
// face sanctions.
//
// https://liveg.tech
// Licensed by the LiveG Open-Source Licence, which can be found at LICENCE.md.
const {remote, ipcRenderer, contextBridge} = require("electron");
const path = require("path");
const fs = require("fs");
const id = remote.getGlobal("newTabID");
var storagePath = "";
var userData = {};
function getUserData() {
userData = JSON.parse(fs.readFileSync(storagePath));
}
function saveUserData() {
fs.writeFileSync(storagePath, JSON.stringify(userData));
}
process.once("loaded", function() {
// Sending messages from Sphere to the tab
ipcRenderer.on("_sphereBookmarks", function(event, message) {
_sphereBookmarks = message;
});
});
contextBridge.exposeInMainWorld("_sphere", {
send: function(data) {
if (data._sphereKey == remote.getGlobal("messageSphereKey")) {
if (data.type == "bookmarks") {
window.postMessage({
type: "_sphereBookmarks",
data: userData.bookmarks || []
}, "*");
} else if (data.type == "favicons") {
window.postMessage({
type: "_sphereFavicons",
data: userData.favicons || {}
});
} else if (data.type == "newBookmark") {
getUserData();
userData.bookmarks = [...(userData.bookmarks || []), data.bookmark];
saveUserData();
} else if (data.type == "deleteBookmark") {
getUserData();
userData.bookmarks.splice(data.bookmarkID, 1);
saveUserData();
} else if (data.type == "setUserData") {
getUserData();
userData = data.userData;
saveUserData();
} else if (data.type == "getUserData") {
getUserData();
window.postMessage({
type: "_sphereUserData",
data: userData || {}
});
} else if (data.type == "getCacheSize") {
remote.getCurrentWindow().webContents.session.getCacheSize().then(function(size) {
window.postMessage({
type: "_sphereCacheSize",
data: size
});
});
} else if (data.type == "clearSessionData") {
var storages = [];
if (data.siteData) {
storages.push(
"cookies",
"filesystem",
"indexdb",
"localstorage",
"websql",
"serviceworkers"
);
}
if (data.cache) {
storages.push(
"appcache",
"shadercache",
"cachestorage"
);
remote.getCurrentWindow().webContents.session.clearCache();
}
remote.getCurrentWindow().webContents.session.clearStorageData({
storages: storages
});
} else {
ipcRenderer.send("_sphereTab", data);
}
}
}
});
storagePath = path.join(remote.app.getPath("userData"), "userData.json");
getUserData();