我创建了一个简单的扩展,可以在当前和最后一个焦点选项卡之间切换。切换可以通过ALT+Q
或单击扩展图标栏中的按钮来完成。只有一个问题。在当前选项卡上花费了一段时间后,该扩展程序似乎忘记了最后一个焦点选项卡。也就是说,我不能再切换回最后一个焦点选项卡了。
谁能给我一个解释,为什么我的扩展程序忘记了最后一个焦点选项卡?
// manifest.json
{
"background": {
"scripts": ["background.js"],
"persistent": false
},
"name": "Toggle Switch Recent Last Tabs",
"version": "1.1",
"manifest_version": 2,
"description": "Toggle between your current and last used (focused) tab with a keyboard shortcut (ALT+Q by default) or mouse click on the icon.",
"browser_action": {
"default_title": "",
"default_icon": "icon.png"
},
"commands": {
"toggle": {
"suggested_key": {
"default": "Alt+Q"
},
"description": "Toggle tabs"
}
},
"permissions": [
"tabs",
"http://*/",
"https://*/*"
],
"icons": {
"16": "icon_16.png",
"32": "icon_32.png",
"48": "icon_48.png",
"128": "icon_48.png"
}
}
并且background.js
:
// background.js
var previousTab;
var currentTab;
// General functions
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.update(previousTab, {selected: true});
});
chrome.tabs.onSelectionChanged.addListener(function(tab) {
if (previousTab == null) {
previousTab = tab;
}
if (currentTab == null) {
currentTab = tab;
}
else {
previousTab = currentTab;
currentTab = tab;
}
});
// Keyboard shortcut toggle function
chrome.commands.onCommand.addListener(function(command) {
if (command == "toggle") {
chrome.tabs.getSelected(null, function(tab) {
previousTab = tab.id;
currentTab = null;
});
chrome.tabs.update(previousTab, {selected: true});
}
});