-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathpreload.js
More file actions
43 lines (40 loc) · 1.35 KB
/
preload.js
File metadata and controls
43 lines (40 loc) · 1.35 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
const { ipcRenderer, contextBridge } = require('electron');
contextBridge.exposeInMainWorld('electron', {
setLocalStorage: (key, value) => {
localStorage.setItem(key, value);
},
getLocalStorage: (key) => {
return localStorage.getItem(key);
},
setCookie: (name, value, days) => {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = "expires=" + date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
},
getCookie: (name) => {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
});
window.addEventListener('beforeunload', () => {
const state = {
url: window.location.href,
scrollPosition: window.scrollY,
};
localStorage.setItem('appState', JSON.stringify(state));
});
window.addEventListener('load', () => {
localStorage.clear();
const savedState = JSON.parse(localStorage.getItem('appState'));
if (savedState) {
window.location.href = savedState.url;
window.scrollTo(0, savedState.scrollPosition);
}
});