網站搜尋

typescript - electron setWindowOpenHandler 不是一個函數


我為我的網頁應用程式創建了一個 Electron 應用程式,它使用 SAML。我想允許 SAML 視窗顯示出來。

所以我使用了 BrowserWindow.WebContents.setWindowOpenHandler 函數。

我第一次嘗試的是,在使用新的 BrowserWindow 實例化 window 對象的地方。那個方法不起作用。我在 GitHub 上找到了一個新方法,使用事件 "web-contents-created"

//Erlaube SAML Authentifizierung, andere Seiten werden blockiert
app.on("web-contents-created", (createEvent, contents) => {
  contents.setWindowOpenHandler(({url}) => {
    if(url.startsWith("https://login.microsoftonline.com/")) {
      return {action: 'allow'};
    }
    else return {action: 'deny'};
  });
});

那個也不行,我總是收到相同的錯誤訊息:

我正在使用 TypeScript,這是有效的程式碼。我正在使用 Electron 17.1.2

有人知道為什麼以及怎麼做嗎?

答案#1

你可以在主窗口中使用

mainWindow.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('https://www*')) {
  return {
    action: 'allow',
    overrideBrowserWindowOptions: {
      width: 624,
      height: 428,
    },
  };
} else {
  console.log("Blocked by 'setWindowOpenHandler'");
  return { action: 'deny' };
}

});