Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new-win-policy prevent default #2926

Closed
FezVrasta opened this issue Jan 12, 2015 · 18 comments
Closed

new-win-policy prevent default #2926

FezVrasta opened this issue Jan 12, 2015 · 18 comments
Assignees

Comments

@FezVrasta
Copy link

I'm trying to access Google Inbox using a node-webkit app and I/m trying to make my app open every link on the default browser listening for new-win-policy, the problem is that when I click on a link it gets opened on the default browser but it gets even opened on a new node-webkit window.

Is there a way to call some kind of preventDefault() on this event?

This is the very simple app:

<html>
  <head>
    <title>Google Inbox</title>
    <style>
      body {
        margin: 0px;
        overflow: hidden;
      }
      iframe {
        border: 0;
        width: 100%;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <iframe src="https://inbox.google.com" nwdisable nwfaketop></iframe>
    <script>
      var gui = require('nw.gui');
      var win = gui.Window.get();
      win.on('new-win-policy', function (frame, url, policy) {
        gui.Shell.openExternal(url);
        policy.ignore();
      });
    </script>
  </body>
</html>

Thanks!

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@marcfowler
Copy link

I've tried that code and it works for me - it opens the URL in my default browser (Chrome) and nothing changes in the node-webkit window.

What happens for you? (I'm on OSX 10.10.1 and node-webkit 0.11.5)

@FezVrasta
Copy link
Author

Every time I click on an URL it gets opened on both the default browser and a new node-webkit window.

@FezVrasta
Copy link
Author

@FezVrasta
Copy link
Author

Any update?

@tommoor
Copy link

tommoor commented Jan 15, 2015

Try

win.on('new-win-policy', function(frame, url, policy){
  policy.forceNewWindow();
});

@FezVrasta
Copy link
Author

with it two node-webkit windows get opened

@tommoor
Copy link

tommoor commented Jan 18, 2015

It works for me on Mac, which platform are you using / have you tested on?

@FezVrasta
Copy link
Author

Yosemite

@Joblytics-io
Copy link

I have the same problem on windows 7, NW 0.11.6.

It happens just on gmail.com with elements targer="_blank" (links in linkedin.com email's is best to reproduce)
I override gmail's window.open to this.

 window.open=function(){console.log(arguments)};

This is mi new-win-policy

win.on('new-win-policy', function(frame, url, policy){
  policy.ignore();
});

But the window is still created.
It works as expected on other sites.

@Joblytics-io
Copy link

The problem is in simulating javascript click on element with target _blank.
Here is web page loaded in iframe to reproduce problem.

<html>
    <script>
        function openTest (url){
            var a = document.createElement("a");
            a.target = "_blank";
            a.href = url;
            a.click()
        }
    </script>
    <button onclick="openTest('http://google.com')">Create Window</button>
</html>

Event new-win-policy did not call, but the window is still created.

win.on('new-win-policy', function(frame, url, policy){
  policy.ignore();
});

@loretoparisi
Copy link

I confirm that having an element like

<a href="http://about.musixmatch.com" target="_blank" data-reactid=".orogdvcw.0.2.1.0.2.0">Apps</a>

the

var eventsNewWinPolicy = function(frame, url, policy) {
      console.log("navigate to "+url);
      policy.ignore();
      gui.Shell.openExternal( url );
    };

will only work once, then the policy ignore will not work, and a new window will be opened.

@VoidVolker
Copy link

VoidVolker commented Sep 9, 2016

will only work once, then the policy ignore will not work, and a new window will be opened.

Same problem in 17.1: it work only once and then not working at all.

@loretoparisi
Copy link

@VoidVolker 👍

@rogerwang rogerwang self-assigned this Sep 9, 2016
@rogerwang
Copy link
Member

@VoidVolker the following always works for me with 0.17.1:

<html>
<head>
<title></title>
<script>
var win = nw.Window.get();
win.on('new-win-policy', function(frame, url, policy) { 
  console.log("new-win-policy called: " + url); 
  policy.ignore();
});
</script>
</head>
<body>
<a href="target.html" target="_blank">new window</a>
</body>
</html>

@VoidVolker
Copy link

Yes, for me too. But place a into new window page - and you will see, when user click link into this new window. Then it stop working.

@rogerwang
Copy link
Member

Are you suggesting it stops working in the new window? That works for me too. Please upload a full sample.

Here is mine:

index.html

<html>
<head>
<title></title>
<script>
var win = nw.Window.get();
win.on('new-win-policy', function(frame, url, policy) { 
  console.log("new-win-policy called in main win: " + url); 
  //policy.ignore();
});
</script>
</head>
<body>
<a href="target.html" target="_blank">new window</a>
</body>
</html>

target.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf8">
    <title></title>
<script>
var win = nw.Window.get();
win.on('new-win-policy', function(frame, url, policy) { 
  console.log("new-win-policy called: " + url); 
  policy.ignore();
});
</script>
</head>
<body>
<p>t1</p>
<a href="t2.html" target="_blank">new window 2</a>
</body>
</html>

@rogerwang
Copy link
Member

close until more information is provided.

@FezVrasta
Copy link
Author

really? LOL

better use Electron at this point if this is the professionality here 😓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants