简体   繁体   English

无法访问JavaScript注入功能

[英]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. 如果我只是从Firebug的HTML标签中对其进行了一点编辑(甚至没有更改任何代码),它将起作用。 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. 这段代码将更改页面上所有表单的onsubmit事件,以调用一个函数,该函数检索该表单的字段值并将其作为GET方法发送到另一个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"}})() . 在一行中,将是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": 编辑:如果需要制作表单的副本,请使用onsubmit处理程序,并且可以使用cloneNode DOM方法制作该副本,并使用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}}})() 使用Closure Compiler压缩成书签形式,将是: 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. 它仅适用于Internet Explorer,Firefox和Opera,但希望它足以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM