LoadScript

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

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.greatest.deepsurf.us/scripts/10108/54092/LoadScript.js

  1. // ==UserScript==
  2. // @name LoadScript
  3. // @namespace brambles
  4. // @version 1.2
  5. // @grant none
  6. // ==/UserScript==
  7. var LoadScript = function LoadScript(urls, callback) {
  8. var _loadScript = arguments.callee;
  9. var head = document.head;
  10. var script = document.createElement('script');
  11. script.type = 'text/javascript';
  12. script.async = 'true';
  13. script.className = 'loadscript';
  14. script.setAttribute('charset', 'utf-8');
  15.  
  16. if (typeof urls === 'string') {
  17. var urls = [
  18. urls
  19. ];
  20. }
  21. if (!Array.isArray(urls)) {
  22. throw 'type error';
  23. }
  24. if (urls.length > 1) {
  25. script.onload = script.onerror = function () {
  26. script.onload = script.onerror = null;
  27. _loadScript(urls.slice(1), callback);
  28. };
  29. }
  30. else {
  31. script.onload = script.onerror = function(){
  32. script.onload = script.onerror = null;
  33. callback();
  34. };
  35. }
  36. script.src = urls[0];
  37. head.insertBefore(script, head.firstChild);
  38. };