Added Cross-domain javascript requests

This commit is contained in:
Dmitriy Simushev 2011-12-09 10:18:52 +00:00
parent 777b640f8e
commit 04a096fea1
6 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,8 @@
var mibewRequestedScripts=[],mibewHandlers=[],mibewHandlersDependences=[];
function mibewMakeRequest(){mibewDoLoadScript(mibewRequestUrl+"&rnd="+Math.random(),"responseScript")}
function mibewOnResponse(a){var b=a.load,c=a.handlers,e=a.data,a=a.dependences;for(id in b)if(!(b[id]in mibewRequestedScripts))mibewRequestedScripts[id]=[],mibewRequestedScripts[id].url=b[id],mibewRequestedScripts[id].status="loading",mibewLoadScript(id);for(handler in a)handler in mibewHandlersDependences||(mibewHandlersDependences[handler]=a[handler]);for(b=0;b<c.length;b++){var d=c[b];if(mibewCanRunHandler(c[b]))window[d](e);else c[b]in mibewHandlers||(mibewHandlers[d]=function(){window[d](e)})}mibewCleanUpAfterRequest(); window.setTimeout(mibewMakeRequest,mibewRequestTimeout)}
function mibewCleanUpAfterRequest(){document.getElementsByTagName("head")[0].removeChild(document.getElementById("responseScript"))}
function mibewDoLoadScript(a,b){var c=document.createElement("script");c.setAttribute("type","text/javascript");c.setAttribute("src",a);c.setAttribute("id",b);document.getElementsByTagName("head")[0].appendChild(c);return c}
function mibewLoadScript(a){var b=mibewDoLoadScript(mibewRequestedScripts[a].url,a);b.onload=function(){mibewScriptReady(a)};b.onreadystatechange=function(){("complete"==this.readyState||"loaded"==this.readyState)&&mibewScriptReady(a)}}
function mibewScriptReady(a){mibewRequestedScripts[a].status="ready";for(handlerName in mibewHandlers)mibewCanRunHandler(handlerName)&&(mibewHandlers[handlerName](),delete mibewHandlers[handlerName])}
function mibewCanRunHandler(a){for(var a=mibewHandlersDependences[a],b=0;b<a.length;b++)if("ready"!=mibewRequestedScripts[a[b]].status)return!1;return!0};

View File

@ -41,6 +41,10 @@
<arg value="invite:1"/>
<arg value="--js"/>
<arg value="invite.js"/>
<arg value="--module"/>
<arg value="request:1"/>
<arg value="--js"/>
<arg value="request.js"/>
</java>
</target>

View File

@ -0,0 +1,98 @@
var mibewRequestedScripts = new Array();
var mibewHandlers = new Array();
var mibewHandlersDependences = new Array();
function mibewMakeRequest()
{
mibewDoLoadScript(mibewRequestUrl + '&rnd=' + Math.random(), 'responseScript');
}
function mibewOnResponse(response)
{
var load = response.load;
var handlers = response.handlers;
var data = response.data;
var dependences = response.dependences;
for(id in load){
if(! (load[id] in mibewRequestedScripts)){
mibewRequestedScripts[id] = new Array();
mibewRequestedScripts[id]['url'] = load[id];
mibewRequestedScripts[id]['status'] = 'loading';
mibewLoadScript(id);
}
}
for(handler in dependences){
if(! (handler in mibewHandlersDependences)){
mibewHandlersDependences[handler] = dependences[handler];
}
}
for(var i = 0; i < handlers.length; i++){
var handlerName = handlers[i];
if(mibewCanRunHandler(handlers[i])){
window[handlerName](data);
}else{
if(! (handlers[i] in mibewHandlers)){
mibewHandlers[handlerName] = function(){
window[handlerName](data);
};
}
}
}
mibewCleanUpAfterRequest();
window.setTimeout(mibewMakeRequest,mibewRequestTimeout);
}
function mibewCleanUpAfterRequest()
{
document.getElementsByTagName('head')[0].removeChild(document.getElementById('responseScript'));
}
function mibewDoLoadScript(url, id)
{
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
script.setAttribute('id', id);
document.getElementsByTagName('head')[0].appendChild(script);
return script;
}
function mibewLoadScript(id)
{
var script = mibewDoLoadScript(mibewRequestedScripts[id]['url'], id);
script.onload = function(){
mibewScriptReady(id);
}
script.onreadystatechange = function(){
if (this.readyState == 'complete' || this.readyState == 'loaded') {
mibewScriptReady(id);
}
}
}
function mibewScriptReady(id)
{
mibewRequestedScripts[id]['status'] = 'ready';
for(handlerName in mibewHandlers){
if(mibewCanRunHandler(handlerName)){
mibewHandlers[handlerName]();
delete mibewHandlers[handlerName];
}
}
}
function mibewCanRunHandler(handlerName)
{
var dependences = mibewHandlersDependences[handlerName];
for(var i = 0; i < dependences.length; i++){
if(mibewRequestedScripts[dependences[i]]['status'] != 'ready'){
return false;
}
}
return true;
}

View File

@ -465,6 +465,14 @@ function start_html_output()
header("Content-type: text/html" . (isset($charset) ? "; charset=" . $charset : ""));
}
function start_js_output(){
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/javascript; charset=utf-8");
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
}
function escape_with_cdata($text)
{
return "<![CDATA[" . str_replace("]]>", "]]>]]&gt;<![CDATA[", $text) . "]]>";

View File

@ -0,0 +1,31 @@
<?php
/*
* This file is part of Mibew Messenger project.
*
* Copyright (c) 2005-2011 Mibew Messenger Community
* All rights reserved. The contents of this file are subject to the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and
* is available at http://www.eclipse.org/legal/epl-v10.html
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License Version 2 or later (the "GPL"), in which case
* the provisions of the GPL are applicable instead of those above. If you wish
* to allow use of your version of this file only under the terms of the GPL, and
* not to allow others to use your version of this file under the terms of the
* EPL, indicate your decision by deleting the provisions above and replace them
* with the notice and other provisions required by the GPL.
*
* Contributors:
* Dmitriy Simushev - Cross-domain javascript requests implementation
*/
function build_js_response($response)
{
$result = array('load' => (isset($response['load'])?$response['load']:array()),
'handlers' => (isset($response['handlers'])?$response['handlers']:array()),
'dependences' => (isset($response['dependences'])?$response['dependences']:array()),
'data' => (isset($response['data'])?$response['data']:array()));
return "mibewOnResponse(" . json_encode($result) . ");";
}
?>

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of Mibew Messenger project.
*
* Copyright (c) 2005-2011 Mibew Messenger Community
* All rights reserved. The contents of this file are subject to the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and
* is available at http://www.eclipse.org/legal/epl-v10.html
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License Version 2 or later (the "GPL"), in which case
* the provisions of the GPL are applicable instead of those above. If you wish
* to allow use of your version of this file only under the terms of the GPL, and
* not to allow others to use your version of this file under the terms of the
* EPL, indicate your decision by deleting the provisions above and replace them
* with the notice and other provisions required by the GPL.
*
* Contributors:
* Dmitriy Simushev - Cross-domain javascript requests implementation
*/
require_once('libs/request.php');
$response = array();
start_js_output();
echo build_js_response($response);
exit;
?>