Last active
April 13, 2021 14:07
-
-
Save samuelcolvin/b0b77df49c192d6855bac5e1a1828c6a to your computer and use it in GitHub Desktop.
strict to run for the console to speedup and smarten up exporting github issues to PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const style = document.createElement('style') | |
style.innerHTML = '@media print {.no-print, .no-print *{display: none !important;}}' | |
document.querySelector('head').appendChild(style) | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
async function pending_images() { | |
const pending_images = Array.from(document.images).filter(img => !img.complete) | |
if (pending_images.length) { | |
console.log(`${issue_id} - waiting for ${pending_images.length} to load`) | |
await Promise.all( | |
pending_images | |
.map(img => new Promise(resolve => { | |
img.onload = img.onerror = resolve | |
})) | |
) | |
} | |
} | |
async function wait_for_element(selector, page) { | |
console.log(`waiting for "${selector}" on ${page}`) | |
while (true) { | |
const element = document.getElementById(selector) | |
if (element) { | |
return element | |
} | |
await sleep(100) | |
} | |
} | |
async function save(issue_id) { | |
const side_bar = await wait_for_element('partial-discussion-sidebar', issue_id) | |
side_bar.classList.add('no-print') | |
await pending_images() | |
document.querySelector('.js-repo-nav').classList.add('no-print') | |
document.querySelector('.gh-header-actions').classList.add('no-print') | |
document.title = location.pathname.substr(1).replace('/issues/', '#') | |
console.log('printing issue', document.title) | |
window.print() | |
} | |
const issue_ids = Array.from(document.querySelectorAll('a[id^="issue_"]')).map(el => el.id) | |
console.log('getting issues:', issue_ids) | |
for (const issue_id of issue_ids) { | |
const issue = await wait_for_element(issue_id, 'issue list') | |
issue.click() | |
await save(issue_id) | |
history.back() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment