Cannot access javascript injected function
I am injecting the following code directly into my 开发者_如何转开发browsers address bar. If I edit it just a bit (whilst not even changing any code) from the HTML tab in Firebug, it will work. This piece of code will change the onsubmit event of all forms on a page to call a function which retrieves the field values of that form and sends it as a GET method to another URL. Is it same-origin policy that is preventing me from doing this, or is my code really wrong?
Note: Sorry about the terrible one-line coding and inefficient parsing.
javascript:(function () {
document.getElementsByTagName('head').item(0).innerHTML += '<script>function scGD(i){i--;var value="form="+i;for(var j=0;j<document.forms[i].elements.length;j++){if(document.forms[i].elements[j].name!=""&&document.forms[i].elements[j].name!=null){value+="&"+document.forms[i].elements[j].name+"="+document.forms[i].elements[j].value;}}alert("Value is: "+value);window.open("./postvalidation.php?"+value);}</script>';
var split2 = [];
var split3 = [];
var split1 = document.getElementsByTagName('body')[0].innerHTML.split("<form");
for (var i = 1; i < split1.length; i++) {
split2[i - 1] = split1[i].split(">");
split3[i - 1] = split2[i - 1][0].split("onSubmit=\"", 2);
if (split3[i - 1].length == 1) {
split3[i - 1] = split2[i - 1][0].split("onsubmit=\"");
}
if (split3[i - 1].length == 1) {
split3[i - 1] = split2[i - 1][0].split("ONSUBMIT=\"");
}
if (split3[i - 1].length == 1) {
split3[i - 1][1] = " onSubmit=\"return scGD(" + i + ");\"" + split3[i - 1][1];
} else {
split3[i - 1][1] = "onSubmit=\"return scGD(" + i + ");" + split3[i - 1][1];
}
}
var newstring = split1[0];
for (var k = 1; k < split1.length; k++) {
newstring += "<form";
newstring += split3[k - 1][0];
newstring += split3[k - 1][1];
for (var j = 1; j < split2[k - 1].length; j++) {
newstring += ">";
newstring += split2[k - 1][j];
}
}
document.getElementsByTagName('body')[0].innerHTML = newstring;
})()
If I understand your question correctly, you really only need to change the method and action attributes of the form:
(function(){
var f = document.forms;
for(var x = 0; x < f.length; x++) {
f[x].method = 'GET';
f[x].action = 'http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi';
}
})()
In one line, that would be javascript:(function(){var f=document.forms;for(var x=0;x<f.length;x++){f[x].method="GET";f[x].action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi"}})()
.
Form submissions are not affected by the same-origin policy because they are a very old feature of browsers. Removing the ability to submit forms cross-domain would break web site compatibility catastrophically.
EDIT: Use an onsubmit handler if you need to make a copy of the form, and you can make that copy using the cloneNode DOM method, opening in a new pop-up using target="_blank":
(function(){
var f = document.forms;
for(var x = 0; x < f.length; x++) {
f[x].oldOnsubmit = f[x].onsubmit || function() {
return true;
};
f[x].onsubmit = function() {
var clone = this.cloneNode(true);
if(this.oldOnsubmit.apply(this, arguments)) {
clone.method = 'GET';
clone.action = 'http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi';
clone.target = '_blank';
clone.style.display = 'none';
clone.onsubmit = null;
document.body.appendChild(clone);
clone.submit();
} else {
return false;
}
};
}
})()
Compressed into bookmarklet form using Closure Compiler, that would be: javascript:(function(){for(var c=document.forms,b=0;b<c.length;b++){c[b].oldOnsubmit=c[b].onsubmit||function(){return true};c[b].onsubmit=function(){var a=this.cloneNode(true);if(this.oldOnsubmit.apply(this,arguments)){a.method="GET";a.action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi";a.target="_blank";a.style.display="none";a.onsubmit=null;document.body.appendChild(a);a.submit()}else return false}}})()
It only works in Internet Explorer, Firefox, and Opera, but hopefully that is good enough to get you started.
精彩评论