diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c6eccff --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2011 Alexey Komissarouk, Eric Allen + +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. diff --git a/README b/README new file mode 100644 index 0000000..a062406 --- /dev/null +++ b/README @@ -0,0 +1,2 @@ +This is a simple bridge for making cross-domain AJAX requests to a specific +remote host, probably your API hosted on a different domain from your main app. diff --git a/bridge.js b/bridge.js new file mode 100644 index 0000000..df0335f --- /dev/null +++ b/bridge.js @@ -0,0 +1,35 @@ +Bridge = { + dispatchTable : {}, + nextCallbackId : 0, + init : function(url, origin) { + //sample origin: https://api.example.com + Bridge.origin = origin; + Bridge.iframe = document.createElement('iframe'); + Bridge.iframe.setAttribute('src', url); + document.body.appendChild(Bridge.iframe); + }, + ajax : function(args) { + // add args to callback table + var callbackId = Bridge.nextCallbackId++; + Bridge.dispatchTable[callbackId] = { + success: args.success, + error: args.error + } + var message = { + args: args, + callbackId: callbackId + }; + + Bridge.iframe.contentWindow.postMessage( + JSON.stringify(message), Bridge.origin); + }, + receive : function(event) { + message = JSON.parse(event.data) + func_to_call = + Bridge.dispatchTable[message.callbackId][message.successOrFail]; + // TODO: error check here + func_to_call(message.response); + } +} + +window.addEventListener('message', Bridge.receive, false); diff --git a/iframe.html b/iframe.html new file mode 100644 index 0000000..34a434e --- /dev/null +++ b/iframe.html @@ -0,0 +1,29 @@ + +
+ + + + + diff --git a/test.html b/test.html new file mode 100644 index 0000000..3301a41 --- /dev/null +++ b/test.html @@ -0,0 +1,54 @@ + + + + + + + + + + + +