-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmkbookmarklet
More file actions
executable file
·61 lines (50 loc) · 1.68 KB
/
mkbookmarklet
File metadata and controls
executable file
·61 lines (50 loc) · 1.68 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
#!/usr/bin/env python
import subprocess as sp
import json
import sys
import os
if len(sys.argv) == 1:
sys.stderr.write("NOTICE: No prefix provided; using 'files/browsercast/\n")
sys.stderr.write("NOTICE: This bookmarklet will work when running "
"'ipython notebook' from this directory.\n")
sys.argv.append("files/browsercast/")
PREFIX = sys.argv[1]
SCRIPTS = [
"popcorn-complete-1.3.min.js",
"browsercast.js",
]
CSS = [
"browsercast.css",
]
JS = """
var prefix = %(PREFIX)s;
%(CSS)s.forEach(function(css_url) {
var s = document.createElement('link');
s.setAttribute('rel', 'stylesheet');
s.setAttribute('type', 'text/css');
s.setAttribute('href', prefix + css_url + "?" + new Date());
document.getElementsByTagName('body')[0].appendChild(s);
});
%(SCRIPTS)s.forEach(function(script_url) {
var s = document.createElement('script');
s.setAttribute('src', prefix + script_url + "?" + new Date());
document.getElementsByTagName('body')[0].appendChild(s);
});
""" %{
"PREFIX": json.dumps(PREFIX),
"SCRIPTS": json.dumps(SCRIPTS),
"CSS": json.dumps(CSS),
}
COMPILE = [
"closure", "--compilation_level", "ADVANCED_OPTIMIZATIONS"
]
if os.system("which closure > /dev/null"):
sys.stderr.write("WARNING: closure compiler not found; "
"not compressing bookmarklet.\n")
COMPILE = ["cat"]
compiler = sp.Popen(COMPILE, stdin=sp.PIPE, stdout=sp.PIPE)
compiled, _ = compiler.communicate(input=JS)
if compiler.returncode:
sys.exit(compiler.returncode)
compiled = compiled.strip().strip(";")
print "javascript:void((function(){%s})())" %(compiled, )