谷歌浏览器扩展 :: console.log() 来自后台页面? [英] google chrome extension :: console.log() from background page?

查看:31
本文介绍了谷歌浏览器扩展 :: console.log() 来自后台页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从弹出页面调用 console.log('something'); 或任何包含它的脚本,它可以正常工作.

If I call console.log('something'); from the popup page, or any script included off that it works fine.

然而,由于后台页面不是直接从弹出页面运行,所以它不包含在控制台中.

However as the background page is not directly run off the popup page it is not included in the console.

有没有办法让后台页面中的 console.log() 显示在弹出页面的控制台中?

Is there a way that I can get console.log()'s in the background page to show up in the console for the popup page?

有没有什么办法,从后台页面调用弹出页面中的一个函数?

is there any way to, from the background page call a function in the popup page?

推荐答案

任何扩展页面(除了内容脚本) 可以通过 chrome.extension.getBackgroundPage().

Any extension page (except content scripts) has direct access to the background page via chrome.extension.getBackgroundPage().

这意味着,在弹出页面中,您可以:

That means, within the popup page, you can just do:

chrome.extension.getBackgroundPage().console.log('foo');

为了更容易使用:

var bkg = chrome.extension.getBackgroundPage();
bkg.console.log('foo');

现在,如果您想在 内容脚本 中执行相同操作,您必须使用 消息传递 来实现这一点.究其原因,它们都属于不同的领域,这是有道理的.消息传递页面中有很多示例供您查看.

Now if you want to do the same within content scripts you have to use Message Passing to achieve that. The reason, they both belong to different domains, which make sense. There are many examples in the Message Passing page for you to check out.

希望能清除一切.

这篇关于谷歌浏览器扩展 :: console.log() 来自后台页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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