PxerLauncher.class.js

Pxer的启动器类文件@稳定版

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.sleazyfork.org/scripts/23946/152160/PxerLauncherclassjs.js

  1. 'use strict';
  2.  
  3. class PxerLauncher{
  4. constructor(options){
  5. Object.assign(this ,{
  6. sync:[],
  7. asyn:[],
  8. onsync:()=>console.log('Sync list is OK!'),
  9. onasyn:()=>{},
  10. onerror:err=>console.error(err),
  11. cache:true,
  12. } ,options)
  13. };
  14. };
  15.  
  16.  
  17. PxerLauncher.prototype['load'] =function(){
  18. this.loadSync();
  19. this.loadAsyn();
  20. };
  21.  
  22.  
  23. PxerLauncher.prototype['createElt'] =function(path){
  24. var extName =path.replace('.php','').match(/\.([A-Za-z0-9]*?)(?:\?.+)?$/)[1];
  25. let tagName =this.tagMap[extName].tag;
  26. var elt =document.createElement(tagName);
  27.  
  28. //避免缓存(new Date()).getTime()
  29. if(!this.cache) path =path.indexOf('?')==-1 ?path+'?'+(new Date()).getTime() :path+'&'+(new Date()).getTime();
  30.  
  31. let attr =this.tagMap[extName].attr;
  32. for(let key in attr){
  33. if(attr[key] ==='%PATH%'){
  34. elt.setAttribute(key ,path);
  35. continue;
  36. };
  37. elt.setAttribute(key ,attr[key])
  38. };
  39.  
  40. return elt;
  41.  
  42. };
  43.  
  44.  
  45. PxerLauncher.prototype['loadAsyn'] =function(){
  46. this.asyn.forEach(function (item){
  47. let elt =this.createElt(item);
  48. elt.async =true;
  49. document.head.appendChild(elt);
  50. } ,this);
  51. };
  52. PxerLauncher.prototype['loadSync'] =function(){
  53. var pms =Promise.resolve();
  54. this.sync.forEach(function (item){
  55. pms =pms.then(()=>{
  56. return new Promise((resolve ,reject)=>{
  57. let elt =this.createElt(item);
  58. elt.addEventListener('load' ,resolve);
  59. elt.addEventListener('error' ,reject.bind(null ,elt));
  60. document.head.appendChild(elt);
  61. });
  62. });
  63. } ,this);
  64.  
  65. pms.then(this.onsync);
  66. pms.catch((err)=>{
  67. console.error('PxerLauncher load file error!');
  68. this.onerror(err);
  69. });
  70.  
  71. };
  72.  
  73.  
  74. PxerLauncher.prototype['tagMap'] ={
  75. js:{
  76. tag:'script',
  77. attr:{
  78. charset:'utf-8',
  79. type:'text/javascript',
  80. src:'%PATH%',
  81. }
  82. },
  83. css:{
  84. tag:'link',
  85. attr:{
  86. rel:'stylesheet',
  87. href:'%PATH%',
  88. }
  89. },
  90. ico:{
  91. tag:'link',
  92. attr:{
  93. rel:'shortcut icon',
  94. type:'image/x-icon',
  95. href:'%PATH%',
  96. }
  97. },
  98. };
  99.  
  100.  
  101.  
  102.  
  103.