如何在Chrome扩展程序的bg页面中访问弹出页面DOM? [英] How do I access the popup page DOM from bg page in Chrome extension?

查看:166
本文介绍了如何在Chrome扩展程序的bg页面中访问弹出页面DOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Chrome浏览器的扩展开发者部分中,

In Google Chrome's extension developer section, it says

扩展内的HTML页面 可以完全访问彼此的 DOM,它们可以在上调用函数 彼此. ...弹出窗口的内容 是由HTML文件定义的网页 (popup.html).弹出窗口不需要 复制其中的代码 背景页面(background.html) 因为弹出窗口可以调用函数 在背景页面上

The HTML pages inside an extension have complete access to each other's DOMs, and they can invoke functions on each other. ... The popup's contents are a web page defined by an HTML file (popup.html). The popup doesn't need to duplicate code that's in the background page (background.html) because the popup can invoke functions on the background page

我已经加载并测试了jQuery,并且可以使用jQuery访问background.html中的DOM元素,但是我无法弄清楚如何从background.html中访问popup.html中的DOM元素.

I've loaded and tested jQuery, and can access DOM elements in background.html with jQuery, but I cannot figure out how to get access to DOM elements in popup.html from background.html.

推荐答案

您能讨论为什么要这么做吗?背景页面是在扩展程序的生命周期内永久存在的页面.虽然弹出页面仅在您单击弹出窗口时存在.

can you discuss why you would want to do that? A background page is a page that lives forever for the life time of your extension. While the popup page only lives when you click on the popup.

在我看来,应该以另一种方式对其进行重构,您的弹出窗口应该从后台页面请求某些内容.您只需在弹出窗口中执行此操作即可访问后台页面: chrome.extension.getBackgroundPage()

In my opinion, it should be refactored the other way around, your popup should request something from the background page. You just do this in the popup to access the background page: chrome.extension.getBackgroundPage()

但是,如果您坚持要使用扩展名页面,则可以使用 sendRequest() onRequest .也许您可以使用 chrome.extension.getViews

But if you insist, you can use simple communication with extension pages with sendRequest() and onRequest. Perhaps you can use chrome.extension.getViews

这篇关于如何在Chrome扩展程序的bg页面中访问弹出页面DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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