Skip to content

Instantly share code, notes, and snippets.

@hdf
Last active December 16, 2015 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdf/5427331 to your computer and use it in GitHub Desktop.
Save hdf/5427331 to your computer and use it in GitHub Desktop.
Script to download and integrate a list of js libraries, (with the option to compress) into a given html file. (Into the first script tag.)
import os, urllib2
HTML_FILE = 'securechat.html'
SEARCH1 = '<script type="text/javascript">\n'
SEARCH2 = '</script>'
YUI_COMPRESSOR = 'yuicompressor-2.4.7.jar'
SCRIPTS = [
['https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', 0],
# ['https://raw.github.com/openpgpjs/openpgpjs/master/resources/openpgp.min.js', 0],
['https://raw.github.com/openpgpjs/openpgpjs/devel/resources/openpgp.min.js', 0],
['https://cdn.firebase.com/v0/firebase.js', 0],
['https://webrtc-experiment.appspot.com/DataChannel.js', 1]
# ['https://raw.github.com/muaz-khan/WebRTC-Experiment/master/DataChannel/DataChannel.js', 1]
]
def minify(url):
name = url[url.rfind('/')+1:]
outname = name[:-3] + ".min.js"
print "Compressing " + name
with open(name, 'w') as f: f.write(urllib2.urlopen(url).read())
os.system('java -jar "%s" %s "%s" "%s"' % (YUI_COMPRESSOR, '--type js -o', outname, name))
os.remove(name)
with open(outname, 'r') as f: out = f.read()
os.remove(outname)
return out
def main():
out = ""
for s in SCRIPTS:
print "Downloading " + s[0]
if s[1]:
out += minify(s[0])
else:
out += urllib2.urlopen(s[0]).read()
out = out.strip() + '\n\n'
print "Writing scripts to " + HTML_FILE
with open(HTML_FILE, 'rb') as f: file = f.read().decode('utf-8')
part1 = file[:file.find(SEARCH1)+len(SEARCH1)]
part2 = file[file.find(SEARCH2):]
with open(HTML_FILE, 'wb') as f: f.write((part1 + out.strip() + '\n' + part2).encode('utf-8'))
print "Done"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment