0

I'm working with node-webkit. I want to do something when node-webkit visits some specific URL, but I don't know how to execute a script when node-webkit visits the other URL.

For example http://www.google.com. I can only execute native scripts, but how can I execute a script when I visit another URL like a chrome plugin?

<script type="text/javascript" src="">
    document.getElementById('id').click();
</script>

Script like this can only execute when visit my native script. How can I get it to execute when visiting http://www.google.com?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Arnold
  • 210
  • 1
  • 6
  • 13

2 Answers2

0

You can always know what the current url is: window.location

For your specific example it seems you misunderstood the point of node-webkit. Nw does not use http protocol; it does use file protocol. You do not navigate from one external web page to another external web page as your application is local (desktop).

Be more specific if you want additional help.

roland
  • 7,695
  • 6
  • 46
  • 61
  • I can visit one external web page with Nw,and chrome plugin can control a page's behavior,i just want to control the page.For example,when i visit google.com with Nw,the page will be shut down,how can i do it.I know I can use Nw as a browser,but how can i inject a script to a external web page? – Arnold Dec 14 '13 at 15:04
  • You cannot directly. You can however fetch the target page (google i.e.) and parse the content using a parser module. – roland Dec 14 '13 at 16:53
0
  • It's better not to include or write scripts directly in the HTML. The disadvantage of this technique is that the script (from which you might want to do some background tasks) will reload (or vanish) every time you reload (or navigate to another page).
  • 'node-main' in package.json is what you need. Here: https://github.com/rogerwang/node-webkit/wiki/node-main. The scripts stays there even if you visit another page.
hippietrail
  • 15,848
  • 18
  • 99
  • 158
Sagar
  • 26
  • 3
  • I read the example [node-main](https://github.com/rogerwang/node-webkit/wiki/node-main), but I still don't know how to controll a romote page.This example add ``, how can I add this to a romote page when this page isn't mine? – Arnold Dec 19 '13 at 15:21
  • You should be able to attach an onload listener in your js. `var gui = require('nw.gui'); var win = gui.Window.get(); win.on('loaded', function() { console.log('ureka') } );` – Sagar Dec 20 '13 at 08:37
  • when I try you code, my app will flash back, and I don't know how to solve it, my app just can't be opened.this is my code (https://github.com/Arnoldnuo/node-main) – Arnold Dec 21 '13 at 05:51
  • wrap everything in main.js into `setTimeout(function(){ #code_here},2000);` . The thing is, window object dsnt even exist when you try to access it the first time. So everything crashes. – Sagar Dec 21 '13 at 08:25
  • Also, you have written `win.on('minimize',`. Use `win.on('loaded',` in your case. – Sagar Dec 21 '13 at 15:27