Skip to content

Instantly share code, notes, and snippets.

@nadav-dav
Last active February 20, 2018 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadav-dav/4665af362fd93aaccfd7916e77536624 to your computer and use it in GitHub Desktop.
Save nadav-dav/4665af362fd93aaccfd7916e77536624 to your computer and use it in GitHub Desktop.
download intercom invoices (print to pdf)
// works on Chrome (need async-await support)
// go to https://app.intercom.io/a/apps/<APP ID>/billing/invoices
// and run this:
(async function(){
const printPdf = function (url, title) {
let iframe;
return new Promise((resolve, reject)=> {
console.log(url, title)
iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.style.display = 'none';
iframe.onload = function() {
var interval = setInterval(function() {
if (iframe.contentDocument.getElementsByClassName("invoice-box").length > 0){
clearInterval(interval);
iframe.focus();
document.title = title;
iframe.contentWindow.print();
iframe.parentElement.removeChild(iframe);
resolve();
}
}, 1000);
};
iframe.src = url;
})
}
const invoices = Array.from($(".invoices-table tr").map((i, tr)=> ({id:tr.children[0].innerText, date:tr.children[1].innerText, url:tr.children[3].children[0] && tr.children[3].children[0].href}))).filter(t=>Boolean(t.url))
for(const invoice of invoices) {
await printPdf(invoice.url, invoice.date+" "+invoice.id);
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment