我试图安排一个Google chrome扩展程序,该扩展程序仅提取一个json或xml来显示我网站上的内容。但是我做不到此任务,可能会说一些执行此任务的代码示例。我将非常感谢
这是有关如何配置manifest.json的示例
{
"manifest_version": 2,
"name": "Name",
"description": "My WEB SITE DESCRIPTION."
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
"permissions": [
"http://domain.com/"
]
}
当我尝试获取XML时(如果他们知道如何实现JSON同样有用):
window.addEventListener("load", function inicial(){
xmlhttp.open("GET", "http://domain.com/feed", false);
xmlhttp.send(null);
if (xmlhttp.status==200) {
xmlDoc = xmlhttp.responseXML;
var links = xmlDoc.getElementsByTagName("link");
alert(links);
} else if (xmlhttp.status==404) {
alert("XML could not be found");
}
});
问候。
您提供的代码中似乎存在许多错误。我意识到其中许多可能是在修改代码以在此处发布时引入的,因此可能不存在于实际代码中。无论如何,这是从头开始的方法:
扩展文件结构:
extension-root-directory/
|___manifest.json
|___popup.html
|___popup.js
manifest.json:
{
"manifest_version": 2,
"name": "<EXTENSION-NAME>",
"description": "<EXTENSION-DESCRIPTION>",
"version": "1.0",
"browser_action": {
"default_title": "<BROWSER-ACTION-TITLE>",
"default_popup": "popup.html"
},
"permissions": [
"<URL-TO-XML-OR-JSON>" // e.g. "http://domain.com/feed"
]
}
popup.html:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="popup.js"></script>
</head>
<body>
Loading...
</body>
</html>
popup.js:
var ajaxURL = "<URL-TO-XML-OR-JSON>"; // e.g. "http://domain.com/feed"
window.addEventListener("load", function() {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", ajaxURL, false);
xmlhttp.send(null);
if (xmlhttp.status == 200) {
var data = xmlhttp.responseXML; // or JSON.parse(xmlhttp.responseText)
// Do stuff with the retrieved data...
console.log(data);
} else {
document.body.innerHTML("Failed to load the data !");
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句