poi*_*ida 5 javascript google-chrome-app
在Google Chrome浏览器应用程序中,是否可以从background.js脚本中访问捆绑的数据文件?
例如,如果我有一个data.json包含在应用程序中的文件,是否可以在background.js脚本中使用JavaScript API 来获取文件内容?
使用示例包目录结构:
/app/manfifest.json
/app/backround.js
/app/data.json
Run Code Online (Sandbox Code Playgroud)
我想做类似的事情:
chrome.app.runtime.onLaunched.addListener(function() {
data = unknown.api.loadFileSync("data.json");
// do stuff with data
// ...
});
Run Code Online (Sandbox Code Playgroud)
在 API 文档中,您可以获取包目录的DirectoryEntry对象,然后使用 HTML5 FileSystem API 获取文件的内容。API 函数是chrome.runtime.getPackageDirectoryEntry。
chrome.runtime.getPackageDirectoryEntry(function (dirEntry) {
dirEntry.getFile("data.json", undefined, function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader()
reader.addEventListener("load", function (event) {
// data now in reader.result
console.log(reader.result);
});
reader.readAsText(file);
});
}, function (e) {
console.log(e);
});
});
Run Code Online (Sandbox Code Playgroud)