WebView: Get event parameters via WebContents rather than WebviewTag.

Works around https://github.com/electron/electron/issues/31924.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2022-02-02 23:25:50 -08:00
parent 3a8541f601
commit 2f7529cd71
2 changed files with 11 additions and 15 deletions

View File

@@ -10,11 +10,9 @@ const dingSound = new Audio("../resources/sounds/ding.ogg");
export default function handleExternalLink( export default function handleExternalLink(
this: WebView, this: WebView,
event: Electron.NewWindowEvent, details: Electron.HandlerDetails,
): void { ): void {
event.preventDefault(); const url = new URL(details.url);
const url = new URL(event.url);
const downloadPath = ConfigUtil.getConfigItem( const downloadPath = ConfigUtil.getConfigItem(
"downloadsPath", "downloadsPath",
`${app.getPath("downloads")}`, `${app.getPath("downloads")}`,

View File

@@ -130,16 +130,18 @@ export default class WebView {
} }
registerListeners(): void { registerListeners(): void {
this.$el.addEventListener("new-window", (event) => { const webContents = this.getWebContents();
handleExternalLink.call(this, event);
webContents.setWindowOpenHandler((details) => {
handleExternalLink.call(this, details);
return {action: "deny"};
}); });
if (shouldSilentWebview) { if (shouldSilentWebview) {
this.getWebContents().setAudioMuted(true); webContents.setAudioMuted(true);
} }
this.$el.addEventListener("page-title-updated", (event) => { webContents.on("page-title-updated", (_event, title) => {
const {title} = event;
this.badgeCount = this.getBadgeCount(title); this.badgeCount = this.getBadgeCount(title);
this.props.onTitleChange(); this.props.onTitleChange();
}); });
@@ -152,9 +154,7 @@ export default class WebView {
this.canGoBackButton(); this.canGoBackButton();
}); });
this.$el.addEventListener("page-favicon-updated", (event) => { webContents.on("page-favicon-updated", (_event, favicons) => {
const {favicons} = event;
// This returns a string of favicons URL. If there is a PM counts in unread messages then the URL would be like // This returns a string of favicons URL. If there is a PM counts in unread messages then the URL would be like
// https://chat.zulip.org/static/images/favicon/favicon-pms.png // https://chat.zulip.org/static/images/favicon/favicon-pms.png
if ( if (
@@ -170,7 +170,6 @@ export default class WebView {
} }
}); });
const webContents = this.getWebContents();
webContents.addListener("context-menu", (event, menuParameters) => { webContents.addListener("context-menu", (event, menuParameters) => {
contextMenu(webContents, event, menuParameters); contextMenu(webContents, event, menuParameters);
}); });
@@ -181,8 +180,7 @@ export default class WebView {
this.show(); this.show();
}); });
this.$el.addEventListener("did-fail-load", (event) => { webContents.on("did-fail-load", (_event, _errorCode, errorDescription) => {
const {errorDescription} = event;
const hasConnectivityError = const hasConnectivityError =
SystemUtil.connectivityERR.includes(errorDescription); SystemUtil.connectivityERR.includes(errorDescription);
if (hasConnectivityError) { if (hasConnectivityError) {