diff --git a/.gitignore b/.gitignore index 009aaf6d..aa9ffa70 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ *~ *.js !zxcvbn.js +!zxcvbn-async.js +!zxcvbn-async-bower.js test/index_dev.html node_modules diff --git a/async.coffee b/async.coffee deleted file mode 100644 index 73ea6b56..00000000 --- a/async.coffee +++ /dev/null @@ -1,16 +0,0 @@ - -ZXCVBN_SRC = 'zxcvbn.js' - -# adapted from http://friendlybit.com/js/lazy-loading-asyncronous-javascript/ -async_load = -> - s = document.createElement 'script' - s.src = ZXCVBN_SRC - s.type = 'text/javascript' - s.async = true - first = document.getElementsByTagName('script')[0] - first.parentNode.insertBefore s, first - -if window.attachEvent? - window.attachEvent 'onload', async_load -else - window.addEventListener 'load', async_load, false diff --git a/compile_and_minify.sh b/compile_and_minify.sh index bebccd90..2c514fdc 100755 --- a/compile_and_minify.sh +++ b/compile_and_minify.sh @@ -9,7 +9,12 @@ function_wrap () { echo 'compiling cs -> js' coffee --compile --bare {matching,scoring,init}.coffee -coffee --compile async.coffee + +# bower's async loading uses a different relative path +cp zxcvbn-async.js zxcvbn-async-bower.js +sed -i '' 's/zxcvbn.js/bower_components\/zxcvbn\/zxcvbn.js/' zxcvbn-async-bower.js +sed -i '' '/^\/\//d' zxcvbn-async-bower.js +sed -i '' '/^$/d' zxcvbn-async-bower.js echo 'compiling js -> js' # closure's simple optimizations ended up being about 200k better than whitespace-only. @@ -18,6 +23,5 @@ echo 'compiling js -> js' COMPILATION_LEVEL=SIMPLE_OPTIMIZATIONS cat {matching,scoring,adjacency_graphs,frequency_lists,init}.js | function_wrap > compiled.js java -jar tools/closure.jar --compilation_level $COMPILATION_LEVEL --js compiled.js --js_output_file zxcvbn.js -java -jar tools/closure.jar --compilation_level $COMPILATION_LEVEL --js async.js --js_output_file zxcvbn-async.js rm -f compiled.js -echo 'done. produced zxcvbn.js and zxcvbn-async.js' +echo 'done' diff --git a/zxcvbn-async-bower.js b/zxcvbn-async-bower.js new file mode 100644 index 00000000..6d6ac415 --- /dev/null +++ b/zxcvbn-async-bower.js @@ -0,0 +1,17 @@ +(function() { + var ZXCVBN_SRC = 'bower_components/zxcvbn/zxcvbn.js'; + var async_load = function() { + var first, s; + s = document.createElement('script'); + s.src = ZXCVBN_SRC; + s.type = 'text/javascript'; + s.async = true; + first = document.getElementsByTagName('script')[0]; + return first.parentNode.insertBefore(s, first); + }; + if (window.attachEvent != null) { + window.attachEvent('onload', async_load); + } else { + window.addEventListener('load', async_load, false); + } +}).call(this); diff --git a/zxcvbn-async.js b/zxcvbn-async.js index 7a2a45f4..17149ea8 100644 --- a/zxcvbn-async.js +++ b/zxcvbn-async.js @@ -1 +1,31 @@ -(function(){var a;a=function(){var a,b;b=document.createElement("script");b.src="zxcvbn.js";b.type="text/javascript";b.async=!0;a=document.getElementsByTagName("script")[0];return a.parentNode.insertBefore(b,a)};null!=window.attachEvent?window.attachEvent("onload",a):window.addEventListener("load",a,!1)}).call(this); +// cross-browser asynchronous script loading for zxcvbn. +// adapted from http://friendlybit.com/js/lazy-loading-asyncronous-javascript/ + +// Suggestion: instead of manually configuring this script, +// follow the bower setup instructions (see README) for easier installation and +// updating. + +// If you do want to manually include zxcvbn, you'll likely only need to change +// ZXCVBN_SRC to point to the correct relative path from your index.html. +// (this script assumes index.html and zxcvbn.js sit next to each other.) + +(function() { + var ZXCVBN_SRC = 'zxcvbn.js'; + + var async_load = function() { + var first, s; + s = document.createElement('script'); + s.src = ZXCVBN_SRC; + s.type = 'text/javascript'; + s.async = true; + first = document.getElementsByTagName('script')[0]; + return first.parentNode.insertBefore(s, first); + }; + + if (window.attachEvent != null) { + window.attachEvent('onload', async_load); + } else { + window.addEventListener('load', async_load, false); + } + +}).call(this);