2011-11-24 46 views
1

Coffeescript以匿名封闭方式封装每个文件。当我想写一个全球性的功能,我必须把它分配给窗口:如何在开发chrome扩展时共享coffeescript中的跨文件功能?

func = -> alert("hello world") 
windows.func = func 

或Node.js的,出口:

func = -> alert("hello world") 
export.func = func 

但如何对Chrome扩展程序?如何将后台页面中的功能分享给其他页面?

回答

2

你不能像这样共享函数/数据,但我想你可以在两个页面中加载相同的代码,如果你只需要运行某些功能而不共享数据。如果你需要与你的背景页沟通,你需要使用正确的短信功能,请参阅:

how can i use the port.postmessage to send info from the background page to the content script in a google chrome extension

+0

如何“加载相同的代码”? JavaScript是否提供负载或需求功能? –

+0

无论如何,我使用消息传递来解决我的问题。谢谢你的回答。 –

0

您可以在后台页面使用chrome.extension.getBackgroundPage() API来访问功能。例如:

var bp = chrome.extension.getBackgroundPage(); 
bp.func(); 
+0

看起来像我纠正。感谢您的信息和链接。 –