我正在尝试将currrent活动标签信息从后台脚本发送到弹出页面,但是我得到了#34; undefined"这样做的时候。
后台脚本:
//load popup.html in a pop-up window.
chrome.browserAction.onClicked.addListener(function() {
chrome.windows.create({'url': 'HTML/popup.html', 'type': 'popup'},
function(window) {
});
});
//return current active tab
function backgroundFunction () {
var tab = "";
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
tab = tabs[0].title;
});
return tab;
}
popup.js:
(function () {
var otherWindows = chrome.extension.getBackgroundPage();
console.log(otherWindows.backgroundFunction());
})();
我的清单文件:
{
"manifest_version": 2,
"name": "Application Test",
"version": "1.0",
"description": "Test Application",
"icons": {
"128": "icons/icon128.png",
"48": "icons/icon48.png",
"16": "icons/icon16.png"
},
"browser_action":{
"default_icon": "icons/icon16.png"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs",
"<all_urls>",
"activeTab"
]
}
我怀疑它是未定义的,因为后台脚本选项卡现在是&#34;未定义&#34;因为弹出窗口。
当他们点击Extension图标时,如何成功地将用户所在URL的标签信息发送到popup.js文件?
答案 0 :(得分:0)
使用chrome.runtime
,大多数方法已在chrome.extension
chrome.runtime.getBackgroundPage(backgroundWindow => {
console.log(backgroundWindow.backgroundFunction());
});