Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobo committed Sep 8, 2011
0 parents commit 0a4294f
Show file tree
Hide file tree
Showing 12 changed files with 1,114 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Cakefile
@@ -0,0 +1,54 @@
VERSION = "0.0.1"
PROJECT = "inject"

fs = require("fs")
exec = require("child_process").exec

files = [
"copyright.js"
"licenses.js"
"inject.coffee"
]

task "build", "Build the Project", ->
index = 0
remaining = files.length
allContents = []

readCompileFile = (file, index) ->
if file.indexOf(".coffee") == -1
# read file into slot
console.log "Reading #{file}"
fs.readFile "./src/#{file}", "utf8", (err, contents) ->
throw err if err
remaining--
allContents[index] = contents
if remaining is 0 then cleanArtifacts()
else
# compile
console.log "Compiling #{file}"
exec "coffee --output ./artifacts --compile ./src/#{file} ", (err, stdout, stderr) ->
throw err if err
newFile = file.replace(".coffee", ".js")
fs.readFile "./artifacts/#{newFile}", "utf8", (err, contents) ->
remaining--
allContents[index] = contents
if remaining is 0 then cleanArtifacts()

for file, index in files
readCompileFile(file, index)
cleanArtifacts = () ->
# clean artifacts directory
exec "rm ./artifacts/*.js", (err, stdout, stderr) ->
throw err if err
writeJS()
writeJS = () ->
fs.writeFile "artifacts/#{PROJECT}-#{VERSION}.js", allContents.join("\n\n"), "utf8", (err) ->
throw err if err
srcFile = fs.createReadStream('./src/relay.html');
artFile = fs.createWriteStream('./artifacts/relay-0.0.1.html');
srcFile.once 'open', () ->
require('util').pump srcFile, artFile, () ->
complete()
complete = () ->
console.log "Complete!"
Empty file added README.markdown
Empty file.
350 changes: 350 additions & 0 deletions artifacts/inject-0.0.1.js

Large diffs are not rendered by default.

161 changes: 161 additions & 0 deletions artifacts/relay-0.0.1.html
@@ -0,0 +1,161 @@
<!DOCTYPE html>
<html><head>
<script type="text/javascript">
// ----------------------------------------------------------------------------
// YOU MUST SET THESE TO THE TWO LOCATIONS FOR YOUR relay.html FILE
// OTHERWISE, THE SYSTEM WILL NOT KNOW WHERE TO SEND COMMUNICATION TO
// inject: a relay.html file on the same domain as you're using inject.js
// xhr: a relay.html file on the same domain as your JS files you are including
// ----------------------------------------------------------------------------
var PROXY_LOCATIONS = {
inject: "http://dl.dropbox.com/u/493580/inject/relay.html",
xhr: "http://jheuser-md.linkedin.biz/~jheuser/inject/relay_cdn.html"
};
</script>
</head>
<body>
<script type="text/javascript">
/*
Porthole
Copyright (c) 2011 Ternary Labs. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var Porthole=(typeof Porthole=="undefined")||!Porthole?{}:Porthole;Porthole={trace:function(a){try{console.log("Porthole: "+a)}catch(b){}},error:function(a){try{console.error("Porthole: "+a)}catch(b){}}};Porthole.WindowProxy=function(){};Porthole.WindowProxy.prototype={postMessage:function(){},addEventListener:function(a){},removeEventListener:function(a){}};Porthole.WindowProxyLegacy=function(a,b){if(b===undefined){b=""}this.targetWindowName=b;this.eventListeners=[];this.origin=window.location.protocol+"//"+window.location.host;if(a!==null){this.proxyIFrameName=this.targetWindowName+"ProxyIFrame";this.proxyIFrameLocation=a;this.proxyIFrameElement=this.createIFrameProxy()}else{this.proxyIFrameElement=null}};Porthole.WindowProxyLegacy.prototype={getTargetWindowName:function(){return this.targetWindowName},getOrigin:function(){return this.origin},createIFrameProxy:function(){var a=document.createElement("iframe");a.setAttribute("id",this.proxyIFrameName);a.setAttribute("name",this.proxyIFrameName);a.setAttribute("src",this.proxyIFrameLocation);a.setAttribute("frameBorder","1");a.setAttribute("scrolling","auto");a.setAttribute("width",30);a.setAttribute("height",30);a.setAttribute("style","position: absolute; left: -100px; top:0px;");if(a.style.setAttribute){a.style.setAttribute("cssText","position: absolute; left: -100px; top:0px;")}document.body.appendChild(a);return a},postMessage:function(b,a){if(a===undefined){a="*"}if(this.proxyIFrameElement===null){Porthole.error("Can't send message because no proxy url was passed in the constructor")}else{sourceWindowName=window.name;this.proxyIFrameElement.setAttribute("src",this.proxyIFrameLocation+"#"+b+"&sourceOrigin="+escape(this.getOrigin())+"&targetOrigin="+escape(a)+"&sourceWindowName="+sourceWindowName+"&targetWindowName="+this.targetWindowName);this.proxyIFrameElement.height=this.proxyIFrameElement.height>50?50:100}},addEventListener:function(a){this.eventListeners.push(a);return a},removeEventListener:function(b){try{var a=this.eventListeners.indexOf(b);this.eventListeners.splice(a,1)}catch(c){this.eventListeners=[];Porthole.error(c)}},dispatchEvent:function(c){for(var b=0;b<this.eventListeners.length;b++){try{this.eventListeners[b](c)}catch(a){Porthole.error("Exception trying to call back listener: "+a)}}}};Porthole.WindowProxyHTML5=function(a,b){if(b===undefined){b=""}this.targetWindowName=b};Porthole.WindowProxyHTML5.prototype={postMessage:function(b,a){if(a===undefined){a="*"}if(this.targetWindowName===""){targetWindow=top}else{targetWindow=parent.frames[this.targetWindowName]}targetWindow.postMessage(b,a)},addEventListener:function(a){window.addEventListener("message",a,false);return a},removeEventListener:function(a){window.removeEventListener("message",a,false)},dispatchEvent:function(b){var a=document.createEvent("MessageEvent");a.initMessageEvent("message",true,true,b.data,b.origin,1,window,null);window.dispatchEvent(a)}};if(typeof window.postMessage!="function"){Porthole.trace("Using legacy browser support");Porthole.WindowProxy=Porthole.WindowProxyLegacy;Porthole.WindowProxy.prototype=Porthole.WindowProxyLegacy.prototype}else{Porthole.trace("Using built-in browser support");Porthole.WindowProxy=Porthole.WindowProxyHTML5;Porthole.WindowProxy.prototype=Porthole.WindowProxyHTML5.prototype}Porthole.WindowProxy.splitMessageParameters=function(c){if(typeof c=="undefined"||c===null){return null}var e=[];var d=c.split(/&/);for(var b in d){var a=d[b].split("=");if(typeof(a[1])=="undefined"){e[a[0]]=""}else{e[a[0]]=a[1]}}return e};Porthole.MessageEvent=function MessageEvent(c,a,b){this.data=c;this.origin=a;this.source=b};Porthole.WindowProxyDispatcher={forwardMessageEvent:function(c){var b=document.location.hash;if(b.length>0){b=b.substr(1);m=Porthole.WindowProxyDispatcher.parseMessage(b);if(m.targetWindowName===""){targetWindow=top}else{targetWindow=parent.frames[m.targetWindowName]}var a=Porthole.WindowProxyDispatcher.findWindowProxyObjectInWindow(targetWindow,m.sourceWindowName);if(a){if(a.origin==m.targetOrigin||m.targetOrigin=="*"){c=new Porthole.MessageEvent(m.data,m.sourceOrigin,a);a.dispatchEvent(c)}else{Porthole.error("Target origin "+a.origin+" does not match desired target of "+m.targetOrigin)}}else{Porthole.error("Could not find window proxy object on the target window")}}},parseMessage:function(b){if(typeof b=="undefined"||b===null){return null}params=Porthole.WindowProxy.splitMessageParameters(b);var a={targetOrigin:"",sourceOrigin:"",sourceWindowName:"",data:""};a.targetOrigin=unescape(params.targetOrigin);a.sourceOrigin=unescape(params.sourceOrigin);a.sourceWindowName=unescape(params.sourceWindowName);a.targetWindowName=unescape(params.targetWindowName);var c=b.split(/&/);if(c.length>3){c.pop();c.pop();c.pop();c.pop();a.data=c.join("&")}return a},findWindowProxyObjectInWindow:function(a,c){if(a.RuntimeObject){a=a.RuntimeObject()}if(a){for(var b in a){try{if(a[b]!==null&&typeof a[b]=="object"&&a[b] instanceof a.Porthole.WindowProxy&&a[b].getTargetWindowName()==c){return a[b]}}catch(d){}}}return null},start:function(){if(window.addEventListener){window.addEventListener("resize",Porthole.WindowProxyDispatcher.forwardMessageEvent,false)}else{if(document.body.attachEvent){window.attachEvent("onresize",Porthole.WindowProxyDispatcher.forwardMessageEvent)}else{Porthole.error("Can't attach resize event")}}}};
</script>
<script type="text/javascript">
var whoami = location.hash.substr(1),
hostPrefixRegex = /^https?:\/\//,
hostSuffixRegex = /^(.*?)(\/.*|$)/,
xDomainRpc,
retryReady = null,
loaded = false,
origins = {
inject: trimHost(PROXY_LOCATIONS.inject),
xhr: trimHost(PROXY_LOCATIONS.xhr)
};

if (whoami == "xhr") setupXhr();
else if (whoami == "inject") setupDispatcher();

// a null operation function
function noOp() {}

// set up the dispatcher
function setupDispatcher() {
window.onload=function(){ Porthole.WindowProxyDispatcher.start(); };
}

// sets up an XHR relationship
function setupXhr() {
window.onload=function(){
// Create a proxy window to send to and receive message from the parent
xDomainRpc = new Porthole.WindowProxy(PROXY_LOCATIONS.inject+"#inject");

function looper() {
if (loaded) { return; }
xDomainRpc.postMessage("READY");
retryReady = window.setTimeout(looper, 10);
}
looper();

// Register an event handler to receive messages;
xDomainRpc.addEventListener(function(event) {
if (trimHost(event.origin) !== origins.inject) {
return;
}

if (event.data === "READYREADY") {
window.clearTimeout(retryReady);
retryReady = null;
loaded = true;
return;
}

var pieces = event.data.split(" "),
txId = pieces[0],
module = pieces[1],
path = pieces[2]

doXmlHttpRequest(txId, module, path);
});
};
}

// trims a host down to hostname + port
function trimHost(host) {
host = host.replace(hostPrefixRegex, "").replace(hostSuffixRegex, "$1")
return host
}

// makes an xhr, posts response
function doXmlHttpRequest(txId, module, path) {
var xhr = getXHR();
xhr.open("GET", path);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
xDomainRpc.postMessage([
txId,
module,
path,
xhr.responseText
].join(" "));
}
};
xhr.send(null);
}

// get an XHR object, old school
function getXHR() {
var xmlhttp;
xmlhttp = false;
if (typeof XMLHttpRequest !== "undefined") {
try {
xmlhttp = new XMLHttpRequest();
} catch (errorWin) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof window.createRequest !== "undefined") {
try {
xmlhttp = new window.createRequest();
} catch (errorCr) {
xmlhttp = false;
}
}
if (!xmlhttp) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (msErrOne) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (msErrTwo) {
xmlhttp = false;
}
}
}
if (!xmlhttp) {
throw new Error("Could not create an xmlHttpRequest Object");
}
return xmlhttp;
}

</script>
</body>
</html>
4 changes: 4 additions & 0 deletions example/deps/bar.js
@@ -0,0 +1,4 @@
var Bar = function() {};
Bar.prototype.baz = function() {};

exports.Bar = Bar;
2 changes: 2 additions & 0 deletions example/deps/foo.js
@@ -0,0 +1,2 @@
exports.foo = function() {};
exports.lala = "Stringness!";
23 changes: 23 additions & 0 deletions example/sample.html
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="../artifacts/inject-0.0.1.js"></script>
</head>
<body>
<script type="text/javascript">
inject().config({
path: "http://dl.dropbox.com/u/493580/inject/example/deps"
});

inject("foo", "bar", function(foo, bar) {
try {
console.log("foo is", foo);
console.log("bar is", bar);
}
catch (e) {
alert("foo and bar:"+foo+bar);
}
});
</script>
</body>
</html>
28 changes: 28 additions & 0 deletions example/sample2.html
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="../artifacts/inject-0.0.1.js"></script>
</head>
<body>
<span></span>
<script type="text/javascript">
inject().config({
path: "http://jheuser-md.linkedin.biz/~jheuser/inject/remotedeps",
xd: {
local: "http://dl.dropbox.com/u/493580/inject/relay.html",
remote: "http://jheuser-md.linkedin.biz/~jheuser/inject/relay_cdn.html"
}
});

inject("foo", "bar", function(foo, bar) {
try {
console.log("foo is", foo);
console.log("bar is", bar);
}
catch (e) {
alert("foo and bar:"+foo+bar);
}
});
</script>
</body>
</html>
5 changes: 5 additions & 0 deletions src/copyright.js
@@ -0,0 +1,5 @@
/*
Library: Inject
Homepage: https://github.com/jakobo/inject
License: MIT License
*/

0 comments on commit 0a4294f

Please sign in to comment.