Chrome extension: How to remember previous previous Tab? -
i made extension remembers previous tab , on button click toggles between current tab , previous tab. want extend remember previous previous tab. allow toggle between 2 tabs once current tab closed. however, struggling code logics. code toggle between current , previous tab:
var previoustab; var currenttab; // switch tab on button click chrome.browseraction.onclicked.addlistener(function(tab) { chrome.tabs.update(previoustab, {selected: true}); }); // update variables on tab change chrome.tabs.onselectionchanged.addlistener(function(tab) { if (previoustab == null) { previoustab = tab; } if (currenttab == null) { currenttab = tab; } else { previoustab = currenttab; currenttab = tab; } });
now code toggle between 2 tabs once current tab closed:
var previoustab; var previousprevioustab; var currenttab; // switch tab on button click chrome.browseraction.onclicked.addlistener(function(tab) { chrome.tabs.update(previoustab, {selected: true}); }); // update variables on tab change chrome.tabs.onselectionchanged.addlistener(function(tab) { if (previoustab == null) { previoustab = tab; } if (currenttab == null) { currenttab = tab; } if (previousprevioustab == null) { previousprevioustab = tab; } else { previousprevioustab = previoustab; previoustab = currenttab; currenttab = tab; } }); // update variables on tab removal chrome.tabs.onremoved.addlistener(function(tab) { previoustab = previousprevioustab; });
something logic must wrong. when open 2 new tabs , close recent one, clicking on toggle button raises error tabs.update: no tab id: 698.
here idea: create tabs objects array (you can tabs objects calling chrome.windows.getall(). add property each tab object called "recent_use". set '0' selected tab.
anytime, tab selected, set "recent_use" 0. add '1' other tab's "recent_use". way, on time, nice order of tab focused recently.
then depending on how many keystrokes users hits (you have define own key strokes here), change tab focus tab has "recent_use" equal number of times user has hit keystroke. way, solution more generic previous , previousprevious tab.
(btw, have take care of modifying tabs array anytime new tab created or closed. can adding callbacks events)
in way, implementation of lfu - http://en.wikipedia.org/wiki/least_frequently_used
Comments
Post a Comment