nonchip
nonchip

Reputation: 1142

run javascript when opening another page

I'd like to open another web page from javascript (like default navigating, not fullscreen iframe or window.open) and run some javascript code upon loading it.

Steps:

  1. Define a javascript function
  2. Navigate to another page
  3. Browser runs function in new page context

is there any way to achieve this? the only way I remember would be emulating this by loading the page using ajax, and replacing document.body.innerHtml, then running the function, but that would not change location.href, so e.g. the back button or bookmarks wouldn't work. also relative links had to be rewritten at loading, etc...

PS: I know that would be some ugly XSS, but it's needed for example when writing bookmarklets that load a page and fill in a form automatically.

Upvotes: 1

Views: 293

Answers (4)

nonchip
nonchip

Reputation: 1142

OK, reserch results:

Impossible methods:

  • using some persistent function to run on the new page (like I suggested in the question)
  • using an iframe replacing the whole page (ugly and forbidden by most browsers)

Overkill methods:

  • write a browser plugin
  • get the remote page owner to accept some GET argument and do the stuff for you

Remaining method 1, requiring user interaction:

(function(){
  if(self.location.href!=targetlocation){
    window.alert("Please run this bookmarklet again upon loading the page.");
    self.location.href=targetlocation;
  }else{
    doSomething();
  }
  return false;
})();

Remaining method 2, doing some nasty proxy stuff:

  • write some php/etc script to "proxy" the target page
  • now you can:
    • use an iframe, because it's not cross-site anymore
    • rewrite the page server-side before delivering to browser (doing "overkill method: GET argument" by yourself)

Example:

<?php //USAGE: ?uri=http://www.google.com&search[0]=L2dvb2dsZS8K&replace[0]=dGVzdAo=
  $page=file_get_contents($_GET["uri"]);
  $count = min(count($_GET["search"]),count($_GET["replace"]),100);
  for ($i = 0; $i < $count; $i++) {
    $page=preg_replace(base64_decode($_GET["search"][$i]),
                       base64_decode($_GET["replace"][$i]), $page);
  }
  echo $page;
?>

Upvotes: -1

bjelli
bjelli

Reputation: 10090

You can do all this stuff if you load the other page from your own server:

Say you want to load http://other.com in your site http://mine.org You write a tiny serverside script that you can call like this: http://mine.org/load.php?site1 (with the urls to all the sites you want to load listed inside load.php or in some database)

but now your site has the security problem: javascript embedded in http://other.com is run in your sites context.

Upvotes: 1

fardjad
fardjad

Reputation: 20394

While there could be some legitimate use cases, for security reasons you can't do this unless the new page is on the same domain.

What you can do is to write a browser extension if the target browser has extensions support,

Or tell users to open the target page, and use your bookmarklet on that context.

Upvotes: 1

Paul
Paul

Reputation: 141829

No, you can't do that. That would allow you to do things like steal cookies for session hijacking behind the scenes, so no browsers allow you to do it at all.

Upvotes: 2

Related Questions