我为开发人员编写了一个GreaseMonkey脚本,如果他们点击我们的错误报告工具中的特定文本区域,会弹出一个GUI。
现在我想要更改该脚本以从我们的网络驱动器之一(这是为所有人映射)的XML中获取该GUI的格式,所以我不必更改脚本并且不必重新安装它每次我们改变该状态日志的所需格式。从网络驱动器打开XML并使其准备解析
问题是,我无法打开xml文件。我想这个代码得到它我就发现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);
的XML很简单,我只是把它放在那里进行测试:
<?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>
我想简单的路径语法,如:
文件:///// Y:\路径\为\文件\ note.xml
,也绝对UNC路径:
文件://///服务器名称\路径\为\文件\ note.xml
和他们都没有工作。
我写了一些打印到控制台和脚本运行,直到我想使用loadXMLDoc函数,然后它似乎死了,它不写任何错误。
这让问题变得更加困难,我无法访问网页的源代码,我不想使用该脚本,也无法访问将存储文件的服务器管理。
@Edit:
Firefox是40.0.3,是的GreaseMonkey 3.3,如果我添加@grant GM_xmlhttpRequest脚本休息。
现在我在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);
}
});
}
的onload事件没有返回值(未定义),但加载页面时,我得到了responseText的控制台日志。
我试图添加setTimeout,或使GM_xmlhttpRequest同步,但到目前为止falied。
尝试使用标准url语法,而不是使用'''''(js中的转义符号)''。此外,您可以简单地将文件拖放到浏览器中,并显示正确的'file://'url。 – wOxxOm
然后我得到'file:/// y:/ path/to/file/note.xml'。它似乎工作,但后来我得到,XMLHttpRequest已过时,并且对用户体验来说太糟糕了。我认为这是因为主线程上的同步请求,它会使页面重新加载。我应该用什么来代替? – shepy02
尝试使用[GM_xmlhttpRequest](http://wiki.greasespot。net/GM_xmlhttpRequest)(记得将它添加到元块'// @grant GM_xmlhttpRequest')。并避免w3schools :-) – wOxxOm