简体   繁体   English

从 iframe 内的元素获取文本

[英]Get text from an element inside an iframe

Well I wanted to get the text from a tag element inside an iframe, what I mean is that I would like to click any element inside said iframe and be able to retrieve the text of said tag back to the parent window just like this code does with my current page:好吧,我想从 iframe 内的标签元素中获取文本,我的意思是我想单击所述 iframe 内的任何元素,并且能够像这段代码一样将所述标签的文本检索回父窗口使用我当前的页面:

$(document).click(function(event) {
    var text = $(event.target).text();
});

PS: The website contained in the iFrame is not under my control basically I want to web scrape the website but was trying to create a tool to ease the process PS:iFrame 中包含的网站基本上不受我控制

So in order to communicate between the iframe and parent window, either the content of the iframe has to have been generated by the JavaScript (not sure if it applies if it's an iframe of another file on the same server as well...) Or you have to otherwise have access to the source code of the iframe page, to implement the postMessage API to send data to and from the main page因此,为了在 iframe 和父窗口之间进行通信,iframe 的内容必须由 JavaScript 生成(不确定它是否适用于同一服务器上另一个文件的 iframe ......)或者您必须以其他方式访问 iframe 页面的源代码,以实现 postMessage API 以将数据发送到主页或从主页发送数据

For example, somewhere in your iframe source code, like on the click of an element, you can call postMessage to send a message to the main frame例如,在您的 iframe 源代码中的某处,例如单击某个元素时,您可以调用 postMessage 向主框架发送消息

myButton.onclick= ()=>{
    parent.postMessage({message:"you clicked something"},"*")
}

Then in the main file you listen for new messages然后在主文件中收听新消息

onmessage = e=> 
    console.log(e.data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM