Advertisement
Guest User

AI0.2

a guest
Dec 14th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Copyright (c) 2019 Fernando Picoral
  3. *
  4. * Auto Importer v0.2
  5. *
  6. * Usage:
  7. *
  8. * Set to 'true' the libraries you want to import on lines 16 to 21
  9. * Link this script to you .html file using <script src='...' type='text/javascript'><script/>
  10. * If needed alter the CDNs URLs and other configs on lines 28 to 53
  11. *
  12. * Default Values:
  13. * - Everything is set to 'true'
  14. * - jQuery 3.4.1
  15. * - Popper 1.16.0
  16. * - BS4 4.4.1
  17. * - FA Pro 5.11.2
  18. * - AOS.init({ once: true });
  19. *
  20. */
  21.  
  22. let Importer = class  {
  23.  
  24.     constructor(config = {
  25.         jquery: true,
  26.         popper: true, //BS4 requirement
  27.         bs4_js: true,
  28.         bs4_css: true,
  29.         fa_pro: true,
  30.         aos: true //AOS Animations
  31.     }) {
  32.  
  33.         this.config = config;
  34.  
  35.         this.cdn = {
  36.             jquery: 'https://code.jquery.com/jquery-3.4.1.slim.min.js',
  37.             popper: 'https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js',
  38.             bs4_js: 'https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js',
  39.             bs4_css: 'https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css',
  40.             fa_pro: 'https://kit-pro.fontawesome.com/releases/v5.11.2/css/pro.min.css',
  41.             aos_css: 'https://unpkg.com/aos@next/dist/aos.css',
  42.             aos_js: 'https://unpkg.com/aos@next/dist/aos.js'
  43.         };
  44.  
  45.         this.integrity = {
  46.             jquery: 'sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n',
  47.             popper: 'sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo',
  48.             bs4_js: 'sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6',
  49.             bs4_css: 'sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh'
  50.         };
  51.  
  52.         this.crossorigin = {
  53.             jquery: 'anonymous',
  54.             popper: 'anonymous',
  55.             bs4_js: 'anonymous',
  56.             bs4_css: 'anonymous'
  57.         };
  58.  
  59.         //DON'T use "double quotes" on string values here, only 'single quotes'
  60.         this.aos_config = {
  61.             once: true
  62.         };
  63.     }
  64.  
  65.     init() {
  66.  
  67.         let config = this.config;
  68.         let cdn = this.cdn;
  69.         let integrity = this.integrity;
  70.         let crossorigin = this.crossorigin;
  71.         let aos_config = this.aos_config;
  72.  
  73.         let head = document.getElementsByTagName('head')[0];
  74.  
  75.         //Repetition needed. Using the same element causes a bug and creates only one of the instances
  76.  
  77.         if (config.jquery) {
  78.             let comment = document.createComment('');
  79.             let script = document.createElement('script');
  80.             script.type = 'text/javascript';
  81.             script.async = true;
  82.  
  83.             script.src = cdn.jquery;
  84.             script.integrity = integrity.jquery;
  85.             script.crossOrigin = crossorigin.jquery;
  86.             comment.data = "jQuery";
  87.             head.appendChild(comment);
  88.             head.appendChild(script);
  89.         }
  90.         if (config.popper) {
  91.             let comment = document.createComment('');
  92.             let script = document.createElement('script');
  93.             script.type = 'text/javascript';
  94.             script.async = true;
  95.  
  96.             script.src = cdn.popper;
  97.             script.integrity = integrity.popper;
  98.             script.crossOrigin = crossorigin.popper;
  99.             comment.data = "Popper";
  100.             head.appendChild(comment);
  101.             head.appendChild(script);
  102.         }
  103.         if (config.bs4_js) {
  104.             let comment = document.createComment('');
  105.             let script = document.createElement('script');
  106.             script.type = 'text/javascript';
  107.             script.async = true;
  108.  
  109.             script.src = cdn.bs4_js;
  110.             script.integrity = integrity.bs4_js;
  111.             script.crossOrigin = crossorigin.bs4_js;
  112.             comment.data = "Bootstrap 4 JS";
  113.             head.appendChild(comment);
  114.             head.appendChild(script);
  115.         }
  116.         if (config.bs4_css) {
  117.             let comment = document.createComment('');
  118.             let link = document.createElement('link');
  119.             link.rel = 'stylesheet';
  120.             link.type = 'text/css';
  121.  
  122.             link.href = cdn.bs4_css;
  123.             link.integrity = integrity.bs4_css;
  124.             link.crossOrigin = crossorigin.bs4_css;
  125.             comment.data = "Bootstrap 4 CSS";
  126.             head.appendChild(comment);
  127.             head.appendChild(link);
  128.         }
  129.         if (config.fa_pro) {
  130.             let comment = document.createComment('');
  131.             let link = document.createElement('link');
  132.             link.rel = 'stylesheet';
  133.             link.type = 'text/css';
  134.  
  135.             link.href = cdn.fa_pro;
  136.             comment.data = "FontAwesome Pro 4 CSS";
  137.             head.appendChild(comment);
  138.             head.appendChild(link);
  139.         } if (config.aos) {
  140.             let comment = document.createComment('');
  141.             //JS
  142.             let script = document.createElement('script');
  143.             script.type = 'text/javascript';
  144.             script.async = true;
  145.  
  146.             script.src = cdn.aos_js;
  147.             comment.data = "AOS Animations";
  148.             head.appendChild(comment);
  149.             head.appendChild(script);
  150.  
  151.             //CSS
  152.             let link = document.createElement('link');
  153.             link.rel = 'stylesheet';
  154.             link.type = 'text/css';
  155.  
  156.             link.href = cdn.aos_css;
  157.             head.appendChild(link);
  158.  
  159.             //Init
  160.             let aos_init = document.createElement('script');
  161.             aos_init.type = 'text/javascript';
  162.             aos_init.text = "window.onload = function() { AOS.init(" + JSON.stringify(aos_config).replace(/['"]+/g, '') + "); };";
  163.             head.appendChild(aos_init);
  164.  
  165.         }
  166.     }
  167. };
  168.  
  169. //Self Init
  170. (function(){
  171.     let head = document.getElementsByTagName('head')[0];
  172.     let comment = document.createComment('');
  173.     let script = document.createElement('script');
  174.     script.type = 'text/javascript';
  175.     script.text = "let I = new Importer();I.init();";
  176.     comment.data = "Importer Init";
  177.     head.appendChild(comment);
  178.     head.appendChild(script);
  179. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement