下载不会使用javascript下载

时间:2018-07-08 15:03:02

标签: javascript html json

所以,我有这段代码,下载不会下载此变量。这是代码:

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <title>Extension Maker</title>
    </head>
    <body>
    <form>
    <h1>Extension maker for chrome</h1>
    <input id="name" size="22" placeholder="Extension name">
    <br>
    <input id="vers" size="22" placeholder="Version">
    <br>
    <textarea id="desc" placeholder="Description"></textarea>
    <br><br><br>
    <input type="checkbox" id="popup">Popup <input id="popup-html" placeholder="Popup html">
    <br>
    <input type="checkbox" id="urlo">Change contents of single page <input id="page-cont" placeholder="Page HTML">
    <select id="select">
    <option value="newtab">New Tab Page</option>
    <option value="bookmarks">Bookmarks Page</option>
    <option value="history">History Page</option>
    </select>
    <br>
    <input type="checkbox" id="js">Run JavaScript <input id="js" placeholder="JS">
    <br><br><br>
    <button onclick="downloadAll()" id="downloadbutton">Download</button>
    </form>
    <script>
    
        
        function download(filename, text) {
        alert("download script working");
        var element = document.createElement('a');
        element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
        element.setAttribute('download', filename);
    
        element.style.display = 'none';
        document.body.appendChild(element);
    
        element.click();
    
        document.body.removeChild(element);
    };
        function downloadAll() {
        
    	alert("downloadall works!");
        var first = document.getElementById("popup").checked;
        var second = document.getElementById("urlo").checked;
        var third = document.getElementById("js").checked;
        var name = document.getElementById("name").value;
        var vers = document.getElementById("vers").value;
        var desc = document.getElementById("desc").value;
        var pophtml = document.getElementById("popup-html").value;
        var manifeststart = "{     'name': " + name + ",     'version': " + vers + ",     'description': " + desc + ",               'manifest_version': 2   ";
        if(first === true){
        var manifest = manifeststart + "'default_popup': 'popup.html',       'default_icon': {         '16': 'images/get_started16.png'       }     },}";
        var text = document.getElementById("popup-html").value;
        var filename = "popup.html";
        
        download(filename, text);
        };
        if(second === true){
        var select = var text = document.getElementById("select").value;
        var manifest = manifest + "'chrome_url_overrides' : { ' " + select + "': 'myPage.html' }";
        var text = document.getElementById("page-cont").value;
        var filename = "myPage.html";
        
        download(filename, text);
        };
        if(third === true){
        var manifest = manifest + "'background': {'scripts': ['background.js'],'persistent': false },";
        var text = document.getElementById("js").value;
        var filename = "background.js";
        
        download(filename, text);
        };
        
        var text = manifest;
        var filename = "manifest.json";
        
        download(filename, text);
    };
        </script>
</body>
</html>
它根据复选框添加json。它只是不想下载manifest.json。另外,当下载json时,json是否有效?请帮忙。并在投票不足之前评论出了什么问题。谢谢!

0 个答案:

没有答案