简体   繁体   English

Chrome 扩展程序中未显示另存为对话框

[英]Save As Dialog not showing in Chrome Extension

I'm developing chrome extension for my coworkers and myself.我正在为我的同事和我自己开发 chrome 扩展。 The goal is to download single image by clicking a button at behance.net.目标是通过单击 behance.net 上的按钮下载单个图像。

The problem is that some of my coworkers using "Ask where to save each file before downloading" option, so the code doesn't work for them.问题是我的一些同事使用“在下载前询问每个文件的保存位置”选项,因此代码对他们不起作用。 I don't use this option, so download is successfull for me.我不使用这个选项,所以下载对我来说是成功的。

I've tried to set different options for chrome.downloads.download(), but I got nothing.我尝试为 chrome.downloads.download() 设置不同的选项,但我一无所获。 Save As dialog not showing.另存为对话框未显示。

Manifest显现

{
"name": "Begrab",
"description": "Download pics right from Behance",
"version": "1.0",
"manifest_version": 3,
"background": {
    "service_worker": "background.js"
  },
"permissions": ["activeTab", "downloads","tabs", "background"],

"content_scripts": [
  {
    "matches": ["https://*.behance.net/*"],
    "run_at": "document_end",
    "css": ["content.css"],
    "js": ["content.js"]
  }
]}

content.js内容.js

window.setTimeout(()=> {
    codeRun();
}, 1000)

let projectLinks = document.querySelectorAll('.ProjectCoverNeue-coverLink-102') 

Array.from(projectLinks).forEach(projectLink => {
    projectLink.addEventListener('click', function(){
        window.setTimeout(()=> {
            codeRun();
        }, 4000)
        
    })
})

function codeRun() {
    let imageParents = document.querySelectorAll('.js-project-lightbox-link');

    Array.from(imageParents).forEach(imageParent => {
        let button = document.createElement('a'),
            buttonCopy = document.createElement('a'),
            buttonsWrap = document.createElement('div'),
            img = imageParent.querySelector('img'),
            controls = imageParent.querySelector('.project-module__actions');

            if(img) {
                button.innerText = "Download";
                button.className = "begrab__btn";
                buttonsWrap.className = "begrab__wrap";

                buttonsWrap.appendChild(button);
                imageParent.appendChild(buttonsWrap)

                button.addEventListener('click', (e) => {
                    let param = {src: img.src,
                    fileName: window.location.href};
                    e.stopPropagation()

                    chrome.runtime.sendMessage(param);
                });
            }
    });
}

background.js背景.js

chrome.runtime.onMessage.addListener(
  function(arg, sender, sendResponse) {

  if(arg.src){
    
    chrome.downloads.download({
      url: arg.src,
      conflictAction: "prompt"
      }, function (downloadId) {
        console.log(downloadId);
    });
  }
  
});

Did anyone face the same problem?有没有人面临同样的问题? Hope that somebody would help me.希望有人能帮助我。 Thanks!谢谢!

After some debugging i found out that the user is canceling the download for some reason.经过一些调试后,我发现用户出于某种原因取消了下载。

The onChanged event outputs the following: onChanged事件输出以下内容:

{
    "error": {
        "current": "USER_CANCELED"
    },
    "id": 1,
    "state": {
        "current": "interrupted",
        "previous": "in_progress"
    }
}

The only way i was able to get it work was downgrading the manifest to v2:我能够让它工作的唯一方法是将清单降级到v2:

{
    "manifest_version": 2,
    "background": {
        "persistent": true,
        "scripts": [
            "background.js"
        ]
    }
}

I don't know if this is an option for you.我不知道这是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM