Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                // D'ontreye Nero (d@neroscript.io) - 2019
const NsPrettyPrintPage = {
  print: function () {
    
  // create a hidden iframe named PrettyPrintFrame
  const prettyPrintIframe = document.createElement('iframe');
  
  prettyPrintIframe.setAttribute('id', 'PrettyPrintFrame');
  prettyPrintIframe.setAttribute('name', 'PrettyPrintFrame');
  prettyPrintIframe.setAttribute('style', 'display: none;');
    
	// add newly created iframe to the current DOM
	document.body.appendChild(prettyPrintIframe);

  // add generated header content
  prettyPrintIframe.contentWindow.document.head.innerHTML = this.
  generateHeaderHtml();

	// add generated body content
  prettyPrintIframe.contentWindow.document.body.innerHTML = this.generatePrintLayout();

    try {
        // reference to iframe window
        const contentWindow = prettyPrintIframe.contentWindow;
        
        // execute iframe print command
        const result = contentWindow.document.execCommand('print', false, null);
        
        // iframe print listener
        const printListener = contentWindow.matchMedia('print');
        printListener.addListener(function(pl) {
          if (!pl.matches) {
            // remove the hidden iframe from the DOM
            // prettyPrintIframe.remove();
          }
        });
        
        // if execCommand is unsupported
        if(!result) { contentWindow.print(); }

      } catch (e) {
        // print fallback
        window.frames['PrettyPrintFrame'].focus();
    	window.frames['PrettyPrintFrame'].print();
      }
  },
  generatePrintLayout: function () {
  	// this function houses your default header/footer details and the switch to identify your pages by
  	
    let html = '';

    // add logo
    html += '<h3 style="margin-bottom: 16px;">NeroScript.io Pretty Printed Page Header</h3>';
	
    /*
    // this switch should be based on whatever unique identifier your pages go by
    // add a new case statement for each new page type
    switch (location.pathname) {
        case '/samplePage1':
          html += this.prettyPrintCodeForSamplePageType1();
        break;

        case "/samplePage2":
          html += this.prettyPrintCodeForSamplePageType2();
        break;
      }
    */
	
	html += this.amazonSearchResults();
	
	// global footer elements
	html += this.generateFooterHtml();
	
	return html;
  },
  generateHeaderHtml: function () {
    let	headerHtml = '';

    // loop through the styleSheets object and pull in all styles
    for (let i = 0; i < document.styleSheets.length; i++) {
      headerHtml += '<style>';
      
      try {
        for (let j = 0; j < document.styleSheets[i].cssRules.length; j++) {
          headerHtml += document.styleSheets[i].cssRules[j].cssText || '';
        }
      } catch(e) {}
      
      headerHtml += '</style>';
    }

    headerHtml += this.generateGlobalCss();

    return headerHtml;
  },
  generateGlobalCss: function () {
  	// add any global css you want to apply to all pretty print pages
    let css = '<style>';

    // global css
    css += 'body { padding: 40px 24px; }';
    css += 'table tr { page-break-inside: avoid; }';
    css += 'table td { vertical-align: top; padding: 4px 8px;}';
    css += 'img { height: 100px !important; width: 100px !important; }';
    css += '@page { margin: 0.8cm; }';
    
    css += '</style>';
    
    return css;
  },
  generateFooterHtml: function () {
  	let footerHtml = '<hr style="margin: 24px 0 8px;">';
  	
  	footerHtml += 'Global footer text - NeroScript.io ' + ( new Date().getFullYear() );
  	footerHtml += '</body></html>';
  	
    return footerHtml;
  },
  amazonSearchResults: function() {
    let html = '';

    // reference to all result items that are not ads
    let resultItems = document.querySelectorAll('#s-results-list-atf li.s-result-item:not(.AdHolder):not(.acs-private-brands-container-background)');
    
    html += '<table>';
    
    // iterate over result items
    resultItems.forEach(function(item) {
    	html += '<tr>';
    	
    	// product image 
    	html += '<td>' + item.querySelector('.s-access-image').outerHTML + '</td>';
    	
    	// product title
    	html += '<td style="max-width: 400px;">' + item.querySelector('.s-access-title').innerHTML + '</td>';
    	
    	// product price
    	html += '<td>' + item.querySelector('.sx-price-currency').innerHTML;
    	html += item.querySelector('.sx-price-whole').innerHTML + '.';
    	html += item.querySelector('.sx-price-fractional').innerHTML + '</td>';
    	
    	// product rating
    	html += '<td style="min-width: 150px;">' + item.querySelector('.a-spacing-mini .a-icon-alt').innerHTML + '</td>';
    	
    	html += '</tr>';
    });
    
    html += '</table>';
    
    return html;
  },
  prettyPrintCodeForSamplePageType2: function() {
    let html = '';

    // ... more extracted html
    return html;
  }
};

// override Ctrl/Cmd + P
document.addEventListener("keydown", function (event) {
  if((event.ctrlKey || event.metaKey) && event.key == "p") {
        NsPrettyPrintPage.print();
        event.preventDefault();
        return false;
    }
} , false);
              
            
!
999px

Console