Google chrome extension window.open在chrome扩展中获取tabid后不工作

Google chrome extension window.open在chrome扩展中获取tabid后不工作,google-chrome-extension,tabs,window.open,Google Chrome Extension,Tabs,Window.open,在获得当前选项卡ID后,我正在尝试打开一个新选项卡,以便通过 从新打开的选项卡到上一个选项卡的消息, 我正在这样做: myscr.js: var myvar = 0; chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) { console.log(tabs[0]); myvar = tabs[0].id; //console.log(myvar); }); var string_ur

在获得当前选项卡ID后,我正在尝试打开一个新选项卡,以便通过 从新打开的选项卡到上一个选项卡的消息, 我正在这样做:

myscr.js:

var myvar = 0;
chrome.tabs.query({
  currentWindow: true,
  active: true
}, function (tabs) {
  console.log(tabs[0]);
  myvar = tabs[0].id;
  //console.log(myvar);
});
var string_url = "getfilehandler.html/?" + "tabid=" + myvar;
window.open(string_url);
"permissions": [
    "tabs",
    "contextMenus", "fileBrowserHandler"
],
"content_scripts": [
  {
    "matches": ["file:///home/user/Desktop/itproject/test.html"],
    "js": ["myscr.js"]
  }
],
manifest.json:

var myvar = 0;
chrome.tabs.query({
  currentWindow: true,
  active: true
}, function (tabs) {
  console.log(tabs[0]);
  myvar = tabs[0].id;
  //console.log(myvar);
});
var string_url = "getfilehandler.html/?" + "tabid=" + myvar;
window.open(string_url);
"permissions": [
    "tabs",
    "contextMenus", "fileBrowserHandler"
],
"content_scripts": [
  {
    "matches": ["file:///home/user/Desktop/itproject/test.html"],
    "js": ["myscr.js"]
  }
],
我的问题是,当我打开文件
test.html
时,浏览器似乎什么都没有 发生了

新窗口在我使用
窗口时打开。在
myschr.js
中打开(…)
。我真的不知道为什么会这样!任何帮助都将不胜感激

变化:

chrome.tabs.query({
    active: true,                              // Select active tabs
    windowId: chrome.windows.WINDOW_ID_CURRENT // In the current window
}, function(array_of_Tabs) {
    // Since there can only be one active tab in one active window, 
    //  the array has only one element
    var tab = array_of_Tabs[0];
    // Example:
    var url = tab.url;
console.log(url);
    // ... do something with url variable
});

内容脚本无法使用
chrome.tabs
API。如果你真的想使用tabId,你的代码必须移动到背景页面。如果要继续使用tabId,请确保您了解
chrome.tabs.query
方法是异步的,如中所述。非常感谢!我会调查的!那代码很难读懂。考虑编辑你的问题来添加代码。由于还没有人发布答案,您也可以编辑现有问题,因为它不会使(不存在的)答案无效。@RobW:我已更新了问题中的更改。我猜这是你的代码本身!设法在其他线程上找到它!您是否已将代码移动到服务器?前面我已经说过,
chrome.tabs
API对内容脚本不可用。您可以通过事件触发代码,或使用从内容脚本发送消息。