-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjector.js
163 lines (134 loc) · 3.78 KB
/
injector.js
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
//injector
(function (window) {
var injector = function(g, fn){
var sc=g['SCOPE']
, fn=fn||function(){}
, cnt
, doc = document
, cE = 'createElement'
, aP = 'appendChild'
;
if(sc){
fn('already exists');
}else{
g['SCOPE'] = sc = {};
function getCnt() {
if (!cnt) {
cnt = doc.getElementById('SCOPE');
if(!cnt){
cnt = doc[cE]('div');
cnt.id = 'SCOPE';
doc.body[aP](cnt);
};
};
return cnt;
};
function mU (u) {
return ((u.match(/^\//))? sc.href + u: u);
};
function iJS(url, fn){
var fn = fn || function(){}
, el = doc[cE]('script')
;
el.type = 'text/javascript';
el.src = mU(url);
function ol (err) {
fn(err, sc.exports);
sc.exports = undefined;
};
return ld(el, url, ol);
};
function iCSS(url, fn) {
var fn = fn || function(){}
, el = doc[cE]('link')
;
el.type = 'text/css';
el.rel = 'stylesheet';
el.href = mU(url);
return ld(el, url, fn);
};
function iHTML(tg, url, fn) {
var fn = fn || function(){}
, el = doc[cE]('object')
;
el.data = mU(url);
return tg[aP](el);
};
function ld(el, u, cb) {
var ua = u.split('/')
, n = ua[ua.length - 1]
;
el.id = n;
function on (err) {
return function () {
cb(err);
};
}
el.onload = on();
el.onabort = on('Abort');
el.onerror = on('Load error');
return getCnt()[aP](el);
};
sc.getContainer = getCnt;
sc.includeJS = iJS;
sc.includeCSS = iCSS;
sc.includeHTML = iHTML;
sc.exports = undefined;
sc.v = '0.6';
sc.href = 'HREF';
sc._load = ld;
fn(null, sc);
};
};
/*
* Make injection script
* make([scope,] [href,] callback);
* callback(error, scope);
*/
function make(cb) {
var str = injector.toString()
, sc
, hr
, func
;
switch (arguments.length) {
case 1:
sc = 'INJECT';
hr = window.location.href;
func = ('function' == typeof arguments[0])? arguments[0]: function(){};
break;
case 2:
sc = ('string' == typeof arguments[0])? arguments[0].replace(/\s/g, ''): 'INJECT';
hr = window.location.href;
func = ('function' == typeof arguments[1])? arguments[1]: function(){};
break;
case 3:
sc = ('string' == typeof arguments[0])? arguments[0].replace(/\s/g, ''): 'INJECT';
hr = ('string' == typeof arguments[1])? arguments[1].replace(/\s/g, ''): window.location.href;
func = ('function' == typeof arguments[2])? arguments[2]: function(){};
break;
default:
fn('Wrong argument');
};
hr = hr.replace(/\/$/, '');
str = minJS(str.replace(/SCOPE/g, sc).replace(/HREF/g, hr));
func = minJS(func.toString());
return "javascript:(" + str + '(this,' + func +'));';
};
function minJS(jsStr) {
var r = jsStr.replace(/\s{2,}|\r|\n/g, '')
.replace(/,\s+/g, ',')
.replace(/\s{0,}\)\s{0,}/g, ')').replace(/\s{0,}\(\s{0,}/g, '(')
.replace(/\s{0,}{\s{0,}/g,'{').replace(/[\s;]{0,}}\s{0,}/g,'}')
.replace(/\s{0,}=\s{0,}/g, '=').replace(/\s{0,}==\s{0,}/g, '==').replace(/\s{0,}===\s{0,}/g, '===')
.replace(/\s{0,}!\s{0,}/g, '!').replace(/\s{0,}!=\s{0,}/g, '!=').replace(/\s{0,}!==\s{0,}/g, '!==')
.replace(/\s{0,}>\s{0,}/g, '>').replace(/\s{0,}>>\s{0,}/g, '>>').replace(/\s{0,}>=\s{0,}/g, '>=')
.replace(/\s{0,}<\s{0,}/g, '<').replace(/\s{0,}<<\s{0,}/g, '<<').replace(/\s{0,}<=\s{0,}/g, '<=')
.replace(/\s{0,}&\s{0,}/g, '&').replace(/\s{0,}&&\s{0,}/g, '&&')
.replace(/\s{0,}\|\s{0,}/g, '|').replace(/\s{0,}\|\|\s{0,}/g, '||')
.replace(/function\s+/g, 'function ').replace(/var\s+/g, 'var ').replace(/case\s+/g, 'case ')
;
return r;
};
window.makeInject = make;
})(window);