PxerLauncher.class.js

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

Ezt a szkriptet nem ajánlott közvetlenül telepíteni. Ez egy könyvtár más szkriptek számára, amik tartalmazzák a // @require https://update.sleazyfork.org/scripts/23946/152160/PxerLauncherclassjs.js hivatkozást.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

'use strict';

class PxerLauncher{
    constructor(options){
        Object.assign(this ,{
            sync:[],
            asyn:[],
            onsync:()=>console.log('Sync list is OK!'),
            onasyn:()=>{},
            onerror:err=>console.error(err),
            cache:true,
        } ,options)
    };
};


PxerLauncher.prototype['load'] =function(){
    this.loadSync();
    this.loadAsyn();
};


PxerLauncher.prototype['createElt'] =function(path){
    var extName =path.replace('.php','').match(/\.([A-Za-z0-9]*?)(?:\?.+)?$/)[1];
    let tagName =this.tagMap[extName].tag;
    var elt =document.createElement(tagName);

    //避免缓存(new Date()).getTime()
    if(!this.cache) path =path.indexOf('?')==-1 ?path+'?'+(new Date()).getTime() :path+'&'+(new Date()).getTime();

    let attr =this.tagMap[extName].attr;
    for(let key in attr){
        if(attr[key] ==='%PATH%'){
            elt.setAttribute(key ,path);
            continue;
        };
        elt.setAttribute(key ,attr[key])
    };

    return elt;

};


PxerLauncher.prototype['loadAsyn'] =function(){
    this.asyn.forEach(function (item){
        let elt =this.createElt(item);
        elt.async =true;
        document.head.appendChild(elt);
    } ,this);
};
PxerLauncher.prototype['loadSync'] =function(){
    var pms =Promise.resolve();
    this.sync.forEach(function (item){
        pms =pms.then(()=>{
            return new Promise((resolve ,reject)=>{
                let elt =this.createElt(item);
                elt.addEventListener('load' ,resolve);
                elt.addEventListener('error' ,reject.bind(null ,elt));
                document.head.appendChild(elt);
            });
        });
    } ,this);

    pms.then(this.onsync);
    pms.catch((err)=>{
        console.error('PxerLauncher load file error!');
        this.onerror(err);
    });

};


PxerLauncher.prototype['tagMap'] ={
    js:{
        tag:'script',
        attr:{
            charset:'utf-8',
            type:'text/javascript',
            src:'%PATH%',
        }
    },
    css:{
        tag:'link',
        attr:{
            rel:'stylesheet',
            href:'%PATH%',
        }
    },
    ico:{
        tag:'link',
        attr:{
            rel:'shortcut icon',
            type:'image/x-icon',
            href:'%PATH%',
        }
    },
};