简体   繁体   中英

Save As Dialog not showing in Chrome Extension

I'm developing chrome extension for my coworkers and myself. The goal is to download single image by clicking a button at 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. 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

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

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:

{
    "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:

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

I don't know if this is an option for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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