使用 Javascript 打开新页面并在其中填充表单值
我正在书签中使用 JavaScript 来填充网站上的表单元素:
javascript:var f = document.forms[0];
f.getElementsByTagName('input')[0].value = 'myname';
f.getElementsByTagName('input')[1].value = 'mypassword';
f.getElementsByTagName('input')[2].click
这有效。然而,我想创建一个书签,以便它打开目标页面,并填充那里的值;然而,一旦页面加载,其他 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;
我还尝试过使用 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);
一旦我知道目标页面已完全加载,如何加载我的脚本?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不使用 GreaseMonkey 作为个人规则,为不应该使用它的浏览器编写代码。当您的系统被锁定并且不允许安装 Greasemonkey、Roboform 等时,书签是一种自动登录的最小公分母方法。
我编写了很多登录书签,并考虑了您想要做什么:添加一些在页面加载后执行的脚本。我来到此页面寻找解决方案,但现在我很高兴它不起作用。
考虑一下这对安全的影响。如果可以将击键回显到加载的页面,那么它也可以监听击键并将其发送到其他地方——这非常糟糕。
如果您想自动登录,请尝试像这样的书签模式(删除换行符):
单击链接一次,您将转到signon.aspx 页面。一旦用户名字段在加载的页面上可用,再次单击同一链接将填写表单并提交。
因此,这比您希望的多单击一次,但如果您将小书签放在工具栏上,则几乎没有任何延迟。祝你好运!
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):
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!
Greasemonkey 已构建进入 Chrome,听起来就像你正在尝试重新发明轮子。安装一个 JS 文件,它将在页面加载时运行。
Greasemonkey is built into Chrome, sounds like you are trying to reinvent the wheel. Install a JS file and it will run when the page loads.