2

I'm having problems getting this userscript to work on both Chrome/Tampermonkey and Firefox/Greasemonkey.

The userscript gathers information from a domain I'm logged into, but outside my control, and sends a POST request to my server, which creates and returns the HTML of a interactive webpage from the information.

The following works in Chrome, but not Firefox [edited out the data scraping parts]. Firefox gives no errors or warnings, it just opens a blank window and nothing is written to it.

// ==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);

What's cool about this is that I can then populate fields in the original page that the script is running on, based on the user input to the popup window.

I think this is related to Firefox troubles in question 22651334; new blank pages are considered out of domain. The suggestions from that question sort of work. If I modify the script to open the new window from within the original page:

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);
            }
        }
    });
};

Then it works in Firefox, but not Chrome, where the event listener seems to disappear. Removing the event listener works in both browsers.

But you see my problem, now I can't get the responseText containing the web page I want to display into the write function for the new page.

Any suggestions?

Community
  • 1
  • 1
  • More discoveries on Firefox: `fireNewTab.call(this.gradinghtml).toString()` gets the right data into the function, but open/write/close no longer work. With or without the addEventListener. Simply passing parameters into fireNewTab makes open/write/close not work. – Thad Harroun Sep 04 '15 at 17:10

0 Answers0