admin管理员组文章数量:1023091
I am using executeScript
to run in the current active tab. But inside its callback function I want to send a message to the script being executed...
chrome.tabs.executeScript(null, {
file: 'src/js/scripts/extractCSS.js'
}, function() {
chrome.tabs.sendMessage(this.props.source);
this.props.source
is an object I am trying to pass. And inside src/js/scripts/extractCSS.js
I am trying to catch the message...
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
console.log(message);
});
However I get the following error...
Error in response to tabs.executeScript: Error: Invocation
of form tabs.sendMessage(object) doesn't match definition
tabs.sendMessage(integer tabId, any message, optional object
options, optional function responseCallback)
From what I gather, I need to define tabId
, but I just need to send the message to the active tab. I tried adding null
for tabId
but it still gives me an error.
How can I fix this?
I am using executeScript
to run in the current active tab. But inside its callback function I want to send a message to the script being executed...
chrome.tabs.executeScript(null, {
file: 'src/js/scripts/extractCSS.js'
}, function() {
chrome.tabs.sendMessage(this.props.source);
this.props.source
is an object I am trying to pass. And inside src/js/scripts/extractCSS.js
I am trying to catch the message...
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
console.log(message);
});
However I get the following error...
Error in response to tabs.executeScript: Error: Invocation
of form tabs.sendMessage(object) doesn't match definition
tabs.sendMessage(integer tabId, any message, optional object
options, optional function responseCallback)
From what I gather, I need to define tabId
, but I just need to send the message to the active tab. I tried adding null
for tabId
but it still gives me an error.
How can I fix this?
Share Improve this question asked Nov 16, 2016 at 19:57 buydadipbuydadip 9,45722 gold badges93 silver badges160 bronze badges1 Answer
Reset to default 6Even though its a active tab, you will have to pass the tabId
. chrome.tabs.query
can be used to get the tabs. You can do it the following way:
chrome.tabs.query(
{ currentWindow: true, active: true },
function (tabArray) {
chrome.tabs.executeScript(tabArray[0].id, {
file: 'src/js/scripts/extractCSS.js'
}, function() {
chrome.tabs.sendMessage(this.props.source);
})
}
);
As there can be only one active tab in current window, tabArray
will have one element only and then id
attribute can be accessed.
I am using executeScript
to run in the current active tab. But inside its callback function I want to send a message to the script being executed...
chrome.tabs.executeScript(null, {
file: 'src/js/scripts/extractCSS.js'
}, function() {
chrome.tabs.sendMessage(this.props.source);
this.props.source
is an object I am trying to pass. And inside src/js/scripts/extractCSS.js
I am trying to catch the message...
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
console.log(message);
});
However I get the following error...
Error in response to tabs.executeScript: Error: Invocation
of form tabs.sendMessage(object) doesn't match definition
tabs.sendMessage(integer tabId, any message, optional object
options, optional function responseCallback)
From what I gather, I need to define tabId
, but I just need to send the message to the active tab. I tried adding null
for tabId
but it still gives me an error.
How can I fix this?
I am using executeScript
to run in the current active tab. But inside its callback function I want to send a message to the script being executed...
chrome.tabs.executeScript(null, {
file: 'src/js/scripts/extractCSS.js'
}, function() {
chrome.tabs.sendMessage(this.props.source);
this.props.source
is an object I am trying to pass. And inside src/js/scripts/extractCSS.js
I am trying to catch the message...
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
console.log(message);
});
However I get the following error...
Error in response to tabs.executeScript: Error: Invocation
of form tabs.sendMessage(object) doesn't match definition
tabs.sendMessage(integer tabId, any message, optional object
options, optional function responseCallback)
From what I gather, I need to define tabId
, but I just need to send the message to the active tab. I tried adding null
for tabId
but it still gives me an error.
How can I fix this?
Share Improve this question asked Nov 16, 2016 at 19:57 buydadipbuydadip 9,45722 gold badges93 silver badges160 bronze badges1 Answer
Reset to default 6Even though its a active tab, you will have to pass the tabId
. chrome.tabs.query
can be used to get the tabs. You can do it the following way:
chrome.tabs.query(
{ currentWindow: true, active: true },
function (tabArray) {
chrome.tabs.executeScript(tabArray[0].id, {
file: 'src/js/scripts/extractCSS.js'
}, function() {
chrome.tabs.sendMessage(this.props.source);
})
}
);
As there can be only one active tab in current window, tabArray
will have one element only and then id
attribute can be accessed.
本文标签: javascriptsend message to active tabStack Overflow
版权声明:本文标题:javascript - send message to active tab - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745536648a2154991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论