Skip to content
Browse files

create a plugin for Control JS

  • Loading branch information...
1 parent bfe9db8 commit e244ab4f20dfb00f3b85f8443b5ae7f62a326f15 @iamnoah committed Dec 26, 2010
Showing with 105 additions and 3 deletions.
  1. +15 −2 README.md
  2. +23 −1 build.gradle
  3. +42 −0 cjs.writeCapture.js
  4. +25 −0 test/cjs.html
View
17 README.md
@@ -8,18 +8,29 @@ or if you just want to force your ads to load async:
See the [wiki](/iamnoah/writeCapture/wiki/Usage) for more detailed usage.
-****New!**** [extsrc](http://code.google.com/p/extsrcjs/) support:
+# NEW! - Control JS Support #
+
+The built in `document.write` support in [Control JS](http://stevesouders.com/controljs/)
+is not great. It adds extra `span` tags and is not nearly as robust as a full library like `writeCapture`. That's why writeCapture provides a [plugin](/iamnoah/writeCapture/blob/master/cjs.writeCapture.js) to replace Control JS's `document.write` support with writeCapture.js.
+
+For usage, see the [wiki](/iamnoah/writeCatpture/wiki/ControlJS).
+
+# extsrc Support #
+
+[extsrc](http://code.google.com/p/extsrcjs/) support:
// call from $.ready() or your document ready of choice
writeCapture.extsrc(doneCallback);
-You can use the same markup (`extsrc="..."`,`asryncsrc="..."`) but with a few
+You can use the same markup (`extsrc="..."`,`asyncsrc="..."`) but with a few
advantages:
* All the power of writeCapture.js, so more `document.write` edge cases will work.
* No extra span tags!
* All scripts with `extsrc` will be run in order, so you don't have to worry about dependencies.
+Note that you *do not* need extsrc.js, we are simply supporting the same syntax in writeCapture.
+
# Dependencies #
writeCapture.js was developed using jQuery 1.3.2, but should work with most
@@ -61,6 +72,8 @@ clean HTML, safe for injection into the document.
* [newsweek.com](http://newsweek.com) [uses][jesus] writeCapture.js.
+ * [8tracks.com](http://8tracks.com) "We were having horrible latency with our ad providers and your plugin saved the day! Very impressive work!"
+
If you use writeCapture and want some free advertising, let us know!
# Caveats/Limitations #
View
24 build.gradle
@@ -42,8 +42,17 @@ def doConcatPlugin = {
}
}
+def doConcatCjs = {
+ ant.concat(destfile:'cjs-concat.tmp.js') {
+ fileset(dir:'support',includes:'nolib-support.js')
+ fileset(dir:'.',includes:'writeCapture.js')
+ fileset(dir:'.',includes:'cjs.writeCapture.js')
+ }
+}
+
task concat << doConcat
task concatPlugin << doConcatPlugin
+task concatCjs << doConcatCjs
def doMinifyNolib = {
ant.java(jar:'build/yuicompressor-2.4.2.jar',fork:true) {
@@ -54,6 +63,15 @@ def doMinifyNolib = {
ant.delete(file:'build-concat.tmp.js')
}
+def doMinifyCjs = {
+ ant.java(jar:'build/yuicompressor-2.4.2.jar',fork:true) {
+ arg(value:'-o')
+ arg(value:"writeCapture-${getVersion()}-forCjs-min.js")
+ arg(value:'cjs-concat.tmp.js')
+ }
+ ant.delete(file:'cjs-concat.tmp.js')
+}
+
def doMinifyPlugin = {
ant.java(jar:'build/yuicompressor-2.4.2.jar',fork:true) {
arg(value:'-o')
@@ -67,6 +85,8 @@ task minifyNolib(dependsOn:'concat') << doMinifyNolib
task minifyPlugin(dependsOn:'concatPlugin') << doMinifyPlugin
+task minifyCjs(dependsOn:'concatCjs') << doMinifyCjs
+
task minify << doMinify
task release(dependsOn:['upVersion']) << {
@@ -75,7 +95,9 @@ task release(dependsOn:['upVersion']) << {
doConcat()
doMinifyNolib()
doConcatPlugin()
- doMinifyPlugin()
+ doMinifyPlugin()
+ doConcatCjs()
+ doMinifyCjs()
def version = getVersion()
println "Releasing v${version}"
println "You should:"
View
42 cjs.writeCapture.js
@@ -0,0 +1,42 @@
+(function(global,wc) {
+ function wcEvil(code) {
+ wc.replaceWith(global.CJS.curScript,'<script>'+code+'</script>');
+ }
+
+ function wcExt(src,cb) {
+ // copied from control.js. be sure we support callbacks properly
+ var fonload = ( function(ponload) {
+ switch ( typeof(ponload) ) {
+ case "string":
+ ponload = new Function(ponload);
+ break;
+ case "function":
+ // ponload is already a function
+ break;
+ default:
+ ponload = new Function();
+ }
+ return ponload;
+ })(cb);
+ wc.replaceWith(global.CJS.curScript,'<script src="'+src+'"> </script>',fonload);
+ }
+
+ global.__writeCapture_cjsSupport_setup = function() {
+ // 2. replace CJS's eval and exec with our eval and exec
+ global.CJS.eval = wcEvil;
+ global.CJS.execScript = wcExt;
+ };
+
+ // 1. add an inline CJS script to the top that calls setup
+ // this should be the first script CJS evals
+ var dummy = document.createElement('script');
+ var sib = document.getElementsByTagName('script')[0];
+ sib.parentNode.insertBefore(dummy, sib);
+ wc.replaceWith(dummy,'<script type="text/cjs">__writeCapture_cjsSupport_setup();</script>');
+
+ if(global.writeCapture_cjsSrc) {
+ var se = document.createElement('script');
+ se.src = global.writeCapture_cjsSrc;
+ sib.parentNode.insertBefore(se, sib);
+ }
+})(this,this.writeCapture);
View
25 test/cjs.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
+ dir="ltr" id="html">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>CJS Plugin Tests</title>
+<!--
+ <script type="text/javascript" src="../support/nolib-support.js"></script>
+ <script src="../writeCapture.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../cjs.writeCapture.js" type="text/javascript" charset="utf-8"></script>
+-->
+</head>
+<body>
+
+ <script type="text/cjs">function writeTimes() {}</script>
+ <script type="text/cjs" data-cjssrc="http://stevesouders.com/controljs/examples/docwrite.js?sleep=2&amp;jsdelay=2&amp;t=1293390195"></script>
+<script>(function() {
+ window.writeCapture_cjsSrc = 'http://controljs.googlecode.com/svn-history/trunk/control.js';
+ var script = document.createElement('script');
+ script.src = "../writeCapture-1.0.5-forCjs-min.js";
+ var sib = document.getElementsByTagName('script')[0];
+ sib.parentNode.insertBefore(script, sib);
+})();</script>
+</body>
+</html>

0 comments on commit e244ab4

Please sign in to comment.
Something went wrong with that request. Please try again.