Declare my.extension.js
as event page or background page. (Content Scripts will not work with the following method since the chrome.* API provided in content scripts is limited)
Use chrome.runtime.getPackageDirectoryEntry
, as suggested by Xan, which is based on HTML 5 Filesystem API to access the resource files and thus you can check for the file existence therein.
manifest.json:
{
"background": {
"scripts": ["my.extension.js"],
"persistent": false
},
"manifest_version": 2,
"name": "Check Resource Existence",
"version": "1.0",
}
my.extension.js:
filename = "image.png";
chrome.runtime.getPackageDirectoryEntry(function(storageRootEntry) {
fileExists(storageRootEntry, filename, function(isExist) {
if(isExist) {
/* your code here */
}
});
});
function fileExists(storageRootEntry, fileName, callback) {
storageRootEntry.getFile(fileName, {
create: false
}, function() {
callback(true);
}, function() {
callback(false);
});
}