클릭

2011-12-18 5 views
0

가능한 중복 :
Get xpath location of element in iframe(iframe from my domain)클릭

나는 결과 (getXPath)와 같은 XPath를 얻을 수 아래의 코드가 할 수있는 자바 스크립트 함수가 있습니다. 또한 iframe에 외부 사이트가 있지만 프록시를 사용할 때 그를 보여줍니다 (코드는 아래 참조). 그리고 iframe에서 일부 요소를 클릭 할 때 xpath를 표시하는 jquery 함수가 있지만 작동하지 않습니다. 문제가 무엇 :

CODE :

<?php 

error_reporting(E_ALL^E_NOTICE); 

$url = $_GET['url']; 

if(! empty($url)) 
{ 
    $data = file_get_contents($url); 

    $data = str_replace('<head>', '<head><base href="'.$url.'" /></base>', $data); 

    $data = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $data); 
    $data = preg_replace('#<iframe(.*?)></iframe>#is', '', $data); 

    $data .= 
    ' 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script> 
    $("div").each(function(i){ 
     if($(this).css("position") == "fixed") $(this).css("display", "none"); 
    }); 
    </script> 
    ' 
    ; 

    die($data); 
} 

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <meta name="author" content="Webarto" /> 

    <title>AdriaMart</title> 

<script src="http://code.jquery.com/jquery-latest.js"></script> 
//function for xpath location 
<script type="text/javascript"> 
    function getXPath(node, path) { 
     path = path || []; 
     if(node.parentNode) { 
      path = getXPath(node.parentNode, path); 
     } 

     if(node.previousSibling) { 
      var count = 1; 
      var sibling = node.previousSibling 
      do { 
      if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {count++;} 
      sibling = sibling.previousSibling; 
      } while(sibling); 
      if(count == 1) {count = null;} 
     } else if(node.nextSibling) { 
      var sibling = node.nextSibling; 
      do { 
      if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) { 
       var count = 1; 
       sibling = null; 
      } else { 
       var count = null; 
       sibling = sibling.previousSibling; 
      } 
      } while(sibling); 
     } 

     if(node.nodeType == 1) { 
      path.push(node.nodeName.toLowerCase() + (node.id ? "[@id='"+node.id+"']" : count > 0 ? "["+count+"]" : '')); 
     } 
     return path; 
     }; 

</script> 

//function to get xpath location and write in textfield in focus 
***<script> 

    $('#iframe').ready(function() { 
     var selectedtextbox; 
$('input[name="myinput"]').focus(function(){selectedtextbox=$(this);}); 
     $('div, p, li, a, href').click(function() { 
      var xpath = getXPath(this); 
      selectedtextbox.val(xpath) 
     }); 
    }); 
</script>*** 
<style type="text/css"> 
<!-- 
iframe{width:100%;height:400px;} 
--> 
</style> 

</head> 
<body> 

<!-- ... --> 
<input id="iframe_url" name="" type="text" /> 
<input id="iframe_button" name="" type="button" /> 
<iframe id="iframe" src="?url=http://kupime.com"></iframe> 
<script type="text/javascript"> 
    document.getElementById('iframe_button').onclick = function() { 
    document.getElementById('iframe').src = '?url=' + document.getElementById('iframe_url').value; 
    }; 
</script> 
<!-- ... --> 
//Textfields for xpath location to write inside them 
<input id="" name="myinput" type="text" value=""> 
<input id="" name="myinput" type="text" value=""> 
<input id="" name="myinput" type="text" value=""> 



</body> 
</html> 

***

$('#iframe').ready(function() { 
     var selectedtextbox; 
$('input[name="myinput"]').focus(function(){selectedtextbox=$(this);}); 
     $('div, p, li, a, href').click(function() { 
      var xpath = getXPath(this); 
      selectedtextbox.val(xpath) 
     }); 
    }); 
</script>*** 
+0

문제는 $ (#은 iframe ') 위의 jQuery 코드입니다. 준비 ... ... 여기에 오류가 어디 있습니까 !!! –

답변

1

3 문제가 있습니다.

    iframe이 요소는이 시점에서 액세스 할 수 없습니다
  1. 은 (이 함수는 #은 iframe 요소는 여전히 알 수 iframe을 위에 배치됩니다)
  2. 현재 문서가 준비되지 않을 때 발생합니다

    준비 iframe 내 문서 사용 부하가 대신

  3. 당신이 상황에 인수로 iframe 내에 문서를 설정해야


//wait for ready in the current document to have access to #iframe 
$(function() 
    { 
    $('#iframe') 
     //observe the load-event of the iframe-element 
     .on('load', 
      function() 
      { 
      //set a default-input to avoid errors 
      var selectedtextbox=$('input[name="myinput"]:eq(0)'); 
      $('input[name="myinput"]') 
       .focus(function(){selectedtextbox=$(this);}); 
      //Note the 2nd argument here, 
      //it sets the contextof $() to the document inside the iframe 
      $('div, p, li, a, href', 
       this.contentWindow.document) 
       .click(function() 
         { 
          var xpath = getXPath(this); 
          selectedtextbox.val(xpath) 
         }); 
      }); 
    });