LoadScript

一个用于按顺序加载外部js的脚本的库

Tento skript by nemal byť nainštalovaný priamo. Je to knižnica pre ďalšie skripty, ktorú by mali používať cez meta príkaz // @require https://update.greasyfork.org/scripts/10108/54092/LoadScript.js

// ==UserScript==
// @name        LoadScript
// @namespace   brambles
// @version     1.2
// @grant       none
// ==/UserScript==
  var LoadScript = function LoadScript(urls, callback) {
    var _loadScript = arguments.callee;
    var head = document.head;
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.async = 'true';
    script.className = 'loadscript';
    script.setAttribute('charset', 'utf-8');

    if (typeof urls === 'string') {
      var urls = [
        urls
      ];
    }
    if (!Array.isArray(urls)) {
      throw 'type error';
    }
    if (urls.length > 1) {
      script.onload = script.onerror = function () {
        script.onload = script.onerror = null;
        _loadScript(urls.slice(1), callback);
        
      };
    } 
    else {
      script.onload = script.onerror = function(){
          script.onload = script.onerror = null;
          callback();
      };
    }
    script.src = urls[0];
    head.insertBefore(script, head.firstChild);
  };