I'm making a simple chrome extension and need to have my popup page connect to the content script on the current tab. However it keeps giving me an error for the tabId argument to tabs.connect. It's saying the tab id I'm passing is undefined rather than an integer. I have no idea why.
Full Error:
Uncaught Error: Invocation of form tabs.connect(undefined, object) doesn't match definition tabs.connect(integer tabId, optional object connectInfo)
at Object.normalizeArgumentsAndValidate (extensions::schemaUtils:115)
at Object.<anonymous> (extensions::binding:363)
at connectToCurrentTab (popup.js:21)
at HTMLDocument.<anonymous> (popup.js:44)
at mightThrow (jquery-3.2.1.js:3583)
at process (jquery-3.2.1.js:3651)
I made a short function to retrieve the current tab id:
function getCurrentTabId() {
var query = {active: true, currentWindow: true};
var tabId;
chrome.tabs.query(query, function (tabArray) {
tabId = tabArray[0].id;
console.log("Tab id: " + tabId);
});
return tabId;
}
It appears to be getting the correct tab id or at least an integer. Console out put: Tab id: 111
I use the current tab id retrieved by getCurrentTabId to chrome.tabs.connect from my popup to the content script running on the current tab then return the Port it connected on.
function connectToCurrentTab () {
var currentTabId = getCurrentTabId();
return chrome.tabs.connect(currentTabId, {name: "popup"}); //<-- error thrown here
}
Any help or information is much appreciated.