@@ -10,7 +10,8 @@ JSLINT = ctx.JSLINT
10
10
try
11
11
if process .argv .length < 3
12
12
throw new Error " No file specified"
13
- script = fs .readFileSync process .argv [2 ], ' utf8'
13
+ filename = process .argv [2 ]
14
+ script = fs .readFileSync filename, ' utf8'
14
15
catch e
15
16
console .log " File could not be read"
16
17
process .exit 1
@@ -27,11 +28,15 @@ unless details?
27
28
console .error " There are no details!"
28
29
process .exit 1
29
30
30
- globals = [' document' ,' twttr' ]
31
- if details .access ? and Array .isArray details .access
32
- for a in details .access
33
- if typeof a is ' string' and a .match / ^ [a-zA-Z ] + $ /
34
- globals .push a
31
+ details .id = 2
32
+ details .revision = 1
33
+
34
+ # Alias dependencies.
35
+ accessList = details .access ? []
36
+ accessList .unshift " app"
37
+ accessList .unshift " plugin"
38
+ for access in accessList
39
+ script = " var #{ access} = lib.#{ access} ();\n #{ script} "
35
40
36
41
walk = (ast ) ->
37
42
if Array .isArray (ast) and ast .length > 0
@@ -55,16 +60,6 @@ walk = (ast) ->
55
60
else if type is ' unary-prefix'
56
61
ast[2 ] = walk ast[2 ]
57
62
else if type is ' name'
58
- if globals .indexOf (ast[1 ]) isnt - 1
59
- ast = [
60
- ' call'
61
- [
62
- ' dot'
63
- [' name' ,' lib' ]
64
- ' global'
65
- ]
66
- [[' string' ,ast[1 ]]]
67
- ]
68
63
# skip
69
64
else if type is ' sub'
70
65
ast[1 ] = walk ast[1 ]
@@ -123,10 +118,6 @@ walk = (ast) ->
123
118
]
124
119
]
125
120
ast[2 ][1 ].unshift [
126
- ' stat'
127
- ast[1 ]
128
- ]
129
- ast[2 ][1 ].push [
130
121
' if'
131
122
[
132
123
' unary-prefix'
@@ -144,6 +135,10 @@ walk = (ast) ->
144
135
]
145
136
undefined
146
137
]
138
+ ast[2 ][1 ].unshift [
139
+ ' stat'
140
+ ast[1 ]
141
+ ]
147
142
ast = [
148
143
' for'
149
144
null
@@ -216,14 +211,27 @@ ast = uglify.ast_mangle ast, {
216
211
defines : {' this' : [' name' , " null" ]}
217
212
}
218
213
# ast = uglify.ast_squeeze ast
219
- script = uglify .gen_code ast, beautify : true , indent_start : 8 , indent_level : 2 , quote_keys : true
214
+ script = uglify .gen_code ast, beautify : true , indent_start : 4 , indent_level : 2 , quote_keys : true
220
215
console .log script
221
216
222
217
script = script .replace / (\n | ^ )\/\/ . * (\n | $ )/ g , " $2"
223
- adsafeId = " APPAAAAFF_"
224
- script = " <div id=\" #{ adsafeId} \" >\n <script>\n ADSAFE.go(\" #{ adsafeId} \" , function (dom, lib) {\n \" use strict\" ;\n #{ script} }\n );\n </script>\n </div>"
218
+ num2alphabet = (num ) ->
219
+ chars = " ABCDEFGHIJ" .split (" " )
220
+ num = parseInt num
221
+ if isNaN (num) or ! isFinite (num)
222
+ throw new Error " Invalid number"
223
+ num = " " + num
224
+ out = " "
225
+ for i in [0 ... num .length ]
226
+ out += chars[parseInt num .charAt (i)]
227
+ return out
228
+ id = details .id
229
+ adsafeId = " ZZZ#{ num2alphabet id} _"
230
+ head = " <div id=\" #{ adsafeId} \" ><script>\n "
231
+ foot = " </script>\n </div>"
232
+ script = " #{ head} ADSAFE.go(\" #{ adsafeId} \" , function (dom, lib) {\n \" use strict\" ;\n #{ script} \n });#{ foot} "
225
233
ok = JSLINT script, {
226
- adsafe : true , fragment : true , predef : globals , browser : true , safe : true , bitwise : true , continue : true , eqeq : true , es5 : true , evil : false , forin : true , newcap : true , nomen : true , plusplus : true , regexp : true , undef : true , unparam : true , sloppy : true , stupid : true , sub : true , vars : true , white : true , css : true
234
+ adsafe : true , fragment : true , predef : [] , browser : true , safe : true , bitwise : true , continue : true , eqeq : true , es5 : true , evil : false , forin : true , newcap : true , nomen : true , plusplus : true , regexp : true , undef : true , unparam : true , sloppy : true , stupid : true , sub : true , vars : true , white : true , css : true
227
235
}, {
228
236
plugin : false
229
237
}
@@ -232,6 +240,7 @@ unless ok
232
240
result = JSLINT .data ()
233
241
console .log util .inspect result, false , 3 , true
234
242
else
235
- script = script .replace " <script>" ," <script>\n (function(){\n var ADSAFE = new ADSAFE_APP(\" #{ adsafeId} \" ,#{ JSON .stringify (details)} );"
236
- script = script .replace " </script>" ," }());\n </script>"
243
+ script = script .substr (head .length , script .length - (head .length + foot .length ))
244
+ script = " new ADSAFE_APP(#{ id} , \" #{ adsafeId} \" , #{ JSON .stringify (details)} , function(ADSAFE){\n #{ script} \n });"
245
+ fs .writeFileSync " #{ filename} .adsafe.js" , script
237
246
console .log script
0 commit comments