8192fab1b8
* account for iframes in popup list/write-style and badge * fix and simplify openURL + onTabReady + message from popup * fixup! resolve about:blank iframes to their parent URL * fixup! don't underline iframe links until hovered * fix width bug in popup only when needed (Chrome 66-69) * fixup! reset styleIds on main page navigation * fixup! call updateCount explicitly on extension pages * fixup! ensure frame url is present * fixup! frameResults entry may be empty * fixup! init main frame first * fixup! track iframes via ports * fixup! reduce badge update rate during page load * fixup! cosmetics * fixup! don't add frames with errors * fixup! cosmetics
55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
/* global navigatorUtil */
|
|
/* exported tabManager */
|
|
'use strict';
|
|
|
|
const tabManager = (() => {
|
|
const listeners = [];
|
|
const cache = new Map();
|
|
chrome.tabs.onRemoved.addListener(tabId => cache.delete(tabId));
|
|
chrome.tabs.onReplaced.addListener((added, removed) => cache.delete(removed));
|
|
navigatorUtil.onUrlChange(({tabId, frameId, url}) => {
|
|
if (frameId) return;
|
|
const oldUrl = tabManager.get(tabId, 'url');
|
|
tabManager.set(tabId, 'url', url);
|
|
for (const fn of listeners) {
|
|
try {
|
|
fn({tabId, url, oldUrl});
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
}
|
|
});
|
|
|
|
return {
|
|
onUpdate(fn) {
|
|
listeners.push(fn);
|
|
},
|
|
get(tabId, ...keys) {
|
|
return keys.reduce((meta, key) => meta && meta[key], cache.get(tabId));
|
|
},
|
|
/**
|
|
* number of keys is arbitrary, last arg is value, `undefined` will delete the last key from meta
|
|
* (tabId, 'foo', 123) will set tabId's meta to {foo: 123},
|
|
* (tabId, 'foo', 'bar', 'etc', 123) will set tabId's meta to {foo: {bar: {etc: 123}}}
|
|
*/
|
|
set(tabId, ...args) {
|
|
let meta = cache.get(tabId);
|
|
if (!meta) {
|
|
meta = {};
|
|
cache.set(tabId, meta);
|
|
}
|
|
const value = args.pop();
|
|
const lastKey = args.pop();
|
|
for (const key of args) meta = meta[key] || (meta[key] = {});
|
|
if (value === undefined) {
|
|
delete meta[lastKey];
|
|
} else {
|
|
meta[lastKey] = value;
|
|
}
|
|
},
|
|
list() {
|
|
return cache.keys();
|
|
},
|
|
};
|
|
})();
|