jroakes

howtoSchema

Sep 1st, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Define the class for your howto object here
  2. // They should be marked up in a consistent manner.
  3.  
  4. var parentClass         = 'howto_block'
  5. var imageClass          = 'howto_image';
  6. var nameClass           = 'howto_name';
  7. var descriptionClass    = 'howto_description';
  8. var stepClass           = 'howto_step';
  9.  
  10. // Output schema to console. Set to `false` if adding via tag manager.
  11. var logOutput     = true;
  12.  
  13. (function(){
  14.  
  15.     var parent = document.querySelector('.' + parentClass.replace('.', ''))
  16.  
  17.     // Build Data
  18.     const howto_image   = parent.querySelector('.' + imageClass.replace('.', '')).href;
  19.     const howto_name    = parent.querySelector('.' + nameClass.replace('.', '')).textContent;
  20.     const howto_desc    = parent.querySelector('.' + descriptionClass.replace('.', '')).textContent;
  21.     const howto_steps   = parent.querySelectorAll('.' + stepClass.replace('.', ''));
  22.  
  23.     if (howto_steps.length && typeof howto_name !== 'undefined'){
  24.  
  25.         var data = {
  26.           "@context": "https://schema.org",
  27.           "@type": "HowTo",
  28.           "name": howto_name,
  29.           "step": []
  30.         };
  31.        
  32.         var img_data = {
  33.             "@type": "ImageObject",
  34.             "url": null
  35.         };
  36.  
  37.    
  38.         buildStep = (s, i) => {
  39.  
  40.             var item = {
  41.                           "@type": "HowToStep",
  42.                           "name": null,
  43.                           "url": null
  44.                         };
  45.  
  46.             item['name'] = s.textContent;
  47.             const step_img = s.querySelector('img').href;
  48.             if (typeof step_img !== 'undefined') {
  49.                 item['image'] = step_img;
  50.             }
  51.             item['url'] = window.location.href.split('#')[0] + "#step" + (i+1);
  52.  
  53.             return item;
  54.         }
  55.        
  56.         if (typeof howto_image !== 'undefined') {
  57.             img_data['url'] = howto_image;
  58.             data['image'] = img_data;
  59.         }
  60.  
  61.         if (typeof howto_desc !== 'undefined') {
  62.             data['description'] = howto_desc;
  63.         }
  64.        
  65.         data['step'] = howto_steps.map(function(s, i){return buildStep(s, i)});
  66.  
  67.         var script = document.createElement('script');
  68.         script.type = "application/ld+json";
  69.         script.innerHTML = JSON.stringify(data);
  70.         document.getElementsByTagName('head')[0].appendChild(script);
  71.  
  72.         if (logOutput){
  73.             console.log(script.outerHTML);
  74.         }
  75.  
  76.     }else{
  77.    
  78.         console.error('Howto schema could not be generated. Steps or name not defined.')
  79.    
  80.     }
  81.  
  82. })(document);
Add Comment
Please, Sign In to add comment