1

I found this selector:

document.querySelector('#print-header > div > button.print.default').click();

Is there a method with this selector to print directly from Chrome?

I tried with timeout too, but isn't working.

Davide Cavallini
  • 216
  • 4
  • 15
  • It seems that by 'directly', you mean without confirmation. – Dan D. Oct 18 '15 at 10:39
  • yes. i tried with --kiosk-printing mode, but don't work – Davide Cavallini Oct 18 '15 at 10:40
  • 2
    That is just a selector for a button. You haven't shown us if there are any event listeners (for click, that is) bound to that element that will trigger printing. Otherwise you can simply use `window.print()`. – Terry Oct 18 '15 at 10:40
  • Wait! "Yes, [without confirmation]". This shouldn't be possible. Just think about pages printing what ever they want using visitor's printer ... – Teemu Oct 18 '15 at 10:46
  • Yes, but i need it for a special function. – Davide Cavallini Oct 18 '15 at 10:49
  • If any web page I visit will just print something with my printer without asking me first, I'll surely navigate away, and will never come back + report it as a malicious page too. – Teemu Oct 18 '15 at 10:51

2 Answers2

1

You cannot print without user confirmation. It's a security thing, you are not allowed to control user PC from the browser.

When you open the print dialog in chrome it's like a new Tab, you have no control there.

Tiberiu Popescu
  • 4,486
  • 2
  • 26
  • 38
0

(This solution needs user's confirmation)

Use window.print(); function or see this answer : Bill Paetzke's answser of Print the contents of a DIV.

function PrintElem(elem)
{
    Popup($(elem).html());
}

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>my div</title>');
    /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');

    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10

    mywindow.print();
    mywindow.close();

    return true;
}
Community
  • 1
  • 1
user2226755
  • 12,494
  • 5
  • 50
  • 73