- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore.js
More file actions
Latest commit
84 lines (79 loc) · 2.35 KB
/
Copy pathcore.js
File metadata and controls
84 lines (79 loc) · 2.35 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
(function(window,document){
functionnoop(){}
functionscriptLoad(script,load){
if(typeofload!=='function'){
return;
}
if(script.addEventListener){
script.onload=load;
}else{
script.onreadystatechange=function(){
if(script.readyStatein{
loaded: 1,
complete: 1
}){
script.onreadystatechange=null;
load();
}
}
}
}
functionstringToNode(string){
varnodes=[];
varelm=document.createElement("div");
elm.innerHTML="tagmanager<div>"+string+"</div>";
for(varlast=elm.lastChild;last.firstChild;){
nodes.push(last.removeChild(last.firstChild));
}
returnnodes;
}
functioninsertDom(target,nodes,success,fail){
returnfunction(){
try{
if(nodes.length>0){
varelm=nodes.shift();
varselfRun=insertDom(target,nodes,success,fail);
if(elm.nodeName==='SCRIPT'&&elm.type==="text/shellyscript"){
varscript=document.createElement('script');
script.async=false;
script.type="text/javascript";
script.id=elm.id;
script.text=elm.text||elm.textContent||elm.innerHTML||"";
if(elm.charset){
script.charset=elm.charset;
}
varsrc=elm.getAttribute('data-shellysrc');
if(src){
script.src=src;
scriptLoad(script,selfRun);
}
target.insertBefore(script,null);
}elseif(elm.innerHTML&&elm.innerHTML.toLowerCase().indexOf("<script")>-1){
for(varchilds=[];elm.firstChild;){
childs.push(elm.removeChild(elm.firstChild));
}
target.insertBefore(elm,null);
insertDom(elm,childs,selfRun,fail)();
}else{
target.insertBefore(elm,null);
selfRun();
}
}else{
success();
}
}catch(e){fail();}
}
}
varscriptSrcReplacer=/(<script.*?)src\s*(?=\=\s*")/gi;
varscriptTypeReplacer=/(<script.*?)(\s*type\s*\=\s*"text\/javascript")?(.*?>)/gi;
functionprepareHtml(string){
returnString(string).replace(scriptSrcReplacer,"$1data-shellysrc").replace(scriptTypeReplacer,"$1 type='text/shellyscript'$3");
}
functionhtml(target,string,success,fail){
success=success||noop;
fail=fail||noop;
string=prepareHtml(string);
insertDom(target,stringToNode(string),success,fail)();
}
window.runHtmlWithScript=html;
})(window,document)