-
Notifications
You must be signed in to change notification settings - Fork 391
/
make-bookmarklet
executable file
·43 lines (32 loc) · 1.31 KB
/
make-bookmarklet
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
#!/usr/bin/env ruby
# This may be the most fragile build script ever written.
require 'open3'
def pipe(text, *cmds)
cmds.each {|c|
text = Open3.popen2(c) {|i,o|
i.puts(text); i.close
o.read
}
}
text
end
# Get the code and do ridiculous preprocessing on it.
code = File.read('html.js')
code.sub!('/* INSERT_CSS_HERE */', pipe(File.read('jsgif.css'), 'yui-compressor --type css'))
code.sub!('// INSERT_GIF_JS_HERE', File.read('gif.js'))
code.gsub!(/BEGIN_NON_BOOKMARKLET_CODE.*?END_NON_BOOKMARKLET_CODE/m, '')
# Compile the code, and put it through yui-compressor, too, to get rid of the newlines.
code = pipe(code, 'java -jar closure/compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --warning_level VERBOSE',
'yui-compressor --type js')
# To get rid of the stupid global scope.
code.sub!(/^([^(]*)(\(function\(\)\{)/, '\2\1')
# Bookmarklet-ready.
code = 'javascript:' + code
# Oh, I guess I need to escape the HTML too.
#code = CGI.escape(code) # Hah!
code = code.gsub('%', '%25') # '&'
code = code.gsub('&', '%26')
code = code.gsub("'", '%27')
#code = code.gsub('"', '%27') # The bookmarklet is embedded in single quotes in the HTML page, so I don't need to worry about double-quotes; maybe I should escape them anyway?
# Now it's ready.
print code