Electron.NET icon indicating copy to clipboard operation
Electron.NET copied to clipboard

Prevent opening new windows

Open delasource opened this issue 2 years ago • 1 comments

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

delasource avatar Mar 03 '22 12:03 delasource

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);
  });

danatcofo avatar Mar 03 '22 14:03 danatcofo