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.