21

I have a javascript like this, used i a bookmarklet:

window.location="URL"
document.getElementById('username-id').value = 'User'
document.getElementById('pwd-id').value = 'pass'
doLogin();

The problem is that javascript stops as soon as it hits the URL. How to make it go to a page and execute a script?

Any help would be greatly appreciated.

Temikus
  • 309
  • 1
  • 3
  • 8

5 Answers5

18

I don't recommend browser extensions - they allow untrusted code to run in the context of trusted code. If you have a browser extension that loads a page and automatically enters your userID and password, what stops it from posting those credentials to a malicious site? Only you, and only if you've carefully read the source code.

To solve the problem above I've added bookmarklets to the favorites bar in my browser that require two clicks: 1. Click to navigate to the location 2. Click to fill the form

javascript: 
 var L='http://mysite.com/';
 if(location!=L)location=L;
 else{
  document.getElementById('username-id').value='User'; 
  document.getElementById('pwd-id').value = 'pass';
  document.close();
  doLogin(); 
 }

Good Luck!

Rich
  • 268
  • 2
  • 5
8

Unfortunately your bookmarklet will not work. Change of window.location results loading of the new page - you cannot execute any Javascript on that page without having the document present first.

The desired action can be achieved by browser extensions, e.g. Greasemonkey script, which execute specified Javascript on the page upon page load.

timdream
  • 5,914
  • 5
  • 21
  • 24
2

If you control the page you're loading, I would consider passing that data as the fragment and handling that from the page. Something like window.location="URL#username=...;password=...", than parsing the data from the fragment in the page and doing whatever you need with it. However, in this specific case its not a good idea as the password will be saved in the browsing history.

shesek
  • 4,584
  • 1
  • 28
  • 27
0

If you want to execute the bookmarklet from a page loaded on the same domain (and with the same scheme) as your target location, you can do it (sort of) by loading it into an iframe.

javascript:
document.body.innerHTML = '<iframe src="http://example.com/page2.html" onload="this.contentWindow.location.assign(`javascript:
    document.body.style.textTransform = \'uppercase\'; /* put code here */
void(0);`); this.onload = null;" width=100% height=100%>';

Note the use of back ticks for a multi-line string for legibility. The onload = null is required to prevent an infinite loop, as onload will fire after the location is assigned javascript code.

(Only tested in Chrome.)

Protector one
  • 6,926
  • 5
  • 62
  • 86
-2

Try wrapping your code with window.onload event.

window.location = "URL"; window.onload = function() {
document.getElementById('username-id').value = 'User'
document.getElementById('pwd-id').value = 'pass'
doLogin();
}

As the solution above doesn't work, you still should be able to write a simple browser extension to solve your problem. It's easy: choose the browser you use and follow the documentation. This is only way to do that.

Daniel O'Hara
  • 13,307
  • 3
  • 46
  • 68