小编Arv*_*ala的帖子

通过Chrome扩展程序将文件上传为表单数据

我通过chrome扩展程序上传文件作为表单数据,我的代码如下所示.这里的问题是文件浏览窗口打开一秒钟然后消失.
该问题仅出现在Mac OS中.

manifest.json的:

"background": {
  "scripts": ["jszip.js", "background.js"]
},
Run Code Online (Sandbox Code Playgroud)

background.js:

chrome.runtime.onMessage.addListener(function (msg) {
  if (msg.action === 'browse')
  {
    var myForm=document.createElement("FORM");
    var myFile=document.createElement("INPUT");
    myFile.type="file";
    myFile.id="selectFile";
    //myFile.onclick="openDialog()";
    myForm.appendChild(myFile);
    var myButton=document.createElement("INPUT");
    myButton.name="submit";
    myButton.type="submit";
    myButton.value="Submit";
    myForm.appendChild(myButton);
    document.body.appendChild(myForm);
  }
});
Run Code Online (Sandbox Code Playgroud)

popup.js:

window.onload = function () {
  chrome.runtime.sendMessage({
    action: 'browse'
  });
}
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome google-chrome-extension

28
推荐指数
2
解决办法
1万
查看次数

如何使用JavaScript设置chrome扩展的文件下载位置?

嗨我正在使用chrome扩展名下载所选链接,但我无法设置下载位置.所有网址都下载到chrome的默认位置.我知道由于安全原因我们不能这样做.我们可以从这里提示Chrome扩展弹出窗口中的目录选择器对话框用户可以选择下载路径.需要我方的任何信息让我知道.

这有可能吗?有关如何去做的任何建议?

在此先感谢我的代码

function downloadFile(url, onSuccess,arrayOfUrl,zip) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = "blob";
    xhr.onreadystatechange = function () {

        if (xhr.readyState == 4) {
            if (onSuccess)
            {
            onDownloadComplete(xhr.response, arrayOfUrl,zip)
             }
}
}
xhr.send("null");
}
function onDownloadComplete(blobData,urls,zip ){
    if (count < urls.length) {
        blobToBase64(blobData, function(binaryData){
                var fileName = urls[count].substring(urls[count].lastIndexOf('/')+1);
                 zip.file(fileName+".docx", binaryData, {base64: true}); 
                if (count < urls.length -1){
                    count++;
                    downloadFile(urls[count], onDownloadComplete, urls,zip);

                }
                else {

                    var content = zip.generate();

                     var zipName = 'download.zip';
                var a = document.createElement('a'); …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome google-chrome-extension

5
推荐指数
1
解决办法
9865
查看次数