2013-07-22 74 views
0

我的结构如下所示: enter image description here谷歌浏览器扩展程序。获取JSON数据局部

我的manifest.json如下图所示:

我的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> 

我的配置.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值的正确方法吗?

+0

谢谢你的帮助。 – fuat

回答

1

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

自Chrome 29,新chrome.runtime.getPackageDirectoryEntry可以访问包与HTML5文件系统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)。

但是,Rob W在评论中指出了代码无法正常工作的原因。

+0

谢谢你的帮助 – fuat