forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathljs.js
More file actions
223 lines (221 loc) · 7.64 KB
/
Copy pathljs.js
File metadata and controls
223 lines (221 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//https://github.com/jae-jae/l.js
;(function(window, undefined){
/*
* script for js/css parallel loading with dependancies management
* @author Jonathan Gotti < jgotti at jgotti dot net >
* @licence dual licence mit / gpl
* @since 2012-04-12
* @todo add prefetching using text/cache for js files
* @changelog
* - 2016-08-22 - remove global eval and fix issue #13
* - 2014-06-26 - bugfix in css loaded check when hashbang is used
* - 2014-05-25 - fallback support rewrite + null id bug correction + minification work
* - 2014-05-21 - add cdn fallback support with hashbang url
* - 2014-05-22 - add support for relative paths for stylesheets in checkLoaded
* - 2014-05-21 - add support for relative paths for scripts in checkLoaded
* - 2013-01-25 - add parrallel loading inside single load call
* - 2012-06-29 - some minifier optimisations
* - 2012-04-20 - now sharp part of url will be used as tag id
* - add options for checking already loaded scripts at load time
* - 2012-04-19 - add addAliases method
* @note coding style is implied by the target usage of this script not my habbits
*/
var isA = function(a,b){ return a instanceof (b || Array);}
//-- some minifier optimisation
, D = document
, getElementsByTagName = 'getElementsByTagName'
, length = 'length'
, readyState = 'readyState'
, onreadystatechange = 'onreadystatechange'
//-- get the current script tag for further evaluation of it's eventual content
, scripts = D[getElementsByTagName]("script")
, scriptTag = scripts[scripts[length]-1]
, script = scriptTag.innerHTML.replace(/^\s+|\s+$/g,'')
;
//avoid multiple inclusion to override current loader but allow tag content evaluation
if( ! window.ljs ){
var checkLoaded = scriptTag.src.match(/checkLoaded/)?1:0
//-- keep trace of header as we will make multiple access to it
,header = D[getElementsByTagName]("head")[0] || D.documentElement
, urlParse = function(url){
var parts={}; // u => url, i => id, f = fallback
parts.u = url.replace(/#(=)?([^#]*)?/g,function(m,a,b){ parts[a?'f':'i'] = b; return '';});
return parts;
}
,appendElmt = function(type,attrs,cb){
var e = D.createElement(type), i;
if( cb ){ //-- this is not intended to be used for link
if(e[readyState]){
e[onreadystatechange] = function(){
if (e[readyState] === "loaded" || e[readyState] === "complete"){
e[onreadystatechange] = null;
cb();
}
};
}else{
e.onload = cb;
}
}
for( i in attrs ){ attrs[i] && (e[i]=attrs[i]); }
header.appendChild(e);
// return e; // unused at this time so drop it
}
,ajaxExec = function (url,success,error) {
GM_xmlhttpRequest({
method: "GET",
url:url,
onload:function(res){
//eval.call(global,res.responseText);
eval(res.responseText);
success();
},
onerror:function(){
error && error();
}
});
}
,load = function(url,cb){
if( this.aliases && this.aliases[url] ){
var args = this.aliases[url].slice(0);
isA(args) || (args=[args]);
cb && args.push(cb);
return this.load.apply(this,args);
}
if( isA(url) ){ // parallelized request
for( var l=url[length]; l--;){
this.load(url[l]);
}
cb && url.push(cb); // relaunch the dependancie queue
return this.load.apply(this,url);
}
if( url.match(/\.css\b/) ){
return this.loadcss(url,cb);
}
return this.loadjs(url,cb);
}
,exec = function(url,cb){
if( this.aliases && this.aliases[url] ){
var args = this.aliases[url].slice(0);
isA(args) || (args=[args]);
cb && args.push(cb);
return this.exec.apply(this,args);
}
if( isA(url) ){ // parallelized request
for( var l=url[length]; l--;){
this.exec(url[l]);
}
cb && url.push(cb); // relaunch the dependancie queue
return this.exec.apply(this,url);
}
if( url.match(/\.css\b/) ){
return this.loadcss(url,cb);
}
return this.execJs(url,cb);
}
,loaded = {} // will handle already loaded urls
,loader = {
aliases:{}
,loadjs: function(url,cb){
var parts = urlParse(url);
url = parts.u;
if( loaded[url] === true ){ // already loaded exec cb if any
cb && cb();
return this;
}else if( loaded[url]!== undefined ){ // already asked for loading we append callback if any else return
if( cb ){
loaded[url] = (function(ocb,cb){ return function(){ ocb && ocb(); cb && cb(); }; })(loaded[url],cb);
}
return this;
}
// first time we ask this script
loaded[url] = (function(cb){ return function(){loaded[url]=true; cb && cb();};})(cb);
cb = function(){ loaded[url](); };
appendElmt('script',{type:'text/javascript',src:url,id:parts.i,onerror:function(error){
if( parts.f ){
var c = error.currentTarget;
c.parentNode.removeChild(c);
appendElmt('script',{type:'text/javascript',src:parts.f,id:parts.i},cb);
}
}},cb);
return this;
}
,execJs:function(url,cb){
var parts = urlParse(url);
url = parts.u;
if( loaded[url] === true ){ // already loaded exec cb if any
cb && cb();
return this;
}else if( loaded[url]!== undefined ){ // already asked for loading we append callback if any else return
if( cb ){
loaded[url] = (function(ocb,cb){ return function(){ ocb && ocb(); cb && cb(); }; })(loaded[url],cb);
}
return this;
}
// first time we ask this script
loaded[url] = (function(cb){ return function(){loaded[url]=true; cb && cb();};})(cb);
cb = function(){ loaded[url](); };
/*appendElmt('script',{type:'text/javascript',src:url,id:parts.i,onerror:function(error){
if( parts.f ){
var c = error.currentTarget;
c.parentNode.removeChild(c);
appendElmt('script',{type:'text/javascript',src:parts.f,id:parts.i},cb);
}
}},cb);*/
ajaxExec(url,cb,function(){
if( parts.f ){
ajaxExec(parts.f,cb);
}
});
return this;
}
,loadcss: function(url,cb){
var parts = urlParse(url);
url = parts.u;
loaded[url] || appendElmt('link',{type:'text/css',rel:'stylesheet',href:url,id:parts.i});
loaded[url] = true;
cb && cb();
return this;
}
,load: function(){
var argv=arguments,argc = argv[length];
if( argc === 1 && isA(argv[0],Function) ){
argv[0]();
return this;
}
load.call(this,argv[0], argc <= 1 ? undefined : function(){ loader.load.apply(loader,[].slice.call(argv,1));} );
return this;
}
,exec:function(){
var argv=arguments,argc = argv[length];
if( argc === 1 && isA(argv[0],Function) ){
argv[0]();
return this;
}
exec.call(this,argv[0], argc <= 1 ? undefined : function(){ loader.exec.apply(loader,[].slice.call(argv,1));} );
return this;
}
,addAliases:function(aliases){
for(var i in aliases ){
this.aliases[i]= isA(aliases[i]) ? aliases[i].slice(0) : aliases[i];
}
return this;
}
}
;
if( checkLoaded ){
var i,l,links,url;
for(i=0,l=scripts[length];i<l;i++){
(url = scripts[i].getAttribute('src')) && (loaded[url.replace(/#.*$/,'')] = true);
}
links = D[getElementsByTagName]('link');
for(i=0,l=links[length];i<l;i++){
(links[i].rel==='stylesheet' || links[i].type==='text/css') && (loaded[links[i].getAttribute('href').replace(/#.*$/,'')]=true);
}
}
//export ljs
window.ljs = loader;
// eval inside tag code if any
}
// eval script tag content if needed
scriptTag.src && script && appendElmt('script', {innerHTML: script});
})(window);