2011-11-24 44 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

看起來像我糾正。感謝您的信息和鏈接。 –