0

I wrote a GreaseMonkey script for developers which makes a pop up GUI if they click to a specific text area in our bug report tool.
Now I want to change that script to get the format of that GUI from an XML at one of our network drive (which is mapped for everyone), so i don't have to change the script and they don't have to reinstall it everytime we change the desired format of that status log.

The problem is, that I can't open the xml file. I'm trying to get it with this code I found on w3schools:

function loadXMLDoc(filename) 
{
    if (window.XMLHttpRequest)
    {
        xhttp=new XMLHttpRequest();
    } 
    else // code for IE
    {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET", filename, false);
    xhttp.send();
    return xhttp.responseXML;
}

var xmldoc = loadXMLDoc("file://///path\to\file\note.xml");
console.log(xmldoc.getElementsByTagName("body")[0].childNodes[0].nodeValue);

The xml is very simple, I just put it there for testing:

<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

I tried simple path syntax like:

file://///y:\path\to\file\note.xml

and also an absolute unc path:

file://///servername\path\to\file\note.xml

and neither of them worked.
I wrote some print to console and the script runs until I want to use the loadXMLDoc function, then it just seems to die, it's not writing any error.
It makes the problem harder, that I don't have access for the webpage's source where I want to use the script and neither to the server's administration where the file will be stored.

@Edit:

FireFox is 40.0.3, GreaseMonkey is 3.3, and if I add @grant GM_xmlhttpRequest the script breaks.
Now I'm using this in Tampermonkey:

var xmlText = xmlRequest("http://path/to/file/note.xml");
unsafeWindow.console.log(xmlText);

function xmlRequest(path) { 
     GM_xmlhttpRequest({
        method: "GET",
        url: path,
        onload: function(response) {
            unsafeWindow.console.log(response.responseText);
            return response.responseText;
        },
        onerror: function(reponse) {
            unsafeWindow.alert("error: ", reponse);
        }
    });
}

The onload returns nothing (undefined), but when the page loaded I get the responseText to the console log.
I'm trying to add setTimeout, or making the GM_xmlhttpRequest synchronous, but falied so far.

shepy02
  • 1
  • 2
  • Try using the standard url syntax with `/` instead of ```\``` (an escape symbol in js). Also, you can simply dragndrop the file into the browser and it will show the correct `file://` url. – wOxxOm Sep 04 '15 at 10:19
  • Then I get `file:///y:/path/to/file/note.xml`. It seems to work, but then I get, that XMLHttpRequest is outdated and it's awful for user experience. I think it's because of sync request on the main thread and it would make the page to reload. What should I use instead? – shepy02 Sep 04 '15 at 10:39
  • Try using [GM_xmlhttpRequest](http://wiki.greasespot.net/GM_xmlhttpRequest) (remember to add it to the metablock `// @grant GM_xmlhttpRequest`). And avoid w3schools :-) – wOxxOm Sep 04 '15 at 10:48
  • You said "It seems to work". It shouldn't work at all! What version of browser and of Greasemonkey are you using? What error messages are you getting on the browser console? – Brock Adams Sep 09 '15 at 05:12

1 Answers1

0

I solved the edit part with a callback mentioned here:

https://stackoverflow.com/a/5485561/5271745

It's still not working in FF+GM (TamperMonkey in chrome is okay), but maybe this will solve that too:

https://stackoverflow.com/a/25226796/5271745

Community
  • 1
  • 1
shepy02
  • 1
  • 2