admin管理员组文章数量:1024625
I'm trying to write a Google Chrome extension.
The documentation and even sample code say that a background page can run JavaScript on the active tab using the chrome.tabs.executeScript
method, but chrome.tabs
is always undefined
when I break in the debugger.
This behavior is manifest in both my code and the Google sample code.
The Real Question: How do I run JavaScript on the active tab from a background page in a Chrome extension?
background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
debugger;
// chrome.tabs is undefined here
chrome.tabs.executeScript({
code: "console.log('hi')"
});
});
manifest.js:
{
"manifest_version": 2,
"name": "Hello World",
"description": "Says 'hello' to the world.",
"version": "0.1",
"permissions": ["tabs", "activeTab"],
"browser_action": {
"default_title": "hi"
},
"background": {
"scripts": ["background.js"]
}
}
Things I've tried:
- Setting the
persistent
flag onbackground
in the manifest file tofalse
,true
,"false"
, and"true"
- Including the
"tabs"
permission
The runtime throws this error when I try access chrome.tabs
:
Lazy require of tabs.binding did not set the binding field
I'm trying to write a Google Chrome extension.
The documentation and even sample code say that a background page can run JavaScript on the active tab using the chrome.tabs.executeScript
method, but chrome.tabs
is always undefined
when I break in the debugger.
This behavior is manifest in both my code and the Google sample code.
The Real Question: How do I run JavaScript on the active tab from a background page in a Chrome extension?
background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
debugger;
// chrome.tabs is undefined here
chrome.tabs.executeScript({
code: "console.log('hi')"
});
});
manifest.js:
{
"manifest_version": 2,
"name": "Hello World",
"description": "Says 'hello' to the world.",
"version": "0.1",
"permissions": ["tabs", "activeTab"],
"browser_action": {
"default_title": "hi"
},
"background": {
"scripts": ["background.js"]
}
}
Things I've tried:
- Setting the
persistent
flag onbackground
in the manifest file tofalse
,true
,"false"
, and"true"
- Including the
"tabs"
permission
The runtime throws this error when I try access chrome.tabs
:
Lazy require of tabs.binding did not set the binding field
-
2
This is a bug when you use
debugger;
statement. You don't need it so don't use it. Instead set a normal breakpoint. – woxxom Commented Aug 12, 2017 at 14:27 -
Wow. That's quite a bug. Removing the
debugger;
statement gets me past this issue on the sample code. I'm still having difficulty with my code, but I can figure that out. Thanks so much. – Mashmagar Commented Aug 12, 2017 at 14:44 - Reported: crbug./754976 – woxxom Commented Aug 12, 2017 at 15:39
1 Answer
Reset to default 6wOxxOm
's ment diagnosed this correctly:
This is a bug [in Chrome] when you use
debugger;
statement.
However, I've found it goes deeper than that. A breakpoint alone was sufficient to give me this issue. Thanks to this SO answer for pointing out that breakpoints can also cause this problem.
You have to be thorough to "clear out" the debugger. The debugger window must be closed when the extension is reloaded. Simply closing the debugger window without reloading was not enough to make the problem go away.
I'm trying to write a Google Chrome extension.
The documentation and even sample code say that a background page can run JavaScript on the active tab using the chrome.tabs.executeScript
method, but chrome.tabs
is always undefined
when I break in the debugger.
This behavior is manifest in both my code and the Google sample code.
The Real Question: How do I run JavaScript on the active tab from a background page in a Chrome extension?
background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
debugger;
// chrome.tabs is undefined here
chrome.tabs.executeScript({
code: "console.log('hi')"
});
});
manifest.js:
{
"manifest_version": 2,
"name": "Hello World",
"description": "Says 'hello' to the world.",
"version": "0.1",
"permissions": ["tabs", "activeTab"],
"browser_action": {
"default_title": "hi"
},
"background": {
"scripts": ["background.js"]
}
}
Things I've tried:
- Setting the
persistent
flag onbackground
in the manifest file tofalse
,true
,"false"
, and"true"
- Including the
"tabs"
permission
The runtime throws this error when I try access chrome.tabs
:
Lazy require of tabs.binding did not set the binding field
I'm trying to write a Google Chrome extension.
The documentation and even sample code say that a background page can run JavaScript on the active tab using the chrome.tabs.executeScript
method, but chrome.tabs
is always undefined
when I break in the debugger.
This behavior is manifest in both my code and the Google sample code.
The Real Question: How do I run JavaScript on the active tab from a background page in a Chrome extension?
background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
debugger;
// chrome.tabs is undefined here
chrome.tabs.executeScript({
code: "console.log('hi')"
});
});
manifest.js:
{
"manifest_version": 2,
"name": "Hello World",
"description": "Says 'hello' to the world.",
"version": "0.1",
"permissions": ["tabs", "activeTab"],
"browser_action": {
"default_title": "hi"
},
"background": {
"scripts": ["background.js"]
}
}
Things I've tried:
- Setting the
persistent
flag onbackground
in the manifest file tofalse
,true
,"false"
, and"true"
- Including the
"tabs"
permission
The runtime throws this error when I try access chrome.tabs
:
Lazy require of tabs.binding did not set the binding field
-
2
This is a bug when you use
debugger;
statement. You don't need it so don't use it. Instead set a normal breakpoint. – woxxom Commented Aug 12, 2017 at 14:27 -
Wow. That's quite a bug. Removing the
debugger;
statement gets me past this issue on the sample code. I'm still having difficulty with my code, but I can figure that out. Thanks so much. – Mashmagar Commented Aug 12, 2017 at 14:44 - Reported: crbug./754976 – woxxom Commented Aug 12, 2017 at 15:39
1 Answer
Reset to default 6wOxxOm
's ment diagnosed this correctly:
This is a bug [in Chrome] when you use
debugger;
statement.
However, I've found it goes deeper than that. A breakpoint alone was sufficient to give me this issue. Thanks to this SO answer for pointing out that breakpoints can also cause this problem.
You have to be thorough to "clear out" the debugger. The debugger window must be closed when the extension is reloaded. Simply closing the debugger window without reloading was not enough to make the problem go away.
本文标签: javascriptWhy is chrometabs undefined in the background pageStack Overflow
版权声明:本文标题:javascript - Why is chrome.tabs undefined in the background page? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745614366a2159197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论