javascript - 未捕获的类型错误 : Cannot read property 'chooseEntry' of undefined (while developing a chrome extension using fileSystem API)

标签 javascript html google-chrome google-chrome-extension google-chrome-devtools

我正在使用 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/

相关文章:

javascript - 如何防止在 chrome 上向上滚动 href ="#"

jquery - 在滚动期间添加一个类

google-chrome - Firefox 中是否有相当于 Google Chrome 的 allow-insecure-localhost 标志?

javascript - 图表数据缩放无法读取未定义的属性 'length'

javascript - moment.js isBetween() 方法使用澳大利亚日期格式

javascript - 在 Angular JS 中选择 - 我需要元素和值属性相同

css - 持续显示的 HTML5 输入占位符(即使在输入时?)

javascript - 为什么 Firefox 在页面加载时触发 mouseenter 事件?

javascript - 在 Chrome 中对对象数组进行排序

javascript - 在表单提交上运行 JavaScript 函数