tags:

views:

166

answers:

5

Hi I am looking into methods to inject javascript into any webpage loaded in the browser, so that I can traverse through the page's DOM. I use JQUERY for my scripting needs. Method should work in all browsers.

I tried using IFRAME and adding some html into it, but I cant. Please suggest some ways.

A: 

Take a look at jquery JSON and Wikipedia's JSON page.

Alternatively you can simply add a <script> tag to the document:

$("head").append('<script src="..." type="text/javascript"></script>');

This will load the javascript file.

Andreas Bonini
+2  A: 

You can't run Javascript on arbitrary Web pages that you do not control the content of. It would be a huge security hole if that were not true.

Think about it: you could run Javascript and wait for someone to log on to their internet banking and then do something with the characters input.

cletus
+1  A: 

Try using Greasemonkey: http://www.greasespot.net/. You can use it to execute custom scripts on page load for any website you want. You can find some basic tutorials here: http://diveintogreasemonkey.org/.

dutchflyboy
+1  A: 

I suggest to create a page with two iframes one to navigate to the designated website and other to get DOM Objects. in the first one navigate to the site and then select its HTML and append it in the body of the second Iframe.

iframe2.contentWindow.document.body.innerHTML = iframe1.contentWindow.document.body.innerHTML

then traverse the DOM Objects inside the second Iframe with your custom functions

Kronass
A: 

You could create a bookmarklet (see http://en.wikipedia.org/wiki/Bookmarklet) which in turn can add a node to the page, with the src pointing to where your own javascript is located. Onde the script node gets added it will run. You can find more details on http://www.johnvey.com/features/deliciousdirector/ under "how does it work?". This way you can have a bookmark in your bookmarks bar which, when click, will add your script to any page you happen to be on.

Ton van Bart