使用 Javascript 打开一个新页面并在那里填充表单值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3717361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Javascript to Open a New Page and Populate Form Values There
提问by ustun
I am using JavaScript in a bookmarklet to populate form elements on a website:
我在书签中使用 JavaScript 来填充网站上的表单元素:
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 代码就不会被执行。所以,以下不起作用。
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.
我还尝试过使用 setTimeout 来延迟我的代码的执行,但这没有用。
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?
一旦我知道目标页面已完全加载,我该如何加载我的脚本?
采纳答案by epascarello
Greasemonkeyis built into Chrome, sounds like you are trying to reinvent the wheel. Install a JS file and it will run when the page loads.
Greasemonkey内置于 Chrome 中,听起来您正在尝试重新发明轮子。安装一个 JS 文件,它会在页面加载时运行。
回答by Rich Moss
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.
我不使用 GreaseMonkey 作为个人规则,为不应该使用它的浏览器编码。当您的系统被锁定并且不允许安装 Greasemonkey、Roboform 等时,书签是一种最不常见的自动登录方法。
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.
单击该链接一次即可转到 signon.aspx 页面。一旦用户名字段在加载的页面上可用,再次单击相同的链接将填写表单并提交。
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!
因此,点击次数比您希望的多一次,但是如果您将书签放在工具栏上,则几乎不会有任何延迟。祝你好运!

