Electron.NET
Electron.NET copied to clipboard
Prevent opening new windows
There is electrons "new-window" event. But i can not call event.preventDefault()
in C# code (event browserWindow.OnNewWindowForTab
). How am i be able to prevent opening new windows of my application? (for example when the user middle-mouse-clicks a link)
However, it is marked deprecated in the documentation in favor of this function on the webContents class. So how can i access that? There are plenty of methods that are not implemented in Electron.Net.
Related to #434
you have to do that in the javascript on the page itself. You can't do everything in .net.
Here is the code I use to prevent navigating away. For other similar scenarios I would suggest doing a google search. You have somewhat more freedom in the electron world than a standard browser.
// this prevents the window from navigation away.
document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('click', (e) => {
if (e.target.href && e.target.host != window.location.host) {
e.preventDefault();
e.stopPropagation();
setTimeout(() => {
// do something else here
}, 100);
return false;
}
}, true);
});