PxerLauncher.class.js

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

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.sleazyfork.org/scripts/23946/152160/PxerLauncherclassjs.js

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

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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%',
        }
    },
};