Javascript 试图让userscript同时适用于Firefox和Chrome

Javascript 试图让userscript同时适用于Firefox和Chrome,javascript,google-chrome,firefox,Javascript,Google Chrome,Firefox,我在Chrome/Tampermonkey和Firefox/Greasemonkey上都无法使用这个用户脚本 userscript从我登录的域收集信息,但不在我的控制范围内,并向我的服务器发送POST请求,服务器根据信息创建并返回交互式网页的HTML 以下内容适用于Chrome,但不适用于Firefox[删除了数据擦除部分]。 Firefox没有错误或警告,它只打开一个空白窗口,没有写入任何内容 // ==UserScript== // @name GradeAssist //

我在Chrome/Tampermonkey和Firefox/Greasemonkey上都无法使用这个用户脚本

userscript从我登录的域收集信息,但不在我的控制范围内,并向我的服务器发送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);
/==UserScript==
//@name-GradeAssist
//@在文档空闲时运行
//@grant GM_xmlhttpRequest
//==/UserScript==
...
var getreportpdf=函数(){
...
元素[i].innerHTML+=“成绩辅助:单击此处”;
...
document.getElementById(“monkey”).addEventListener(“单击”,传输,错误);
}
...
var传输=功能(){
GMxmlHttpRequest({
方法:“POST”,
网址:'http://XXXX.XXX/post.php',
标题:{
“用户代理”:“Mozilla/4.0(兼容)Greasemonkey”,
“Accept”:“application/json,text/html”,
“内容类型”:“应用程序/x-www-form-urlencoded”
},
数据:“json=“+mydata,
onload:函数(responseDetails){
如果(responseDetails.status==200){
gradinghtml=responseDetails.responseText;
gradingwindow=window.open('about:blank','u blank');
grading window.document.open();
gradingwindow.document.write(gradinghtml);
gradingwindow.document.close();
}
否则{
警报(“脚本不工作”+responseDetails.status);
}
}
});
}
getreportpdf();
getstudentname();
getmodelasnwer();
getturnitinscore();
getturnitinlink();
getcoursename();
var集合=[];
var cLength=collection.length;
集合[cLength]={};
集合[cLength].studentname=studentname;
集合[cLength].reportpdf=reportpdf;
集合[cLength].modelanswer=modelanswer;
集合[cLength]。turnitinscore=turnitinscore;
集合[cLength]。turnitinlink=turnitinlink;
集合[cLength].coursename=coursename;
mydata=JSON.stringify(集合);
最酷的是,我可以根据用户对弹出窗口的输入,在脚本运行的原始页面中填充字段

我认为这与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);
            }
        }
    });
};
函数addJS_节点(){
var scriptNode=document.createElement('script');
scriptNode.type=“text/javascript”;
scriptNode.textContent='('+fireNewTab.toString()+')();
var targ=document.getElementsByTagName('head')[0]|| document.body | document.documentElement;
target.appendChild(脚本节点);
}
函数fireNewTab(文本){
var newTab=window.open('about:blank','u blank');
newTab.addEventListener(
“加载”,
函数(){
警报(“此处”);
var destDoc=newTab.document;
destDoc.open();
destDoc.write(“
  • a
  • b
  • c
    • ”); destDoc.close(); }, 假的 ); } var传输=功能(){ GMxmlHttpRequest({ 方法:“POST”, 网址:'http://XXXXX.XXX/post.php', 标题:{ “用户代理”:“Mozilla/4.0(兼容)Greasemonkey”, “Accept”:“application/json,text/html”, “内容类型”:“应用程序/x-www-form-urlencoded” }, 数据:“json=“+mydata, onload:函数(responseDetails){ 如果(responseDetails.status==200){ gradinghtml=responseDetails.responseText; addJS_节点(); } 否则{ 警报(“脚本不工作”+responseDetails.status); } } }); };
然后它可以在Firefox中工作,但不能在Chrome中工作,因为在Chrome中,事件侦听器似乎消失了。删除事件侦听器在两种浏览器中都有效

但是您可以看到我的问题,现在我无法将包含要显示到新页面的write函数中的网页的responseText获取


有什么建议吗?

关于Firefox的更多发现:
fireNewTab.call(this.gradinghtml).toString()将正确的数据输入函数,但open/write/close不再工作。带或不带addEventListener。简单地将参数传递到fireNewTab会使打开/写入/关闭不起作用。