Use iframe for crossite loading instead of CORS header
authorFabio Comuni <fabrix.xm@gmail.com>
Wed, 19 Oct 2011 09:43:45 +0000 (11:43 +0200)
committerFabio Comuni <fabrix.xm@gmail.com>
Wed, 19 Oct 2011 09:43:45 +0000 (11:43 +0200)
widgets/widget_friends.php
widgets/widget_like.php
widgets/widget_like.tpl
widgets/widgets.js
widgets/widgets.php

index fc7d7aa9846418c012b43d46251e3ed0ee9ea6ee..6b1cf18c44c12b875dafe179a01d1e0eca6ffd18 100644 (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());
index b60d2d94d9ed483c8c1e509bc96828ace2db3b3f..649d4a767ec1b4d65311dc83ae304953c614a731 100644 (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']);
        
index e43e7a540ad6f21938e28090766dca9062f10ae8..3c26d1da3c92dca5605c52bd9270f71259879adb 100644 (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>
index e6382d711d8ab403346fb16eb1ae908c3e7c40ed..f7b7ac9e9aa39955cea0fcd9ae2b8175d2972dff 100644 (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>");
index cedaea34b1ca7167d9a99ca00bfcb22e6cce3be2..226a34c060f88c519469c832f1a1f209172e44e0 100644 (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],
                ));