Wikipedysta:Persino/Gadget-StronicowyParser.js/Funkcje.js

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
StronicowyParser.Kodowanie=function(){
	this.EncodeWiki=function(parametr){
		parametr=parametr.replace(/[_\s]+/g,"_");
		return parametr;
	}
	this.DecodeWiki=function(parametr){
		parametr=parametr.replace(/[_\s]+/g," ");
		return parametr;
	}
	this.IsEncodedHtml=function(parametr,encode,encode_real){
		let czy_dobrze=(((encode_real)&&(parametr.match(new RegExp("^[\\w\\s.,\\/#!$\\^&\\*;:{}=\\-_`~()]*$","g"))))
		                       ||((encode)&&(parametr.match(new RegExp("^[^%]*$","g"))))
		                       ||((((!encode_real)||((encode_real)&&(parametr.match(new RegExp("^[\\w\\s.,\\/#!$%\\^&\\*;:{}=\\-_`~()]*$","g")))))
		                       	      &&parametr.match(/%[0-9A-F]{2}/g))
		                              &&(!parametr.match(/%$/g))
		                              &&(!parametr.match(/%.$/g))
		                              &&(!parametr.match(/%[^0-9A-F]./g))
		                              &&(!parametr.match(/%.[^0-9A-F]/g))
		                          )
		                );
		return czy_dobrze;
	}
	this.EncodeHtml=function(parametr,encode,encode_real){
		if(parametr===null){return null;}
		if(this.IsEncodedHtml(parametr,((encode!==undefined)?encode:true),encode_real)){return this.EncodeWiki(parametr);}
		return encodeURIComponent(this.EncodeWiki(parametr));
	}
	this.DecodeHtml=function(parametr,spacje,encode,encode_real){
		if(parametr===null){return null;}
		if(this.IsEncodedHtml(parametr,((encode!==undefined)?encode:false),encode_real)){
			if(spacje){
				return this.DecodeWiki(decodeURIComponent(parametr));
			}else{
				return this.EncodeWiki(decodeURIComponent(parametr));
			}
		}
		if(spacje){
	    	return this.DecodeWiki(parametr);
		}else{
			return this.EncodeWiki(parametr);
		}
	}
}
StronicowyParser.Wzor=function(){
	this.BezZnakowSpecjalnych=function(ciag){
		return ciag.replace(/([\\\+\*\.\(\)\{\}\[\]\/\,\|\?\^\$])/g,"\\$1");
	}
	this.NiezaleznyOdWielkosciLiterIOznaczeniaOdstepu=function(ciag){
	   let __replace=function(s){
	   	  let maly=s.toLowerCase();let duzy=s.toUpperCase();
	   	  if(maly!=duzy){
	   		return "["+duzy+maly+"]";
	   	  }
	   	  return s;
	   }
	   return"^[\\s_]*"+(ciag.replace(/^[\s_]*/g,"").replace(/[\s_]*$/g,"")).replace(/(.)/g,__replace).replace(/[\s_]+/g,"[\\s_]+")+"[\\s_]*$";
	}
	this.DopasujCiagDoWzorowTablicy=function(tablica,ciag){
		for(let p in tablica){
			let wzor=this.NiezaleznyOdWielkosciLiterIOznaczeniaOdstepu(p);
			let re=new RegExp(wzor);
			return re.test(ciag);
		}
		return false;
	}
}
StronicowyParser.HNumer=function(){
	this.LiczonyHNumer=function(){
		let ulozenie=null;
		let ostatnie=0;
	    return function(h_numer){
	    	if((ulozenie==null)||(ulozenie>=h_numer)){
				ulozenie=h_numer-1;
        	}else{
        		if(ostatnie+1<h_numer-ulozenie){
                	ulozenie=h_numer-ostatnie-1;
        		}
        	}
    		ostatnie=h_numer-ulozenie;
    		h_numer=h_numer-ulozenie;
    		return h_numer;
	    }
	}
	this.PoziomNaglowka=function(hparent){
		let h=hparent.tagName.toUpperCase();
		if(!/^H\d+$/g.test(h)){return null;}
		let h_numer=undefined;
		let h_klasa=hparent.classList;
		for(let xx=0;xx<h_klasa.length;++xx){
			let klasa_pojedynca=h_klasa[xx];
			let czy_pasuje_klasa=klasa_pojedynca.match(/^mw\-hnumber\-\d+$/g);//Szablon HNumer
			if(czy_pasuje_klasa){
				let h_numer2=parseInt(czy_pasuje_klasa.toString().replace(/^mw\-hnumber\-/g,""));
				if(!isNaN(h_numer2)){
					if(h_numer2>6){
							h_numer=h_numer2;
					}
				}
				break;
			}
		}
		 if(h_numer===undefined){
			h_numer=h.replace(/^[^\d]+/g,"");
			h_numer=parseInt(h_numer);
			h_numer=((!isNaN(h_numer))?h_numer:null);
		}
		return h_numer;
	}
}
StronicowyParser.ParametrNumerowanySzablonu=function(strona){
	var tab={
		"|":"{{!}}",
		"=":"{{=}}",
	}
	function Zamienianie(parametr){
		var mikroszablon=tab[parametr];
		if(mikroszablon!==undefined){return mikroszablon;}
		return parametr;
	}
	return strona.replace(/(.)/g,Zamienianie);
}	
StronicowyParser.PotrzebneFunkcje=function(){
	this.UzyskajLiczbe=function(wikikod){
    	let div=document.createElement('div');
    	div.innerHTML=wikikod;
  
    	let hn=div.querySelector(".hn");
    	let classlist=hn.classList;
    	let c=undefined;
    	for(let s=0;s<classlist.length;++s){
    		if(/^hn_\d+$/g.test(classlist[s])){
    			c=classlist[s].replace(/^hn_/g,"");
    			c=parseInt(c);
    			if(isNaN(c)){return undefined;}
    				return new Array(c,hn);
    			}
    		}
    		return undefined;
		};
};
StronicowyParser.Style=function(){
	this.PrzeniesStyleNaPoczatekArtykulu=function(wikikod){
		let styl=wikikod.match(/<[Ss][Tt][Yy][Ll][Ee]\s*>(.*)<\/[Ss][Tt][Yy][Ll][Ee]>/g);
		styl=((styl!=null)?styl.toString():"");
			
		let styl2=wikikod.match(/<[Ss][Tt][Yy][Ll][Ee]\s+[^<>]*>(.*)<\/[Ss][Tt][Yy][Ll][Ee]>/g);
		styl2=((styl2!=null)?styl2.toString():"");
		let styl_koncowe=styl+styl2
		if(styl_koncowe!=""){
    		let mw_parser_output=document.querySelector("#mw-content-text .mw-parser-output");
    		if(mw_parser_output!==null){
    			let div=document.createElement('div');
    			div.innerHTML=styl_koncowe;
    			let style=div.getElementsByTagName('style');
    			if((style!==null)&&(style.length>0)){
    				for(let i=0;i<style.length;++i){
    					let duplikat=style[i].getAttribute('data-mw-deduplicate');
    					let tak=false;
    					let duplikat_children=$(mw_parser_output).children();
    					if((duplikat_children!==null)&&(duplikat_children.length>0)){
    					    for(let s=0;s<duplikat_children.length;++s){
    					    	let duplikat2=duplikat_children[s].getAttribute('data-mw-deduplicate');
    					    	if(duplikat2==duplikat){
    					    		tak=true;
    					    		break;
    					    	}
    					    }
    					}
    					if(!tak){
    						mw_parser_output.insertBefore(style[i],mw_parser_output.firstChild);
    					}
    				}
    			}
    		}
    	}
	}
	
	this.UsunStyle=function(kod){
		return kod.replace(/<[Ss][Tt][Yy][Ll][Ee]\s*>(.*)<\/[Ss][Tt][Yy][Ll][Ee]>/g,"").replace(/<[Ss][Tt][Yy][Ll][Ee]\s+[^<>]*>(.*)<\/[Ss][Tt][Yy][Ll][Ee]>/g,"")
	}

}
StronicowyParser.Muteks=function(){
    var start=true;
    var stop=true;
    var tab=new Array();
    
    this.LiczbaBlokatPlus=0;
    this.LiczbaBlokatMinus=0;
    
    this.LiczbaBlokat = function(){return tab.length;}
    this.zablokuj = async function(na_poczatek,ktory) {
    	this.LiczbaBlokatPlus+=1;
        let promise = new Promise((resolve,reject) => {
        	        		this.odblokuj=()=>{stop=false;this.LiczbaBlokatMinus+=1;}
        					if(start){
        						resolve();start=false;
        					}else{
        						if(!na_poczatek){
        							tab.push(resolve);
        						}else{
        							if(!ktory){
        								tab.unshift(resolve);
        							}else{
        								tab.splice(parseInt(ktory)||0,0,resolve);
        							}
        						}
        						function MutexPromise(){
        							setTimeout(()=>{
        								if(!stop){
        									   if((tab.length!=0)&&(tab[0]==resolve)){
        											stop=true;
        							   		    	tab.shift();
        							   		    	if(tab.length==0){start=true;stop=true;}
        							   				resolve();
        										}else{
        											MutexPromise();
        										}
        								}else{
        									MutexPromise();
        								}
        							},300);
        						}
        						MutexPromise();
        					}
                      });
       return promise;
    };
}
StronicowyParser.GenerowaneObiekty=function(liczba_watkow){
		
	this.length=((liczba_watkow!==undefined)?liczba_watkow:0);
	this.lengthPlus=((liczba_watkow!==undefined)?liczba_watkow:0);
	this.lengthMinus=0;
	/*Przestarzałe*/
	/*this.push=function(...args){
		let liczba_argumentow=args.length;
		this.length+=liczba_argumentow;
		this.lengthPlus+=liczba_argumentow;
	}
	this.pop=function(){
		this.length-=1;
		this.lengthMinus+=1;	
	}*/
	/**/
	this.wygeneruj=function(liczba_watkow){
		this.length+=liczba_watkow;
		this.lengthPlus+=liczba_watkow;
	}
	this.zwolnij=function(){
		this.length-=1;
		this.lengthMinus+=1;	
	}
}
StronicowyParser.CzekajNaZakaczenie=function(tab_promise,tab_promise2,muteks,__FUNKCJA_START,__FUNKCJA_KONTINUUM,__FUNKCJA_KONIEC){
	return new Promise((resolve,reject)=>{
		if(__FUNKCJA_START){__FUNKCJA_START(tab_promise);};
		function Czekaj(){
			if(__FUNKCJA_KONTINUUM){__FUNKCJA_KONTINUUM(tab_promise);};
			if((tab_promise.length==0)&&((!muteks)||(muteks.LiczbaBlokat()==0))){
				if(tab_promise2){tab_promise2.zwolnij();}
				if(__FUNKCJA_KONIEC){__FUNKCJA_KONIEC(tab_promise);}
				resolve();
				return;
			}else{setTimeout(()=>{Czekaj();},300);}
		}
		Czekaj();
	});
}
StronicowyParser.CzekajNaZadaniaApi=function(){
	let c=0;
	let muteks=new StronicowyParser.Muteks();
	this.ZadaniaApi=async function(params,token){
			return new Promise(async(resolve,reject)=>{
				async function Czekaj(){
					await muteks.zablokuj();
					if(c<100){
						c=c+1;
						muteks.odblokuj();
						((token)?((new mw.Api()).postWithToken(token,params)):((new mw.Api()).post(params))).then(async(wynik)=>{
							await muteks.zablokuj();
							c=c-1;
							muteks.odblokuj();
							resolve(wynik);
						}).catch(async(wynik)=>{
							await muteks.zablokuj();
							c=c-1;
							muteks.odblokuj();
							reject(wynik);
						})
					}else{
						muteks.odblokuj();
						setTimeout(async()=>{Czekaj();},300);
					}
				}
			Czekaj();
		});
	}
};
StronicowyParser.api=new StronicowyParser.CzekajNaZadaniaApi();
StronicowyParser.style=new StronicowyParser.Style();
StronicowyParser.potrzebne=new StronicowyParser.PotrzebneFunkcje();
StronicowyParser.kodowanie=new StronicowyParser.Kodowanie();
StronicowyParser.wzor=new StronicowyParser.Wzor();
StronicowyParser.hnumer=new StronicowyParser.HNumer();
StronicowyParser.muteks=new StronicowyParser.Muteks();