admin管理员组文章数量:1222019
I'm building a Chrome extension that allows the user to manage open tabs for an application (website). The manifest is:
{
"manifest_version": 2,
"name": "AT Tabs",
"version": "0.1",
"permissions": ["activeTab", "tabs"],
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["main.js"]
}]
}
But when I do this in the main.js file:
console.log(chrome.windows);
I get undefined in the console... Any ideas why? I have both tabs and activeTab as permissions and the extension is being run in the developer mode.
I'm building a Chrome extension that allows the user to manage open tabs for an application (website). The manifest is:
{
"manifest_version": 2,
"name": "AT Tabs",
"version": "0.1",
"permissions": ["activeTab", "tabs"],
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["main.js"]
}]
}
But when I do this in the main.js file:
console.log(chrome.windows);
I get undefined in the console... Any ideas why? I have both tabs and activeTab as permissions and the extension is being run in the developer mode.
Share Improve this question edited Feb 26, 2015 at 16:51 Sarah Elan 2,4611 gold badge25 silver badges45 bronze badges asked Feb 26, 2015 at 16:30 CameronCameron 28.8k102 gold badges288 silver badges490 bronze badges1 Answer
Reset to default 21chrome.windows
will not be available in your main.js
because it is an injected content script.
Only your background/event pages JavaScript has access to chrome.windows
. You will need to use message passing from your content script to your background script to trigger the window actions you want.
For instance, to create a window from an content script, your extension may look something like this:
Manifest:
{
...
"background": {
"scripts": ["eventPage.js"],
"persistent": false
},
...
}
main.js:
chrome.runtime.sendMessage({
action: 'createWindow',
url: 'http://google.com'
},
function(createdWindow) {
console.log(createdWindow);
});
eventPage.js:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request && request.action === 'createWindow' && request.url) {
chrome.windows.create({url: request.url}, function (win) {
sendResponse(win);
});
}
});
本文标签: javascriptchromewindows undefined for Chrome ExtensionStack Overflow
版权声明:本文标题:javascript - chrome.windows undefined for Chrome Extension - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739268766a2155720.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论