Use iframe for crossite loading instead of CORS header

This commit is contained in:
Fabio Comuni 2011-10-19 11:43:45 +02:00
parent 255685c176
commit a3eb7268ae
5 changed files with 39 additions and 49 deletions

View File

@ -11,6 +11,11 @@ function friends_widget_args(){
return Array();
}
function friends_widget_size(){
return Array('100%','200px');
}
function friends_widget_content(&$a, $conf){
$r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile`
@ -23,12 +28,12 @@ function friends_widget_content(&$a, $conf){
$o = "";
$o .= "<style>
.f9k_widget {font-size: 0.8em;}
.f9k_widget #contact-block { overflow: hidden; height: auto; }
.f9k_widget .contact-block-h4 { float: left; margin: 0px; }
.f9k_widget .allcontact-link { float: right; margin: 0px; }
.f9k_widget .contact-block-content { clear:both; }
.f9k_widget .contact-block-div { display: block !important; float: left!important; width: 50px!important; height: 50px!important; margin: 2px!important;}
body {font-size: 0.8em; margin: 0px; padding: 0px;}
#contact-block { overflow: hidden; height: auto; }
.contact-block-h4 { float: left; margin: 0px; }
.allcontact-link { float: right; margin: 0px; }
.contact-block-content { clear:both; }
.contact-block-div { display: block !important; float: left!important; width: 50px!important; height: 50px!important; margin: 2px!important;}
</style>";
$o .= _abs_url(contact_block());

View File

@ -11,6 +11,11 @@ function like_widget_args(){
return Array("KEY");
}
function like_widget_size(){
return Array('60px','20px');
}
function like_widget_content(&$a, $conf){
$args = explode(",",$_GET['a']);

View File

@ -1,2 +1,3 @@
<style>body {font-size: 0.8em; margin: 0px; padding: 0px;}</style>
<span class='f9k_like' title="$strlike">$like <img src="$baseurl/images/like.gif" alt="like"/></span>
<span class='f9k_dislike' title="$strdislike">$dislike <img src="$baseurl/images/dislike.gif" alt="dislike"/></span>

View File

@ -1,26 +1,16 @@
/**
* @author Fabio Comuni
*/
var f9a_widget_$widget_id = {
width: "$width",
height: "$height",
entrypoint : "$entrypoint",
key : "$key",
widgetid: "$widget_id",
argstr: "$args",
xmlhttp : null,
getXHRObj : function(){
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
this.xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
},
dorequest : function(args, cb) {
if (args===null) args = new Array();
load : function() {
var args = new Array();
args['k']=this.key;
args['s']=window.location.href;
args['a']=this.argstr;
@ -31,36 +21,17 @@ var f9a_widget_$widget_id = {
}
var url = this.entrypoint + "?"+ urlencodedargs.join("&");
this.xmlhttp.open("GET", url ,true);
this.xmlhttp.send();
this.xmlhttp.obj = this;
this.xmlhttp.onreadystatechange=function(){
if (this.readyState==4){
if (this.status==200) {
cb(this.obj, this.responseText);
} else {
document.getElementById(this.obj.widgetid).innerHTML="Error loading widget.";
}
}
}
},
requestcb: function(obj, responseText) {
document.getElementById(obj.widgetid).innerHTML=responseText;
},
load : function (){
this.getXHRObj();
this.dorequest(null, this.requestcb);
console.log(this.widgetid);
console.log(document.getElementById(this.widgetid));
document.getElementById(this.widgetid).innerHTML = '<iframe style="border:0px; width: '+this.width+'; height:'+this.height+'" src="'+url+'"></iframe>';
}
};
document.writeln("<div id='$widget_id' class='f9k_widget $type'>");
document.writeln("<img id='$widget_id_ld' src='$loader'>");
document.writeln("</div>");
(function() {
f9a_widget_$widget_id.load();
})();
document.writeln("<div id='$widget_id' class='f9k_widget'>");
document.writeln("<img id='$widget_id_ld' src='$loader'>");
document.writeln("</div>");

View File

@ -72,6 +72,10 @@ function _abs_url($s){
return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
}
function _randomAlphaNum($length){
return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
}
function widgets_content(&$a) {
@ -106,7 +110,7 @@ function widgets_content(&$a) {
//echo "<pre>"; var_dump($a->argv); die();
if ($a->argv[2]=="cb"){
header('Access-Control-Allow-Origin: *');
/*header('Access-Control-Allow-Origin: *');*/
$o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
} else {
@ -126,15 +130,19 @@ function widgets_content(&$a) {
}
$widget_size = call_user_func($a->argv[1].'_widget_size');
$script = file_get_contents(dirname(__file__)."/widgets.js");
$o .= replace_macros($script, array(
'$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
'$key' => $conf['key'],
'$widget_id' => 'f9a_'.$a->argv[1]."_". ceil(microtime(true)*100),
'$widget_id' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
'$loader' => $a->get_baseurl()."/images/rotator.gif",
'$args' => (isset($_GET['a'])?$_GET['a']:''),
'$width' => $widget_size[0],
'$height' => $widget_size[1],
'$type' => $a->argv[1],
));