Skip to content

Instantly share code, notes, and snippets.

@martinhj
Last active December 3, 2019 08:18
Show Gist options
  • Save martinhj/b53d735e94c1c54f757b7dff137e7fb9 to your computer and use it in GitHub Desktop.
Save martinhj/b53d735e94c1c54f757b7dff137e7fb9 to your computer and use it in GitHub Desktop.
Clean up github code for printing (including main view for readme print)
(() => {
const flattenArray = (acc, cur) => acc = [...acc, ...cur]
const queryElement = selector => document.querySelector(selector)
const getAllChildren = elem => elem.children
const removeElem = elem => elem && elem.parentElement.removeChild(elem);
const removeClassFromElem = (elem, styleclass) => elem && elem.classList.remove(styleclass);
const removeClassesFromElem = ([elem, styleclasses]) => styleclasses.forEach(styleclass => removeClassFromElem(elem, styleclass));
const findAllChildrensExcept = (elemToRemoveExcept) => elemToRemoveExcept
.map(([elem, keepChildrenQueries]) => [queryElement(elem), keepChildrenQueries.map(queryElement)])
.map(([elem, keepChildrenElems]) => [Array.from(getAllChildren(elem)), keepChildrenElems])
.map(([childrenElems, keepChildrenElems]) => childrenElems.filter(elem => !keepChildrenElems.includes(elem)))
.reduce(flattenArray, [])
const elemsToBeRemovedSelectors = [
'body > div.application-main > div > main > div.container-lg.clearfix.new-discussion-timeline.experiment-repo-nav.p-responsive > div > div.Box.Box--condensed.d-flex.flex-column.flex-shrink-0',
'body > div.application-main > div > main > div.pagehead.repohead.instapaper_ignore.readability-menu.experiment-repo-nav.pt-0.pt-lg-4',
'body > div.footer.container-lg.width-full.p-responsive',
'body > div.position-relative.js-header-wrapper',
'body > div.application-main > div > main > div > div > div > div.Box-header.py-2.d-flex.flex-column.flex-shrink-0.flex-md-row.flex-md-items-center',
'body > div.application-main > div > main > div > div > div.d-flex.flex-items-start.flex-shrink-0.pb-3.flex-column.flex-md-row > span',
'body > div.application-main > div > main > div > div > div.d-flex.flex-items-start.flex-shrink-0.pb-3.flex-column.flex-md-row > div',
'body > div.footer.container-lg.width-full.px-3',
'#js-repo-pjax-container > div.pagehead.repohead.instapaper_ignore.readability-menu.experiment-repo-nav'
];
const elemsToRemoveAllChildrenElemsExcept = [
[
'#js-repo-pjax-container > div.container-lg.clearfix.new-discussion-timeline.experiment-repo-nav.px-3 > div',
['#readme']
],
];
const elemSelectorsWithClassesToBeRemoved = [
[
'body > div.application-main > div > main > div > div > div.d-flex.flex-items-start.flex-shrink-0.pb-3.flex-column.flex-md-row',
['pb-3']
]
];
elemsToBeRemovedSelectors
.map(queryElement)
.forEach(removeElem);
elemSelectorsWithClassesToBeRemoved
.map(([selector, styleClasses]) => ([queryElement(selector), styleClasses]))
.forEach(removeClassesFromElem);
removeAllChildrenExcept(elemsToRemoveAllChildrenElemsExcept)
.forEach(removeElem)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment