Skip to content

Commit

Permalink
create a plugin for Control JS
Browse files Browse the repository at this point in the history
  • Loading branch information
iamnoah committed Dec 26, 2010
1 parent bfe9db8 commit e244ab4
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 #
Expand Down
24 changes: 23 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
Expand All @@ -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']) << {
Expand All @@ -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:"
Expand Down
42 changes: 42 additions & 0 deletions cjs.writeCapture.js
Original file line number Diff line number Diff line change
@@ -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);
25 changes: 25 additions & 0 deletions test/cjs.html
Original file line number Diff line number Diff line change
@@ -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.