Skip to content

Commit ec25961

Browse files
committed
feat: add plugin and toast support
1 parent 0d6a009 commit ec25961

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/electron.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,20 @@ function createWindow() {
5757
}
5858
});
5959

60+
function toast(data) {
61+
win.webContents.send('toast', data);
62+
}
63+
6064
function update() {
6165
checkVersion().then((updated) => {
6266
if (!updated) return;
63-
win.webContents.send('toast', {
67+
toast({
6468
title: 'Updated UnderScript',
6569
text: 'Refresh page to finish update',
6670
refresh: true,
6771
});
6872
}).catch((error) => {
69-
win.webContents.send('toast', {
73+
toast({
7074
title: 'Error updating UnderScript',
7175
error,
7276
});
@@ -91,6 +95,7 @@ function createWindow() {
9195
e.preventDefault();
9296
const { host, protocol } = new URL(url);
9397
if (protocol !== 'http:' && protocol !== 'https:') return;
98+
9499
if (host === 'undercards.net' || host === 'www.undercards.net') {
95100
win.loadURL(url.replace('www.', ''));
96101
} else if (url.endsWith('undercards.user.js')) {
@@ -101,8 +106,8 @@ function createWindow() {
101106
});
102107

103108
autoUpdater.on('update-downloaded', (info) => {
104-
win.webContents.send('toast', 'Restart to update.');
105-
})
109+
toast('Restart to update.');
110+
});
106111

107112
autoUpdater.checkForUpdatesAndNotify();
108113
}

src/preload/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require('./rememberMe');
22
require('./zoom');
3+
require('./plugin');

src/preload/plugin.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { ipcRenderer } = require('electron');
2+
3+
const promise = new Promise((res) => {
4+
document.addEventListener('DOMContentLoaded', () => {
5+
plugin = underscript.plugin('UnderScript App');
6+
res(plugin);
7+
promise = undefined;
8+
}, { once: true });
9+
});
10+
11+
let plugin;
12+
function getPlugin() {
13+
if (plugin) return Promise.resolve(plugin);
14+
return promise;
15+
}
16+
17+
ipcRenderer.on('toast', (_, data) => {
18+
getPlugin().then((plugin) => {
19+
if (data.refresh) {
20+
data.onClose = (...args) => {
21+
location.reload();
22+
};
23+
delete data.refresh;
24+
}
25+
plugin.toast(data);
26+
return plugin;
27+
});
28+
});
29+
30+
module.exports = getPlugin;

0 commit comments

Comments
 (0)