Skip to content

Commit 476c0d0

Browse files
committed
fix: update underscript properly
1 parent d0a67ba commit 476c0d0

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/electron.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path');
33
const keytar = require('keytar');
44
const { autoUpdater } = require('electron-updater');
55
const contextMenu = require('electron-context-menu');
6+
const checkVersion = require('./underscript');
67

78
function createWindow() {
89
const win = new BrowserWindow({
@@ -56,22 +57,51 @@ function createWindow() {
5657
}
5758
});
5859

60+
function update() {
61+
checkVersion().then((updated) => {
62+
if (!updated) return;
63+
win.webContents.send('toast', {
64+
title: 'Updated UnderScript',
65+
text: 'Refresh page to finish update',
66+
refresh: true,
67+
});
68+
}).catch((error) => {
69+
win.webContents.send('toast', {
70+
title: 'Error updating UnderScript',
71+
error,
72+
});
73+
});
74+
}
75+
5976
win.webContents.on('will-navigate', (event, url) => {
60-
const { host } = new URL(url);
77+
const { host, protocol } = new URL(url);
6178
if (host === 'undercards.net') return;
6279

6380
event.preventDefault();
81+
if (protocol !== 'http:' && protocol !== 'https:') return;
6482
if (host === 'www.undercards.net') {
6583
win.loadURL(url.replace('www.', ''));
66-
} else if (host === 'unpkg.com' && url.endsWith('undercards.user.js')) {
67-
// Trigger update
84+
} else if (url.endsWith('undercards.user.js')) {
85+
update();
86+
} else {
87+
shell.openExternal(url);
88+
}
89+
});
90+
win.webContents.on('new-window', function(e, url) {
91+
e.preventDefault();
92+
const { host, protocol } = new URL(url);
93+
if (protocol !== 'http:' && protocol !== 'https:') return;
94+
if (host === 'undercards.net' || host === 'www.undercards.net') {
95+
win.loadURL(url.replace('www.', ''));
96+
} else if (url.endsWith('undercards.user.js')) {
97+
update();
6898
} else {
6999
shell.openExternal(url);
70100
}
71101
});
72102

73103
autoUpdater.on('update-downloaded', (info) => {
74-
// win.webContents.send('message', 'Restart to update.');
104+
win.webContents.send('toast', 'Restart to update.');
75105
})
76106

77107
autoUpdater.checkForUpdatesAndNotify();

0 commit comments

Comments
 (0)