Skip to content

Commit

Permalink
Add null checks due to Electron update.
Browse files Browse the repository at this point in the history
Electron adds more places where `null` may happen. This adds some checks
in places TypeScript suggest it does, so no exceptions might happen
because of unhandled `null` type.
  • Loading branch information
SpacingBat3 committed Dec 25, 2024
1 parent d410d04 commit e456baf
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion sources/code/main/modules/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void import("electron/main")
.then(ipc => ipc.on("settings-config-modified",
(event, config:AppConfig) => {
// Only permit the local pages.
if(new URL(event.senderFrame.url).protocol === "file:")
if(event.senderFrame && new URL(event.senderFrame.url).protocol === "file:")
appConfig.value = config;
})
);
6 changes: 3 additions & 3 deletions sources/code/main/windows/about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export default function showAboutPanel(parent:Electron.BrowserWindow): Electron.
};
if(aboutPanel === undefined) return;
ipc.once("about.close", (event) => {
if(!aboutPanel.isDestroyed() && event.senderFrame.url === aboutPanel.webContents.getURL())
if(!aboutPanel.isDestroyed() && event.senderFrame?.url === aboutPanel.webContents.getURL())
aboutPanel.close();
});
ipc.once("about.readyToShow", (event) => {
if(!aboutPanel.isDestroyed() && event.senderFrame.url === aboutPanel.webContents.getURL())
if(!aboutPanel.isDestroyed() && event.senderFrame?.url === aboutPanel.webContents.getURL())
aboutPanel.show();
});
ipc.handle("about.getDetails", (event) => {
if(event.senderFrame.url === aboutPanel.webContents.getURL())
if(event.senderFrame?.url === aboutPanel.webContents.getURL())
return appDetails;
return undefined;
});
Expand Down
4 changes: 2 additions & 2 deletions sources/code/main/windows/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ async function handleEvents(docsWindow: Electron.BrowserWindow) {
(await e).ipcMain.removeHandler("documentation-load");
(await e).ipcMain.removeAllListeners("documentation-show");
(await e).ipcMain.handle("documentation-load", async (event) => {
if(event.senderFrame.url !== docsWindow.webContents.getURL()) return;
if(event.senderFrame?.url !== docsWindow.webContents.getURL()) return;
(await e).ipcMain.once("documentation-show", (event) => {
if(!docsWindow.isDestroyed() && event.senderFrame.url === docsWindow.webContents.getURL()) {
if(!docsWindow.isDestroyed() && event.senderFrame?.url === docsWindow.webContents.getURL()) {
docsWindow.show();
}
});
Expand Down
6 changes: 3 additions & 3 deletions sources/code/main/windows/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo

// Apply settings that doesn't need app restart on change
ipcMain.on("settings-config-modified", (event, object:null|PartialRecursive<AppConfig>) => {
if(new URL(event.senderFrame.url).protocol !== "file:")
if(event.senderFrame && new URL(event.senderFrame.url).protocol !== "file:")
return;
try {
// Menu bar
Expand Down Expand Up @@ -468,7 +468,7 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo
true
) ||
// Fail on different frame
req.frame.routingId !== win.webContents.mainFrame.routingId ||
req.frame?.routingId !== win.webContents.mainFrame.routingId ||
// Whenever user is the one who most likely triggered this
req.userGesture;

Expand Down Expand Up @@ -547,7 +547,7 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo
internalWindowEvents.on("api", (safeApi:string) => {
ipcMain.removeAllListeners("paste-workaround");
ipcMain.on("paste-workaround", (event, api:unknown) => {
if(safeApi !== api || event.senderFrame.url !== win.webContents.getURL()) return;
if(safeApi !== api || event.senderFrame?.url !== win.webContents.getURL()) return;
console.debug("[Clipboard] Applying workaround to the image...");
win.webContents.paste();
});
Expand Down
3 changes: 2 additions & 1 deletion sources/code/main/windows/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export default function loadSettingsWindow(parent:Electron.BrowserWindow):Electr
});
if(settingsWindow === undefined) return;
ipcMain.handle("settings-generate-html", (event) => {
if(new URL(event.senderFrame.url).protocol !== "file:") return;
if(event.senderFrame && new URL(event.senderFrame.url).protocol !== "file:")
return;
if(!settingsWindow.isDestroyed()) settingsWindow.show();
return htmlConfig;
});
Expand Down

0 comments on commit e456baf

Please sign in to comment.