我正在使用 chrome 的文件系统 API 开发一个 chrome 扩展程序,它需要以 .txt 格式保存文件。我有以下代码-
list .json -
{
"version": "1.0",
"manifest_version": 2,
"permissions": [{"fileSystem": ["write", "retainEntries", "directory"]}],
"background": {
"scripts": ["js/background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["file:///index.html"],
"css": ["css/style.css"],
"js": ["js/app.js"]
}
]
}
应用程序.js -
(function () {
var text = document.getElementById("content").innerHTML;
var saveAsButton = document.getElementById("saveas");
function errorHandler() {
return null;
}
saveAsButton.addEventListener('click', function(e) {
chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) {
writableFileEntry.createWriter(function(writer) {
writer.onerror = errorHandler;
writer.onwriteend = function(e) {
console.log('write complete');
};
writer.write(new Blob([text], {type: 'text/plain'}));
}, errorHandler);
});
});
})();
index.html -
<html>
<head>
<title>note-in-chrome</title>
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<button id="saveas">SAVE AS</button>
<div id="content" contenteditable="true" data-text="Enter text here"></div>
<script src="js/app.js"></script>
</body>
</html>
每当我单击“另存为”按钮时,都会打开一个选择文件的提示,但我收到以下错误 - Uncaught TypeError: Cannot read property 'chooseEntry' of undefined
。
最佳答案
chrome.fileSystem
API 可用于 Chrome apps , 但不是 Chrome extensions .
关于javascript - 未捕获的类型错误 : Cannot read property 'chooseEntry' of undefined (while developing a chrome extension using fileSystem API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50137315/