Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function tabTraces() {
- const TRACE_LENGTH = 5; // How many tabs to mark as recently visited
- const THICKNESS_PX = 2; // How thick the marker is, in pixels
- const TAB_LIMIT = 15; // Only mark tabs if there are at least this many in the window
- let recentTabIDs = [];
- let myWindowId = null;
- let styleElement = document.createElement('style');
- document.head.appendChild(styleElement);
- async function generateStyleRulesForRecentTabs() {
- let tabs = await chrome.tabs.query({ windowId: myWindowId });
- if (tabs.length < TAB_LIMIT) {
- styleElement.textContent = "";
- return;
- }
- styleElement.textContent =
- recentTabIDs
- .slice(1, TRACE_LENGTH + 1) //Skip first i.e active tab
- .map((id, i) => {
- let alpha = (0.78 ** i).toFixed(2);
- return `#tab-${id} {
- border-bottom: ${THICKNESS_PX}px solid rgba(255, 255, 255, ${alpha}) !important;
- border-radius: var(--currentRadius) var(--currentRadius) ${THICKNESS_PX}px ${THICKNESS_PX}px !important;
- }`;
- }).join('\n');
- }
- function removeTabFromRecent(tabId) {
- recentTabIDs = recentTabIDs.filter(id => id !== tabId);
- }
- function onActivated({ tabId, windowId }) {
- if (windowId !== myWindowId) return;
- removeTabFromRecent(tabId);
- recentTabIDs.unshift(tabId);
- recentTabIDs = recentTabIDs.slice(0, TRACE_LENGTH + 1); //Removes traces above the limit
- generateStyleRulesForRecentTabs();
- }
- function onRemoved(tabId) {
- removeTabFromRecent(tabId)
- generateStyleRulesForRecentTabs();
- }
- function init() {
- if (!document.getElementById('browser')) {
- return setTimeout(init, 250);
- }
- chrome.windows.getCurrent(win => {
- myWindowId = win.id;
- chrome.tabs.query({ active: true, windowId: myWindowId }, tabs => {
- if (tabs[0]) recentTabIDs.unshift(tabs[0].id);
- });
- chrome.tabs.onActivated.addListener(onActivated);
- chrome.tabs.onRemoved.addListener(onRemoved);
- });
- }
- init();
- })();
Advertisement
Add Comment
Please, Sign In to add comment