试图让用户使用Firefox和Chrome

时间:2015-09-04 14:10:17

标签: javascript google-chrome firefox

我在让这个用户使用Chrome / Tampermonkey和Firefox / Greasemonkey时遇到问题。

用户脚本从我登录但未在我控制范围内的域收集信息,并向我的服务器发送POST请求,该服务器会根据信息创建并返回交互式网页的HTML。

以下适用于Chrome,但不适用于Firefox [编辑出数据抓取部分]。 Firefox没有提供错误或警告,只是打开一个空白窗口,没有任何内容写入。

// ==UserScript==
// @name         GradeAssist
// @run-at       document-idle
// @grant        GM_xmlhttpRequest
// ==/UserScript==

...

var getreportpdf = function () {
...
   elements[i].innerHTML += "<span id=\"monkey\"> GRADE ASSIST : CLICK HERE </span>";  
...
   document.getElementById ("monkey").addEventListener("click", transmit, false);
}

...

var transmit = function () {
    GM_xmlhttpRequest({
        method: 'POST',
        url: 'http://XXXX.XXX/post.php',
        headers: {
            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
            'Accept': 'application/json,text/html',
            'Content-type': 'application/x-www-form-urlencoded'
        },
        data: "json=" + mydata,
        onload: function (responseDetails) {
            if (responseDetails.status == 200) {
                gradinghtml = responseDetails.responseText;
                gradingwindow = window.open('about:blank', '_blank');
                gradingwindow.document.open();
                gradingwindow.document.write(gradinghtml);
                gradingwindow.document.close();
            }
            else {
                alert("Script not working" + responseDetails.status);
            }
        }
    });
}

getreportpdf();
getstudentname();
getmodelasnwer();
getturnitinscore();
getturnitinlink();
getcoursename();
var collection = [];
var cLength = collection.length;
collection[cLength] = {};
collection[cLength].studentname = studentname;
collection[cLength].reportpdf = reportpdf;
collection[cLength].modelanswer = modelanswer;
collection[cLength].turnitinscore = turnitinscore;
collection[cLength].turnitinlink = turnitinlink;
collection[cLength].coursename = coursename;
mydata = JSON.stringify(collection);

关于这一点很酷,我可以根据弹出窗口的用户输入填充运行脚本的原始页面中的字段。

我认为这与问题22651334中的Firefox问题有关;新的空白页面被认为是域外的。这个问题的建议有点工作。如果我修改脚本以从原始页面中打开新窗口:

function addJS_Node () {
    var scriptNode          = document.createElement('script');
    scriptNode.type         = "text/javascript";
    scriptNode.textContent  = '(' + fireNewTab.toString() + ')()';
    var targ = document.getElementsByTagName ('head')[0] || document.body || document.documentElement;
    targ.appendChild(scriptNode);
}

function fireNewTab (text) {
    var newTab = window.open ('about:blank', '_blank');
    newTab.addEventListener (
        "load",
        function () {
            alert("HERE");
            var destDoc = newTab.document;
            destDoc.open ();
            destDoc.write ('<html><head></head><body><ul><li>a</li><li>b</li><li>c</li></ul></body></html>');
            destDoc.close ();
        },
        false
   );
}

var transmit = function () {
    GM_xmlhttpRequest({
        method: 'POST',
        url: 'http://XXXXX.XXX/post.php',
        headers: {
            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
            'Accept': 'application/json,text/html',
            'Content-type': 'application/x-www-form-urlencoded'
        },
        data: "json=" + mydata,
        onload: function (responseDetails) {
            if (responseDetails.status == 200) {
                gradinghtml = responseDetails.responseText;
                addJS_Node();
            }
            else {
                alert("Script not working" + responseDetails.status);
            }
        }
    });
};

然后它适用于Firefox,但不适用于Chrome,事件监听器似乎消失了。删除事件侦听器适用于两种浏览器。

但是你看到了我的问题,现在我无法获得包含我想要显示在新页面的写入功能中的网页的responseText。

有什么建议吗?

0 个答案:

没有答案
相关问题