表单调用tabs.connect(undefined,object)与定义不符? [英] Invocation of form tabs.connect(undefined, object) doesn't match definition?
问题描述
我正在制作一个简单的Chrome扩展,并且需要将弹出
页面连接到内容
脚本上当前标签。然而它不断给我一个 tabId
参数给 tabs.connect
的错误。这是说我传递的标签ID是未定义的而不是整数。我不知道为什么。
完整错误:
Uncaught错误:调用表单tabs.connect(undefined,object)与定义tabs.connect(整型tabId,可选对象connectInfo)不匹配$ b $在Object.normalizeArgumentsAndValidate(extensions :: schemaUtils:115)
在Object <匿名> (extensions :: binding:363)
在connectToCurrentTab(popup.js:21)
在HTMLDocument。< anonymous> (popup.js:44)
在mightThrow(jquery-3.2.1.js:3583)
在进程(jquery-3.2.1.js:3651)
我做了一个简短的函数
来检索当前标签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);
});
返回tabId;
}
它似乎获得了正确的标签ID或至少一个整数。控制台输出:选项卡ID:111
我使用 getCurrentTabId
到 chrome.tabs.connect
从我的弹出
到 content
脚本运行在当前选项卡上,然后返回连接的 Port
。
函数connectToCurrentTab(){
var currentTabId = getCurrentTabId();
返回chrome.tabs.connect(currentTabId,{name:popup}); //< - 此处引发的错误
}
。
你的代码几乎是正确的,它比 chrome.tabs.query
是异步方法。所以,当你调用 chrome.tabs.connect
- currentTabId
时,它是 undefined
。
您可以使用 callback
函数修复它:
function getCurrentTabId(cb){
var query = {active:true,currentWindow:true};
chrome.tabs.query(query,function(tabArray){
console.log(Tab id:+ tabArray [0] .id);
cb(tabArray [0]。 id)
});
函数connectToCurrentTab(){
getCurrentTabId(函数(currentTabId){
chrome.tabs.connect(currentTabId,{name:popup})
});
}
您也可以使用 Promise
来防止回调地狱。
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.
Your code is almost correct, the problem it's than chrome.tabs.query
is async method. So, at the moment when you call chrome.tabs.connect
- currentTabId
it's undefined
.
You can fix it with usage of callback
function:
function getCurrentTabId(cb) {
var query = {active: true, currentWindow: true};
chrome.tabs.query(query, function (tabArray) {
console.log("Tab id: " + tabArray[0].id);
cb(tabArray[0].id)
});
}
function connectToCurrentTab () {
getCurrentTabId(function(currentTabId) {
chrome.tabs.connect(currentTabId, {name: "popup"})
});
}
You can also use a Promise
to prevent a callbacks hell.
这篇关于表单调用tabs.connect(undefined,object)与定义不符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!