2013-11-15 109 views
-1

我知道这个问题有一个简单的解决方案,但相信我现在找不到它。无论如何,我的问题是,我必须从iframe中拖动图像(内容来自另一个域)并放入图像占位符。该演示是在这里跨域iframe拖放

http://popupbuilder.webege.com/IframeDragDropadil/

的形象应该是拖动到盒子里,坐在那里很好,只有当I帧的负荷来自同一个域的内容。但是,如果我从另一个域加载内容,就像我在演示中所做的那样(^链接已在上面提供),则整个事情都不起作用。

现在我知道这是一个跨域问题。您不能仅仅将来自另一个域的数据操作到iframe中。很公平!!

所以我遇到了另一个想法,即通过PHP将整个页面复制到您的服务器,然后将其提供给iframe。这听起来很有前途,但PHP功能

<?php file_get_contents(); ?> 

只返回html而不是完整的网页。再次,这将杀死整个可用性的概念,因为用户不知道拖拽什么。

最后一件事,如果我将图像从另一个浏览器窗口拖放到此占位符,它的工作原理!我不知道iframe引起了什么问题,以及如何解决这个问题。

使用PHP在本地服务器上复制整个网站可能是一个解决方案,但我看不出如何!

+0

如果从其他浏览器拖动它,你实际上是从用户的Internet临时文件拖动。 iframe的问题是iframe和父页面无法相互通信。在iframe中复制整个站点是一个糟糕的主意,你会遇到无数的问题,从资产包括动态数据到网站正在做的Ajax请求。你最好是将图像从图像上刮下来,然后将图像呈现给用户,但即使这样也有其局限性(例如,用ajax插入的图像,用php解析javascript的好运气) –

+0

是否有可能我绑定带有iframe的JQuery事件。我再次猜测,跨域问题会产生问题,并阻止我将任何事件绑定到此iframe的元素。 –

+0

由于交叉来源的问题,JavaScript无法触及iframe,除了iframe本身的src属性和css样式外。 –

回答

0

经过几天的努力,我已经想出了一个解决方案,它实际上可以帮助我们将事件绑定到iframe的元素,即使我们打开另一个网站(属于另一个域的网站)也是如此。这里最好的办法是将完整的网站代理到我们自己的服务器上,并在Iframe中打开它。现在我们可以轻松地绑定事件并操纵本网站。

请注意,这种技术只有70-80%准确。我创建的PHP脚本可能无法正常打开网站。有时由代理站点进行的AJAX调用还没有完成。但该脚本确实提供了一个很好的出发点和其他开发人员,欢迎来扩展它:)

<?php 

$opts = array('http' => 
    array(
     'method' => 'GET', 
     //'user_agent ' => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2) Gecko/20100301 Ubuntu/9.10 (karmic) Firefox/3.6", 
     'header' => array(
      'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8 
' 
     ), 
    ) 
); 

    function is_url_exist($url){ 
      $ch = curl_init($url); 
      $status = false; 
      curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); 
      curl_setopt($ch, CURLOPT_TIMEOUT, 120); 
      curl_setopt($ch, CURLOPT_NOBODY, true); 
      curl_exec($ch); 
      $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

      if($code == 200){ 
       $status = true; 
      }else{ 
       $status = false; 
      } 
      curl_close($ch); 
      return $status; 
     } 
    function get_web_page($url) 
    { 
     $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0'; 

     $options = array(

      CURLOPT_CUSTOMREQUEST =>"GET",  //set request type post or get 
      // CURLOPT_HTTPGET => true, 
      CURLOPT_POST   =>false,  //set to GET 
     // CURLOPT_USERAGENT  => $user_agent, //set user agent 
      CURLOPT_COOKIEFILE  =>"cookie.txt", //set cookie file 
      CURLOPT_COOKIEJAR  =>"cookie.txt", //set cookie jar 
      CURLOPT_RETURNTRANSFER => true,  // return web page 
      //CURLOPT_HEADER   => true, // don't return headers 

      CURLOPT_FOLLOWLOCATION => true,  // follow redirects 
      CURLOPT_ENCODING  => "",  // handle all encodings 
      CURLOPT_AUTOREFERER => true,  // set referer on redirect 
      CURLOPT_CONNECTTIMEOUT => 120,  // timeout on connect 
      CURLOPT_TIMEOUT  => 120,  // timeout on response 
      CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 
     ); 

     $ch  = curl_init($url); 
     curl_setopt_array($ch, $options); 
     $content = curl_exec($ch); 
     $err  = curl_errno($ch); 
     $errmsg = curl_error($ch); 
     $header = curl_getinfo($ch); 
     curl_close($ch); 

     $header['errno'] = $err; 
     $header['errmsg'] = $errmsg; 
     $header['content'] = $content; 
     return $content; 
    } 

    function GetDomainFromAddress($url){ 
     $parse = parse_url($url); 
     $scheme = $parse['scheme']; 
     $URL = $parse['host']; // prints 'domain.com' 
     $URL = $scheme.'://'.$URL.'/'; 
     return $URL; 
    } 

    /*All functions end here*/ 

    $url=$_GET["url"]; 
// $url="http://www.topshop.com/en/tsuk/product/new-in-this-week-2169932/new-in-this-week-493/blue-pleat-laceup-kilt-2441152?bi=1&ps=20"; 
    $input= get_web_page($url); 

$domain = $url; 
$doc = new DOMDocument; 
    libxml_use_internal_errors(true); 
    $doc->loadHTML($input); 
    $tags = $doc->getElementsByTagName('img'); 

    if(count($tags) > 0) 
    { 
     foreach ($tags as $tag) { 

      $src=$tag->getAttribute('src'); 

      if(!empty($src)) 
      { 
       if(strpos($src,"https")!==false||strpos($src,'//')!==false || strpos($src,"http")!==false) 
       {     
       } 
       else 
       { 
        $src1 = null; 
        if(!is_url_exist($src)) 
        { 
         $domain = GetDomainFromAddress($url); 
         $src1=$domain.$src; 
        } 
        $src=$src1; 
       } 
      } 


      $tag->setAttribute('src', $src); 

     } 
    } 

    $tags = $doc->getElementsByTagName('link'); 
    $i=0; 
    if(count($tags) > 0) 
    { 
    foreach ($tags as $tag) { 


      $src=$tag->getAttribute('href'); 

      if(!empty($src)) 
      { 
       if(strpos($src,"https")!==false||strpos($src,'//')!==false || strpos($src,"http")!==false) 
       {     
       } 
       else 
       { 
        $src1 = null; 
        if(!is_url_exist($src)) 
        { $domain = GetDomainFromAddress($url); 
         $src1=$domain.$src; 
        } 
        $src=$src1; 
       } 
      } 

      $tag->setAttribute('href', $src); 

     } 
    } 
     $tags = $doc->getElementsByTagName('script'); 

    if(count($tags) > 0) 
    { 
    foreach ($tags as $tag) { 


      $src=$tag->getAttribute('src'); 

      if(!empty($src)) 
      { 
       if(strpos($src,"https")!==false||strpos($src,'//')!==false || strpos($src,"http")!==false) 
       {     
       } 
       else 
       { 
        $src1 = null; 
        if(!is_url_exist($src)) 
        { $domain = GetDomainFromAddress($url); 
         $src1=$domain.$src; 
        } 
        $src=$src1; 
       } 
      } 

      $tag->setAttribute('src', $src); 

     } 
    } 
echo $doc->saveHTML(); 
?>