Created
August 28, 2018 22:40
-
-
Save jhyland87/4d29f3774620a2552b52f5d1ce7f8cd5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var PrefixedLog = function( prefix, style ) { | |
if ( ! prefix || typeof prefix !== 'string' ) return; | |
this.prefix = prefix; | |
this.prefixFormat = '[%c%s%c]'; | |
this.style = (typeof style === 'string' ? style : 'color: grey; font-weight: bold'); | |
this.unstyle = 'color: none; font-weight: none'; | |
}; | |
PrefixedLog.prototype.initArgs = function(){ | |
return [ | |
this.prefixFormat, this.style, this.prefix, this.unstyle | |
]; | |
}; | |
PrefixedLog.prototype.doLog = function( type, data ){ | |
if ( typeof type === 'undefined' || typeof data === 'undefined' ) | |
throw new TypeError( 'Invalid console log type value specified - expecting a string, received type '+ typeof type ); | |
if ( typeof type !== 'string' ) | |
throw new TypeError( 'Invalid console log type value specified - expecting a string, received type '+ typeof type ); | |
//if ( ! data.length ) | |
//if ( typeof console[type] !== 'function' ){} | |
var logargs = this.initArgs(); | |
if ( data.length === 1 ){ | |
logargs.push( data[0] ); | |
} | |
else if ( data.length > 1 ) { | |
logargs[0] = logargs[0] + ' ' + data.splice(0, 1); | |
logargs = logargs.concat( data ); | |
} | |
console[type].apply( this, logargs ); | |
}; | |
PrefixedLog.prototype.log = function( ){ | |
return this.doLog( 'log', Array.from( arguments ) ); | |
}; | |
PrefixedLog.prototype.error = function( ){ | |
return this.doLog( 'error', Array.from( arguments ) ); | |
}; | |
PrefixedLog.prototype.warn = function( ){ | |
return this.doLog( 'warn', Array.from( arguments ) ); | |
}; | |
PrefixedLog.prototype.info = function( ){ | |
return this.doLog( 'info', Array.from( arguments ) ); | |
}; | |
PrefixedLog.prototype.debug = function( ){ | |
return this.doLog( 'debug', Array.from( arguments ) ); | |
}; | |
console.__proto__.prefixed = PrefixedLog; | |
var CFG = { | |
sections: [ | |
/* | |
2D-Structure | |
3D-Conformer | |
Names-and-Identifiers | |
Chemical-and-Physical-Properties | |
Related-Records | |
Chemical-Vendors | |
Drug-and-Medication-Information | |
Food-Additives-and-Ingredients | |
Pharmacology-and-Biochemistry | |
Use-and-Manufacturing | |
Identification | |
Safety-and-Hazards | |
Toxicity | |
Literature | |
Patents | |
Biomolecular-Interactions-and-Pathways | |
Biological-Test-Results | |
Classification | |
Information-Sources | |
*/ | |
'2D-Structure', | |
'3D-Conformer', | |
'Names-and-Identifiers', | |
'Chemical-and-Physical-Properties', | |
'Drug-and-Medication-Information', | |
'Pharmacology-and-Biochemistry', | |
'Identification', | |
'Safety-and-Hazards', | |
'Toxicity' | |
], | |
removeableElem: [ | |
'#global-header', // Dary grey banner at the very top of page | |
'#header', // Header with PubChem logo and compound search input | |
'#header2', // Blue banner with download/share/help links | |
'.citation-container', // Container for the "Cite This Record" button | |
'.icon-row-container', // Icon links right below component title | |
'.breadcrumbs-container', // Breadcrumb links right below the component summary/description | |
'.content-container > .toc', // collapsible content list | |
'footer', // Footer container | |
'.helptip-btn', | |
'.icon-fullscreen', | |
'.reference', | |
'.structure-bottom-toolbar', | |
'.structure-zoom-btn', | |
//'.toggle-btn', | |
'.agui-toggle-btn-group', // Buttons in the content header sections (right side) | |
//'.structure-widget > .toolbar' | |
'.toolbar', | |
'.view-container', // | |
'.info-btn', // Little circular info icon next to some elements | |
'.docsum-label-container > .right' // Link to section in PubMed | |
], | |
customCss: { | |
// Make the overall font size a little smaller to fit more content per page | |
'body': { | |
'font-size': '80%' | |
}, | |
// Compound title just a little smaller | |
'.agui-summary-app .summary-title.extra-short': { | |
'font-size': '3em' | |
}, | |
// Padding of main content container | |
'.agui-summary-app': { | |
'padding-top': 'inherit' | |
}, | |
'.agui-summary-app.section':{ | |
'padding': '1em 0 0 0' | |
}, | |
// | |
'.content-list.level-0': { | |
'margin-left':'inherit' | |
}, | |
// Remove the CSS attribute that forces content to the next page (which leaves huge blank spots) | |
'.agui-summary-app .content-list.level-0>li': { | |
'page-break-before': 'inherit' | |
}, | |
// Page title banner | |
'.title-toolbar': { | |
'padding': 'inherit', | |
'padding-bottom': '10px', | |
'margin-bottom': '10px', | |
'border-bottom': 'dotted 2px #8c8c8c' | |
}, | |
// Shrink the padding between sections (to fit more content per page) | |
'.section > .section-content': { | |
'padding-bottom': '1em' | |
}, | |
// Shrink the padding for the sections in level 1 (2nd level) | |
'ol.content-list.level-1 > .section': { | |
'padding': 'inherit' | |
}, | |
'.section-title':{ | |
'color': '#369' | |
}, | |
'ol.content-list.level-0 > li > .section-title': { | |
'border-left': 'solid 5px #7b7b7b', | |
'border-top': 'dotted 1px #7b7b7b', | |
'padding-left': '5px', | |
'margin-bottom': '10px' | |
}, | |
'h1.summary-title': { | |
'color': '#369' | |
} | |
}, | |
elemFn: { | |
} | |
} | |
/* | |
$('ol.content-list.level-0 > li > .section-title').each(function(i,e){ | |
$(e).css({ | |
'color': '#369', | |
'border-left': 'solid 5px #7b7b7b', | |
'border-top': 'dotted 1px #7b7b7b', | |
'padding-left':'5px' | |
}) | |
}) | |
*/ | |
/** | |
* Delay the resolve executed by a promise | |
*/ | |
function delayRes( res ){ | |
var _console = new console.prefixed('delayRes') | |
return function(){ | |
setTimeout(function(){ | |
_console.debug('Done!') | |
res() | |
}, 1000) | |
} | |
} | |
/** | |
* Scroll to the bottom of the page - This is necessary because much of the content | |
* doesn't load until it's scrolled into view. I find that even quickly scrolling to | |
* the bottom of the page, then immediately scrolling back to the top works just fine. | |
*/ | |
function scrollToBottom(){ | |
var _console = new console.prefixed('scrollToBottom') | |
return new Promise(function( res, rej ) { | |
_console.debug( 'Scrolling to bottom of page (%i)...', $(document).height() ) | |
$('html, body').eq(0).animate({ | |
scrollTop: $(document).height() | |
}, 'fast', | |
delayRes( res ) ) | |
}) | |
} | |
/** | |
* Scroll to the top (to recover from scrollToBottom). | |
*/ | |
function scrollToTop( ){ | |
var _console = new console.prefixed('scrollToTop') | |
return new Promise(function( res, rej ) { | |
_console.debug( 'Scrolling to top of page...' ) | |
$('html, body').eq(0).animate({ | |
scrollTop: 0 | |
}, 'fast', | |
delayRes( res ) ) | |
}) | |
} | |
/** | |
* Iterate through the elements specified in the CFG.removeableElem array, deleting | |
* each one. | |
*/ | |
function filterElements(){ | |
var _console = new console.prefixed('filterElements') | |
return new Promise(function( res, rej ) { | |
_console.debug( 'Deleting unwanted elements...' ) | |
CFG.removeableElem.forEach(function( e, idx ){ | |
var $e = $( e ) | |
_console.debug( '[%i/%i] Deleting elements matching selector: %s (found %i)', | |
idx+1, | |
CFG.removeableElem.length, | |
e, | |
$e.length ) | |
if ( $e.length ){ | |
$e.remove() | |
//$e.css('display','none') | |
} | |
}) | |
_console.debug('Done!') | |
res() | |
}) | |
} | |
/** | |
* Manipulate the CSS style of the elements specified in CFG.customCss. | |
*/ | |
function setStyles(){ | |
var _console = new console.prefixed('setStyles') | |
return new Promise(function(res, rej) { | |
_console.debug( 'Setting custom CSS styles...' ) | |
Object.keys( CFG.customCss ).forEach(function( sel, idx ){ | |
_console.debug( '[%i/%i] Setting CSS style for elements matching selector "%s" (found %i) to:', | |
idx+1, Object.keys( CFG.customCss ).length, sel, $( sel ).length, CFG.customCss[sel] ) | |
if ( $( sel ).length ) | |
$( sel ).css( CFG.customCss[sel] ) | |
}) | |
_console.debug('Done!') | |
res() | |
}) | |
} | |
/** | |
* Update the page title to a simplified version of the compounds name | |
*/ | |
function simplifyTitle(){ | |
return new Promise(function(res, rej) { | |
var $nameSpans = $( '.summary-title' ).find( 'span' ) | |
if ( ! $nameSpans.length ) | |
return rej( 'Unable to find the compounds title' ) | |
var nameSegments = $.map( $nameSpans, | |
e => $(e).text().charAt(0).toUpperCase() + $(e).text().substr(1).toLowerCase() ) | |
if ( ! nameSegments.length ) | |
return rej( 'Unable to structure the compounds title' ) | |
document.title = nameSegments.join(' ') | |
res() | |
}) | |
} | |
//$('ol.content-list.level-0 > li').each(function(i,e){ console.log(i,$(e).prop('id')) }) | |
/** | |
* Iterate over the hyperlinks on the page, replacing each one with the text in the hyperlink. | |
*/ | |
function unlinkHyperlinks(){ | |
var _console = new console.prefixed('unlinkHyperlinks') | |
return new Promise(function(res, rej) { | |
_console.debug( 'Altering page hyperlinks...' ) | |
$('a').each(function( i, elem ){ | |
$(elem).replaceWith( $(elem).text() ) | |
}) | |
_console.debug('Done!') | |
res() | |
}) | |
} | |
/** | |
* Iterate over the upper level sections, hiding any that aren't in the settings | |
*/ | |
function filterSections( ){ | |
var _console = new console.prefixed('filterSections') | |
return new Promise(function(res, rej) { | |
_console.debug( 'Filtering sections' ) | |
var sectionId | |
$('ol.content-list.level-0 > li').each(function(i,e){ | |
sectionId = $(e).prop('id') | |
if( CFG.sections.indexOf( sectionId ) === -1 ){ | |
_console.debug( 'Section ID %s not in section list - hiding', sectionId ) | |
//$( e ).css( 'display', 'none' ) | |
$( e ).remove() | |
} | |
else { | |
_console.debug( 'Section %s found in sections list', sectionId ) | |
} | |
}) | |
_console.debug('Done!') | |
res() | |
}) | |
} | |
function finishUp(){ | |
var _console = new console.prefixed('finishUp') | |
return new Promise(function(res, rej) { | |
setTimeout(function(){ | |
window.location.hash = '' | |
_console.log('PubChem PDF friendly page preparation complete!') | |
}, 500) | |
window.location.hash = '' | |
_console.debug('Done!') | |
res() | |
}) | |
} | |
function init(){ | |
var _console = new console.prefixed('init') | |
return new Promise(function(res, rej) { | |
$('<div/>', { | |
'id': 'printerfriendly-modifications', | |
'text': 'Div text', | |
'class': 'className' | |
}).css({ | |
'opacity': '0.5', | |
'background': '#000', | |
'width': '100%', | |
'height': '100%', | |
'z-index': '10', | |
'top': '0', | |
'left': '0', | |
'position': 'fixed' | |
}).prependTo('body') | |
res() | |
}) | |
} | |
function beginPrep(){ | |
var _console = new console.prefixed('beginPrep') | |
return new Promise(function(res, rej) { | |
$('<div/>', { | |
'id' : 'printerfriendly-modifications', | |
'text' : 'Div text', | |
//'class' : 'className' | |
}).css({ | |
//'opacity' : '0.5', | |
'background' : '#ffffff', | |
'width' : '100%', | |
'height' : '100%', | |
'z-index' : '1000000', | |
'top' : '0', | |
'left' : '0', | |
'position' : 'fixed' | |
}).prependTo('body') | |
res() | |
}) | |
} | |
function endPrep(){ | |
var _console = new console.prefixed('endPrep') | |
return new Promise(function(res, rej) { | |
$('#printerfriendly-modifications').remove() | |
setTimeout(function(){ | |
window.location.hash = '' | |
_console.log('PubChem PDF friendly page preparation complete!') | |
}, 500) | |
res() | |
}) | |
} | |
beginPrep() | |
.then( simplifyTitle ) | |
.then( scrollToBottom ) | |
.then( scrollToTop ) | |
.then( filterSections ) | |
.then( filterElements ) | |
.then( setStyles ) | |
.then( unlinkHyperlinks ) | |
.then( simplifyTitle ) | |
.then( endPrep ) | |
.catch( function( err ){ | |
setTimeout(function(){ | |
console.error('ERROR:', err) | |
alert('ERROR: ' + err) | |
}, 500) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment