所以我解释了我的问题,让我们看看有人知道我做错了什么。
我正在创建一个chrome扩展,在上下文菜单中添加一个选项。作为操作的一部分,我需要获取一个cookie并使用cookie的值,打开一个新选项卡。
我的问题似乎与Mac版本(我无法在Windows上重现,但我没有尝试过其他Linux发行版)如果用户有一个普通的窗口和一个隐身窗口打开,如果他有焦点在隐身窗口上,右键单击普通窗口以触发contextMenu操作(MacOS不会更改焦点),chrome尝试从隐身窗口中获取cookie并在隐身窗口中打开新选项卡,这是一个专注的,而不是右键单击的窗口。
关于如何修复它的任何想法?
(为了重现,您需要Allow in incognito
)
的script.js
function openTab(info, tab) {
console.log(tab.url); // returns the clicked url, not the incognito
chrome.cookies.getAll({}, function(cookies){
console.log(cookies); // shows only the cookies of the incognito
// this will open the tab in the incognito window
chrome.tabs.create({
url: "https://www.example.com"
});
});
}
chrome.contextMenus.create({
"title":"Open tab",
"contexts":["link"],
"onclick":openTab
});
使用以下清单
{
"name": "Getting cookies context change",
"description": "Example of what goes wrong while getting cookie",
"version": "0.1",
"permissions": [
"contextMenus",
"cookies",
"activeTab",
"<all_urls>"
],
"background": {
"scripts": ["script.js"]
},
"manifest_version": 2
}