谷歌铬扩展:: console.log()从后台页面? [英] google chrome extension :: console.log() from background page?

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

问题描述

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



然而,由于背景页面并未直接从弹出页面运行,因此它不包含在控制台中。



有没有办法让我可以在后台页面中获取 console.log()的内容以显示在控制台中的弹出页面?



<有没有办法从背景页面调用弹出页面中的函数? 任何扩展名(<内容脚本除外)可以通过页面直接访问后台页面< a href =http://developer.chrome.com/extensions/extension.html#method-getBackgroundPage =noreferrer> chrome.extension.getBackgroundPage()



这意味着,在弹出页面,你可以这样做:

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

使用起来更方便:

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

现在,如果您想在内容脚本,您必须使用消息传递来实现这一点。原因是,他们都属于不同的领域,这是有道理的。在消息传递页面中有许多示例供您查看。



希望清除所有内容。


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.

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?

解决方案

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');

To make it easier to use:

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.

Hope that clears everything.

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

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