我正在尝试在获取当前 tabid 后打开一个新选项卡,以便可以将新打开的选项卡中的消息传递到上一个选项卡,我正在这样做:
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);
清单.json:
"permissions": [
"tabs",
"contextMenus", "fileBrowserHandler"
],
"content_scripts": [
{
"matches": ["file:///home/user/Desktop/itproject/test.html"],
"js": ["myscr.js"]
}
],
我的问题是当我打开文件时test.html
,浏览器似乎什么都没有发生!
新窗口在我使用时window.open(...)
打开myscr.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
});