0

I am developing a small chrome extension to help my grandpa print images easily through a button in the context menu. I got to the point that the image opens on a seperate tab, after debugging I found out that "window.print()" got triggered but nothing happend.

My background.js file:

var printEnable = false;

chrome.contextMenus.create({
    "title": "Print",
    "type": "normal",
    "contexts": ["image"],
    "onclick": function(info) {
        chrome.tabs.create({url: info.srcUrl});
        this.printEnable = true;
    }
});

chrome.tabs.onUpdated.addListener(function(){
    if(printEnable){
        window.print();
        this.printEnable = false;
    }
});

My manifest.json

{
  "name": "Print",
  "description": "Easy picture print",
  "permissions": [ "contextMenus","tabs"],
  "version": "0.1",
  "manifest_version": 2,
  "background": {
      "scripts": ["background.js"]
  },  
  "icons": { "16": "calculator-16.png", "128": "calculator-128.png" }
}

Can someone please help me find whats wrong with my code?

StefRommes
  • 11
  • 3
  • It seems the second method is not invoking.chrome.tabs.onUpdated.addListener – Jijo Cleetus Dec 10 '17 at 19:57
  • You are not passing anything to the callback function in the `background.js`? You are referencing the backgrounds window instead of the required one when you are calling `window.print();`. Nevertheless invoking the print dialog from a popup or a background page is impossible. – Luka Čelebić Dec 10 '17 at 20:06
  • Xan gave a simple example regarding this problem in [this](https://stackoverflow.com/a/24193885/6586663) answer. I do believe tho that it can be done much simpler from a content script. Perhaps try using a function from my answer regarding [this](https://stackoverflow.com/a/47728358/6586663) question to select and print the required image. – Luka Čelebić Dec 10 '17 at 20:09

0 Answers0