Regular screenshots with chrome.tabs.captureVisibleTab

Viewed 64

I'm gaining experience in javascript and trying to make extension to Chrome that makes regular screenshots of current active tab.

I finded working well code for one shot. Modification with setInterval function not works. Alert window is shown and then two errors occur:

Unchecked runtime.lastError: Cannot access contents of url "devtools://devtools/". Extension manifest must request permission to access this host.

and

Error handling response: TypeError: Error in invocation of downloads.download(downloads.DownloadOptions options, optional function callback): Error at parameter 'options': Missing required property 'url'.

The code of background.js is so

(function(){

let add_symbol = (str, symbol, len, side)=>{
    ...
    return str;
};

let get_curdate = ()=>{
    ...
    return `${y}-${m}-${d} ${H}-${M}-${S}.png`;
};

let get_filename = ()=>'Screenshot '+get_curdate();

let take_screenshot = ()=>{
    alert("Auto Screenshoter enabled!");    
    setInterval(() => chrome.tabs.captureVisibleTab(null, {format: 'png'}, img=>{               
                    chrome.downloads.download({url: img, filename: get_filename(), saveAs: true});      
                }), 60000)
    console.log("Done!");

        /* chrome.tabs.captureVisibleTab(null, {format: 'png'}, img=>{              
                    chrome.downloads.download({url: img, filename: get_filename(), saveAs: true});      
            }); */   //this works for one screenshot
        
}; 

let run = ()=>{
    chrome.browserAction.onClicked.addListener(take_screenshot);
};

run();

}());

And here is the content of the manifest.json file:

    "manifest_version": 2,
    "name": "Auto Screenshots", 
    "default_locale": "en",
    "description": "__Auto_Screenshoter__",
    "version": "1.111", 

   "background": {
      "page": "html/background.html"
   },
   "permissions": [
        "<all_urls>",
        "downloads", 
        "activeTab", 
        "tabs"
        ],
   "browser_action": {
      "default_icon": {
         "19": "images/icon-19.png",
         "38": "images/icon-38.png"
      },
      "default_title": "__Auto_Screenshot__"
   },   
   "icons": {
      "128": "images/icon-128.png",
      "16": "images/icon-16.png",
      "48": "images/icon-48.png"
   }
}

Why is the parameter 'url' being lost? How to decompose nested anonymous functions? And how to fix the first error? Thank you to everyone who answered.

0 Answers
Related