|
| 1 | +// TabRecency associates an integer with each tab id representing how recently it has been accessed. |
| 2 | +// The order of tabs as tracked by TabRecency is used to provide a recency-based ordering in the |
| 3 | +// tabs vomnibar. |
| 4 | +// |
| 5 | +// The values are persisted to chrome.storage.session so that they're not lost when the extension's |
| 6 | +// background page is unloaded. |
| 7 | +// |
| 8 | +// Callers must await TabRecency.init before calling recencyScore or getTabsByRecency. |
| 9 | +// |
| 10 | +// In theory, the browser's tab.lastAccessed timestamp field should allow us to sort tabs by |
| 11 | +// recency, but in practice it does not work across several edge cases. See the comments on #4368. |
| 12 | +class TabRecency { |
| 13 | + constructor() { |
| 14 | + this.counter = 1; |
| 15 | + this.tabIdToCounter = {}; |
| 16 | + this.loaded = false; |
| 17 | + this.queuedActions = []; |
| 18 | + } |
| 19 | + |
| 20 | + // Add listeners to chrome.tabs, and load the index from session storage. |
| 21 | + async init() { |
| 22 | + if (this.initPromise) { |
| 23 | + await this.initPromise; |
| 24 | + return; |
| 25 | + } |
| 26 | + let resolveFn; |
| 27 | + this.initPromise = new Promise((resolve, _reject) => { |
| 28 | + resolveFn = resolve; |
| 29 | + }); |
| 30 | + |
| 31 | + chrome.tabs.onActivated.addListener((activeInfo) => { |
| 32 | + this.queueAction("register", activeInfo.tabId); |
| 33 | + }); |
| 34 | + chrome.tabs.onRemoved.addListener((tabId) => { |
| 35 | + this.queueAction("deregister", tabId); |
| 36 | + }); |
| 37 | + |
| 38 | + chrome.tabs.onReplaced.addListener((addedTabId, removedTabId) => { |
| 39 | + this.queueAction("deregister", removedTabId); |
| 40 | + this.queueAction("register", addedTabId); |
| 41 | + }); |
| 42 | + |
| 43 | + chrome.windows.onFocusChanged.addListener(async (windowId) => { |
| 44 | + if (windowId == chrome.windows.WINDOW_ID_NONE) return; |
| 45 | + const tabs = await chrome.tabs.query({ windowId, active: true }); |
| 46 | + if (tabs[0]) { |
| 47 | + this.queueAction("register", tabs[0].id); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + await this.loadFromStorage(); |
| 52 | + while (this.queuedActions.length > 0) { |
| 53 | + const [action, tabId] = this.queuedActions.shift(); |
| 54 | + this.handleAction(action, tabId); |
| 55 | + } |
| 56 | + this.loaded = true; |
| 57 | + resolveFn(); |
| 58 | + } |
| 59 | + |
| 60 | + // Loads the index from session storage. |
| 61 | + async loadFromStorage() { |
| 62 | + const tabsPromise = chrome.tabs.query({}); |
| 63 | + const storagePromise = chrome.storage.session.get("tabRecency"); |
| 64 | + const [tabs, storage] = await Promise.all([tabsPromise, storagePromise]); |
| 65 | + if (storage.tabRecency == null) return; |
| 66 | + |
| 67 | + let maxCounter = 0; |
| 68 | + for (const counter of Object.values(storage.tabRecency)) { |
| 69 | + if (maxCounter < counter) maxCounter = counter; |
| 70 | + } |
| 71 | + if (this.counter < maxCounter) { |
| 72 | + this.counter = maxCounter; |
| 73 | + } |
| 74 | + |
| 75 | + this.tabIdToCounter = Object.assign({}, storage.tabRecency); |
| 76 | + |
| 77 | + // Remove any tab IDs which aren't currently loaded. |
| 78 | + const tabIds = new Set(tabs.map((t) => t.id)); |
| 79 | + for (const id in this.tabIdToCounter) { |
| 80 | + if (!tabIds.has(parseInt(id))) { |
| 81 | + delete this.tabIdToCounter[id]; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + async saveToStorage() { |
| 87 | + await chrome.storage.session.set({ tabRecency: this.tabIdToCounter }); |
| 88 | + } |
| 89 | + |
| 90 | + // - action: "register" or "unregister". |
| 91 | + queueAction(action, tabId) { |
| 92 | + if (!this.loaded) { |
| 93 | + this.queuedActions.push([action, tabId]); |
| 94 | + } else { |
| 95 | + this.handleAction(action, tabId); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // - action: "register" or "unregister". |
| 100 | + handleAction(action, tabId) { |
| 101 | + if (action == "register") { |
| 102 | + this.register(tabId); |
| 103 | + } else if (action == "deregister") { |
| 104 | + this.deregister(tabId); |
| 105 | + } else { |
| 106 | + throw new Error(`Unexpected action type: ${action}`); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + register(tabId) { |
| 111 | + this.counter++; |
| 112 | + this.tabIdToCounter[tabId] = this.counter; |
| 113 | + this.saveToStorage(); |
| 114 | + } |
| 115 | + |
| 116 | + deregister(tabId) { |
| 117 | + delete this.tabIdToCounter[tabId]; |
| 118 | + this.saveToStorage(); |
| 119 | + } |
| 120 | + |
| 121 | + // Recently-visited tabs get a higher score (except the current tab, which gets a low score). |
| 122 | + recencyScore(tabId) { |
| 123 | + if (!this.loaded) throw new Error("TabRecency hasn't yet been loaded."); |
| 124 | + const tabCounter = this.tabIdToCounter[tabId]; |
| 125 | + const isCurrentTab = tabCounter == this.counter; |
| 126 | + if (isCurrentTab) return 0; |
| 127 | + return (tabCounter ?? 1) / this.counter; // tabCounter may be null. |
| 128 | + } |
| 129 | + |
| 130 | + // Returns a list of tab Ids sorted by recency, most recent tab first. |
| 131 | + getTabsByRecency() { |
| 132 | + if (!this.loaded) throw new Error("TabRecency hasn't yet been loaded."); |
| 133 | + const ids = Object.keys(this.tabIdToCounter); |
| 134 | + ids.sort((a, b) => this.tabIdToCounter[b] - this.tabIdToCounter[a]); |
| 135 | + return ids.map((id) => parseInt(id)); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +Object.assign(globalThis, { TabRecency }); |
0 commit comments