使用内容脚本获取选项卡的DOM [英] Get tab's DOM using content script

查看:86
本文介绍了使用内容脚本获取选项卡的DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在popup.html中有一个脚本:

 < / body> 
< script src =popup.js>< / script>
< / html>

通过它我试图获取特定选项卡的DOM。更具体地说,我试图确定特定选项卡中当前页面上特定元素的存在,然后在popup.js中使用此信息。我怎么会这样做?

解决方案

您无法从弹出窗口访问当前页面的DOM。你需要一个内容脚本。首先设置这三个脚本:
$ b


  • 弹出脚本

  • 后台页面脚本

  • 内容脚本 $ b

    您可以使用后台脚本作为代理在弹出脚本和内容脚本之间发送消息。阅读关于消息传递的此处



    您可以使用执行脚本来简化过程而不是内容脚本。尽管您仍然需要弹出脚本和背景页面之间的通信。


    I have a script in my popup.html:

     </body>
     <script src="popup.js"></script>
     </html>
    

    Through which I am trying to obtain the DOM of a particular tab. More specifically, I am attempting to determine the existence of a particular element on the current page in a specific tab and then use this information within popup.js. How would I go about doing this?

    解决方案

    You can't access current page DOM from a popup. You need a content script for that. Start by setting up these three scripts:

    With these you can send messages between popup script and content script using background script as a proxy. Read about message passing here.

    You can simplify the process a bit using executeScript instead a content script. You will still need a communication between popup script and a background page though.

    这篇关于使用内容脚本获取选项卡的DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆