Google Chrome扩展程序。在本地获取json数据

时间:2013-07-22 12:12:16

标签: javascript json google-chrome google-chrome-extension

我的结构如下所示: enter image description here

我的manifest.json如下所示:

{
"manifest_version": 2,

"name": "Doktor-MD",
"description": "Tarayıcı üzerinden Doktor-MD'de link paylaşımı yapabilirsiniz.",
"version": "1.0",

"permissions": [
"http://www.google.com/"
],

"browser_action": {
"default_icon": "icon.png",
"default_popup": "index.html"
}
}

我的index.html如下所示:

<!DOCTYPE html>
<html>
<head>  
<META http-equiv=content-type content=text/html;charset=utf8>
<META http-equiv=content-type content=text/html;charset=windows-1254>
<META http-equiv=content-type content=text/html;charset=x-mac-turkish>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">  </script>  
<script src="doktormdcore.js"></script>
<title>Doktor TV Md</title>
<script type="text/javascript">
    function GetDataFunction()
    {
        var xhr = new XMLHttpRequest();
        xmlHttpRequest.onreadystatechange = handleStateChange; //  Implemented elsewhere.
        xmlHttpRequest.open("GET",  chrome.extension.getURL('/config_resources/config.json'), true);
        xmlHttpRequest.send();
    }
</script>
</head>
<body>
    <table>
        <tr>
            <td>GetData</td>
            <td><input type="button" id="GetData" value="Getir"     onclick="GetDataFunction"/></td>
        </tr>
    </table>
</body>
</html>

我的config.json数据(示例数据)如下所示:

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup   Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages  such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}
}

我想从config.json获取json数据。但是,我如何测试此代码以确保它们正常工作。你认为我是对的吗?这是从本地获取json值的正确方法吗?

1 个答案:

答案 0 :(得分:1)

在Chrome 29之前,检索扩展程序包内文件内容的唯一方法是XHR,这是正确的方法。

从Chrome 29开始,新的chrome.runtime.getPackageDirectoryEntry可让您使用HTML5 FileSystem API访问包内的文件(另请参阅http://www.html5rocks.com/en/tutorials/file/filesystem/http://www.w3.org/TR/file-system-api/https://developer.mozilla.org/en-US/docs/Web/API/DirectoryEntry)。< / p>

然而,Rob W在评论中指出了你的代码不起作用的原因。