我想填写通过JavaScript表单上的字段。 问题是,我只知道如何在当前页面上执行JavaScript,所以我不能重定向到形式,并从那里执行代码。 我不愿用这个词,但是,想到的唯一的一句话就是跨站点脚本。 我试图执行的代码如下。
<script language="javascript">
window.location = "http://www.pagewithaform.com";
loaded();
//checks to see if page is loaded. if not, checks after timeout.
function loaded()
{
if(window.onLoad)
{
//never executes on new page. the problem
setTitle();
}
else
{
setTimeout("loaded()",1000);
alert("new alert");
}
}
//sets field's value
function setTitle()
{
var title = prompt("Field Info","Default Value");
var form = document.form[0];
form.elements["fieldName"].value = title;
}
</script>
我并不真正知道这是可能的。 我也开放给其他想法,比如PHP。 谢谢。
编辑:第二页是SharePoint形式。 我不能编辑任何形式的代码。 我们的目标是编写一个脚本,预先填写大多数字段,因为90%的人是静态的。
你试图页之间维持状态。 传统上有两种方法来维护状态:
无论哪种方式,您的第一页有持续状态(要么cookie或查询字符串),另一页有 - 分开 - 恢复状态。 你不能在这两个网页使用相同的脚本。
例如:使用Cookie
使用cookie,第一页会写所有你需要的下一个页面上的cookie表单数据:
<!DOCTYPE html>
<html>
<head>
<title>Maintaining State With Cookies</title>
</head>
<body>
<div>
Setting cookies and redirecting...
</div>
<script>
// document.cookie is not a real string
document.cookie = 'form/title=My Name is Richard; expires=Tue, 29 Aug 2017 12:00:01 UTC'
document.cookie = 'form/text=I am demoing how to use cookies in JavaScript; expires=Tue, 29 Aug 2017 12:00:01 UT';
setTimeout(function(){
window.location = "./form-cookies.html";
}, 1000);
</script>
</body>
</html>
...和第二页会那么读取这些cookies和填充他们的表单字段:
<!DOCTYPE html>
<html>
<head>
<title>Maintaining State With Cookies</title>
</head>
<body>
<form id="myForm" action="submit.mumps.cgi" method="POST">
<input type="text" name="title" />
<textarea name="text"></textarea>
</form>
<script>
var COOKIES = {};
var cookieStr = document.cookie;
cookieStr.split(/; /).forEach(function(keyValuePair) { // not necessarily the best way to parse cookies
var cookieName = keyValuePair.replace(/=.*$/, ""); // some decoding is probably necessary
var cookieValue = keyValuePair.replace(/^[^=]*\=/, ""); // some decoding is probably necessary
COOKIES[cookieName] = cookieValue;
});
document.getElementById("myForm").getElementsByTagName("input")[0].value = COOKIES["form/title"];
document.getElementById("myForm").getElementsByTagName("textarea")[0].value = COOKIES["form/text"];
</script>
</body>
</html>
示例:使用查询字符串
在使用查询字符串的情况下,第一页将只包括在重定向URL的查询字符串,就像这样:
<!DOCTYPE html>
<html>
<head>
<title>Maintaining State With The Query String</title>
</head>
<body>
<div>
Redirecting...
</div>
<script>
setTimeout(function(){
window.location = "./form-querystring.html?form/title=My Name is Richard&form/text=I am demoing how to use the query string in JavaScript";
}, 1000);
</script>
</body>
</html>
......而形式会那么(通过提供的JavaScript解析查询字符串window.location.search
-一个前缀?
):
<!DOCTYPE html>
<html>
<head>
<title>Maintaining State With The Query String</title>
</head>
<body>
<form id="myForm" action="submit.mumps.cgi" method="POST">
<input type="text" name="title" />
<textarea name="text"></textarea>
</form>
<script>
var GET = {};
var queryString = window.location.search.replace(/^\?/, '');
queryString.split(/\&/).forEach(function(keyValuePair) {
var paramName = keyValuePair.replace(/=.*$/, ""); // some decoding is probably necessary
var paramValue = keyValuePair.replace(/^[^=]*\=/, ""); // some decoding is probably necessary
GET[paramName] = paramValue;
});
document.getElementById("myForm").getElementsByTagName("input")[0].value = GET["form/title"];
document.getElementById("myForm").getElementsByTagName("textarea")[0].value = GET["form/text"];
</script>
</body>
</html>
实施例:与片段标识符
还有一个选择:因为国家正在严格保持在客户端(不是个服务器端),你可以把一个片段标识符信息(URL的“哈希”部分)。
所述第一脚本是非常类似于上面的查询字符串例子:重定向URL只是包括片段标识符。 我要再次使用的查询字符串格式化为方便起见,但注意到#
,其中一个在的地方?
以前是:
<!DOCTYPE html>
<html>
<head>
<title>Maintaining State With The Fragment Identifier</title>
</head>
<body>
<div>
Redirecting...
</div>
<script>
setTimeout(function(){
window.location = "./form-fragmentidentifier.html#form/title=My Name is Richard&form/text=I am demoing how to use the fragment identifier in JavaScript";
}, 1000);
</script>
</body>
</html>
...然后形式具有解析片段标识符等:
<!DOCTYPE html>
<html>
<head>
<title>Maintaining State With The Fragment Identifier</title>
</head>
<body>
<form id="myForm" action="submit.mumps.cgi" method="POST">
<input type="text" name="title" />
<textarea name="text"></textarea>
</form>
<script>
var HASH = {};
var hashString = window.location.hash.replace(/^#/, '');
hashString.split(/\&/).forEach(function(keyValuePair) {
var paramName = keyValuePair.replace(/=.*$/, ""); // some decoding is probably necessary
var paramValue = keyValuePair.replace(/^[^=]*\=/, ""); // some decoding is probably necessary
HASH[paramName] = paramValue;
});
document.getElementById("myForm").getElementsByTagName("input")[0].value = HASH["form/title"];
document.getElementById("myForm").getElementsByTagName("textarea")[0].value = HASH["form/text"];
</script>
</body>
</html>
如果你不能为表单页面编辑代码
尝试的Greasemonkey脚本。
这是一个很好用的地方饼干
例如:从quirksmode.org
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
和一个侧面说明,您可以使用onload事件,知道什么时候该页面已准备就绪
<script language="javascript">
function setTitle(){
var title = prompt("Field Info","Default Value");
var form = document.form[0];
form.elements["fieldName"].value = title;
}
windows.onload = setTitle;
</script>
如果有可能得不到目标的系统/源/中间人的方法来操纵目标网站那么这将真正成为恶意软件的开放的高速公路,结合点击劫持! 我不想让你的脚本来告诉我的银行做什么的形式。 ;-)
使用某种形式的自动化工具,如AutoIt的(www.autoitscript.com)用于这一目的。 简单易学,它竟好形式整合。 如果标准是不够的,看起来像WinHTTP的为AutoIt的UDF的。