Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

bookmarklet - Using Javascript to Open a New Page and Populate Form Values There

I am using JavaScript in a bookmarklet to populate form elements on a website:

javascript:var f = document.forms[0];
f.getElementsByTagName('input')[0].value = 'myname';
f.getElementsByTagName('input')[1].value = 'mypassword';
f.getElementsByTagName('input')[2].click

This works. However what I would like to create is a bookmarklet so that it opens the target page, and populates the values there; however it seems that onces the page is loaded, other JavaScript codes are not executed. So, the following doesn't work.

javascript:window.location("mywebsite");var f = document.forms[0];
f.getElementsByTagName('input')[0].value = 'myname';
f.getElementsByTagName('input')[1].value = 'mypassword';
f.getElementsByTagName('input')[2].click;

I have also experimented with setTimeout to delay the execution of my code, but that didn't work.

javascript:var f = document.forms[0];setTimeout("f.getElementsByTagName('input')[0].value = 'myname';f.getElementsByTagName('input')[1].value = 'mypassword';f.getElementsByTagName('input')[2].click;",1000);

How can I load my script once I know the target page is fully loaded?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't use GreaseMonkey as a personal rule, to code for browsers that shouldn't use it. Bookmarklets are a least-common-denominator approach to automate logins when your system is locked down and won't allow install of Greasemonkey, Roboform, etc.

I've coded a lot of login bookmarklets and thought about what you're trying to do: add some script that gets executed after a page loads. I came to this page looking for the solution, but now I'm glad it doesn't work.

Think about the security implications of this. If it were possible to echo keystrokes to to a loaded page, it would also be able to listen to keystrokes and send them elsewhere -- very bad.

If you want to automate logins, try a bookmarklet pattern like this (remove line breaks):

javascript:
    u='my_username';
    p='my_password';
    l='https://my_server/signon.aspx'; 
    if(location!=l)location=l;
    else{
     g=document.getElementById; 
     ue=(g('username') || g('userid') || g('login_name'));
     if(ue){
      ue.value=u;
      pe=(g('password') || g('pw') || g('pin'));
      pe.value = p;
      b=(g('submit_button') || g('signon_button') || g('login_button'));
      document.close();
      if(b)b.click();
     } 
    }

Clicking the link once takes you to the signon.aspx page. Once the username field is available on the loaded page, clicking the same link again will fill the form and submit.

So it's one more click than you hoped, but if you put the bookmarklet on a toolbar it's hardly any delay. Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...