简体   繁体   English

如何创建一个读取xml或json的Chrome扩展程序

[英]How I can create a Chrome extension that reads an xml or json

I tried to schedule a google chrome extension that I only pull one json or xml to display content from my website. 我尝试安排一个谷歌浏览器扩展程序,我只拉一个json或xml来显示我网站上的内容。 But I could not do this task might say some example of code to perform this task. 但我无法完成此任务可能会说一些代码来执行此任务。 I will be greatly thankful 我将非常感激

Here's an example of how to configure my manifest.json 这是一个如何配置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/"
   ]
}

And as I try to get a XML (if they know how I can achieve a JSON will be equally useful): 当我尝试获取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");
          }
});

Greetings. 问候。

You seem to have a number of errors in the code you provide. 您似乎在提供的代码中有许多错误。 I realize many of them might have been introduced while adapting the code for posting here and thus might not exist in the actual code. 我意识到其中许多可能是在调整代码发布时引入的,因此可能不存在于实际代码中。 In any case, this is how you should do it from scratch: 无论如何,这是你应该从头开始的:

Extension file-structure: 扩展文件结构:

extension-root-directory/
                       |___manifest.json
                       |___popup.html
                       |___popup.js

manifest.json: 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: popup.html:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="popup.js"></script>
    </head>
    <body>
        Loading...
    </body>
</html>

popup.js: 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 !");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM