2013-02-16 99 views
0

我工作的一个Chrome扩展程序,提取元数据,并从网页某些文本元素访问框架的DOM。当信息包含在给定页面的框架中时,我遇到了挑战。使用javascript/jQuery的在Chrome扩展

就拿我的工作页面下面的示例框架(实际帧的名称,标题和来源为简洁改变)。

<frameset id="fs" border="0" frameborder="no"> 
    <frame src="/src1" name="frame1" noresize="noresize" scrolling="no" frameborder="0" title="FrameNumber1"> 
    <frame src="/src2" name="frame2" noresize="noresize" scrolling="no" frameborder="0" title="FrameNumber2"> 
    <frame src="/src3" name="frame3" noresize="noresize" scrolling="auto" frameborder="0" title="FrameNumber3"> 
    <frame src="/src4" name="frame4" noresize="noresize" scrolling="no" title="FrameNumber4"> 
</frameset> 

而且,考虑下面的HTML代码段用于FrameNumber3:(这是比较做作,只是为了例子的目的)

<html> 
<head> 
<title>FrameNumber3</title> 
</head> 
<body> 
<h1 id="heading">This is what I want</h1> 
</body> 
</html> 

我已经尝试了很多不同的方法,最他们围绕jquery选择器中正确的上下文参数进行不同的猜测。

我已经尝试了几十个,但这里的事情,我觉得其实可以工作的一些例子:

通过Run JQuery in the context of another frame

alert($('#heading', window.parent.frames[0].document).text()); 
//Uncaught TypeError: Cannot read property 'document' of undefined 

alert($('#heading', window.parent.frames[2].document).text()); 
//Tried this, because I want the 3rd frame's information 
//Uncaught TypeError: Cannot read property 'document' of undefined 

通过Accessing an element in a frame using a jQuery reference to the frame

alert($('#heading', $('frame').get(2).contentWindow.document).text()); 
//Uncaught TypeError: Cannot read property 'document' of undefined 

通过其他网页:

alert($('#heading', $('frame[title="FrameNumber3"]').document).text()); 
//no error but returns "undefined" 

alert($('#heading', $('frame[title="FrameNumber3"]').contents()).text()); 
//no error but returned "undefined" 

我不认为这是铬扩展中的权限问题,因为我已经处理好了。我认为这可能是我不了解如何进入框架DOM。我也读过jquery并不是最可靠的框架,所以我想这也可能是问题。

预先感谢任何意见或援助。

回答

0

也许这个问题的答案question会有所帮助。

document.querySelector('#SomeFrameSelector').contentWindow.document.body.onload