0% found this document useful (0 votes)
13 views4 pages

Code

The document outlines a Chrome extension that manages tab navigation by tracking whether a tab navigated to a work or fun site. It stores and retrieves previous URLs to determine if a tab originated from a fun site when switching to a work site. The extension also cleans up storage when tabs are closed and allows users to switch between fun and work sites using a command.

Uploaded by

aarush.nemani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Code

The document outlines a Chrome extension that manages tab navigation by tracking whether a tab navigated to a work or fun site. It stores and retrieves previous URLs to determine if a tab originated from a fun site when switching to a work site. The extension also cleans up storage when tabs are closed and allows users to switch between fun and work sites using a command.

Uploaded by

aarush.nemani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

let activeTransforms = new Set();

// Track navigation state per tab


chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url) {
console.log(`Tab ${tabId} navigated to: ${changeInfo.url}`);
chrome.storage.sync.get(["funSites", "workSites"], (data) => {
const funSites = data.funSites || [];
const workSites = data.workSites || [];
const currentUrl = new URL(changeInfo.url);
const hostname = currentUrl.hostname.toLowerCase();

const workIndex = workSites.findIndex(site => site &&


hostname.includes(site.toLowerCase().trim()));
if (workIndex !== -1) {
console.log(`Navigated to work site: ${hostname}, checking previous
URL...`);
chrome.storage.local.get([`tab-${tabId}-previous`], (prevData) => {
const previousUrl = prevData[`tab-${tabId}-previous`];
console.log(`Previous URL: ${previousUrl}`);
if (previousUrl) {
const prevHostname = new
URL(previousUrl).hostname.toLowerCase();
const funSite = funSites[workIndex] ?
funSites[workIndex].toLowerCase().trim() : null;
if (funSite && prevHostname.includes(funSite)) {
console.log(`Previous URL matches fun site (${funSite}), storing
exact URL: ${previousUrl}`);
chrome.storage.local.set({
[`tab-${tabId}-fromFunSite`]: true,
[`tab-${tabId}-funUrl`]: previousUrl
}, () => {
console.log(`Stored: tab-${tabId}-fromFunSite: true, tab-${tabId}-
funUrl: ${previousUrl}`);
});
} else {
console.log(`Previous URL does not match fun site (${funSite}),
clearing flags.`);
chrome.storage.local.remove([`tab-${tabId}-fromFunSite`, `tab-$
{tabId}-funUrl`]);
}
} else {
console.log(`No previous URL found, clearing flags.`);
chrome.storage.local.remove([`tab-${tabId}-fromFunSite`, `tab-$
{tabId}-funUrl`]);
}
});
} else {
console.log(`Not a work site, clearing flags.`);
chrome.storage.local.remove([`tab-${tabId}-fromFunSite`, `tab-${tabId}-
funUrl`]);
}

chrome.storage.local.set({ [`tab-${tabId}-previous`]: changeInfo.url }, ()


=> {
console.log(`Stored previous URL: ${changeInfo.url}`);
});
});
}
});

chrome.tabs.onRemoved.addListener((tabId) => {
console.log(`Tab ${tabId} closed, cleaning up storage.`);
chrome.storage.local.remove([`tab-${tabId}-fromFunSite`, `tab-${tabId}-
previous`, `tab-${tabId}-funUrl`]);
});

chrome.commands.onCommand.addListener((command) => {
if (command === 'shift_mode') {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
const tab = tabs[0];
if (!tab.url || tab.url.startsWith('chrome://')) {
console.log(`Cannot switch: Invalid URL: ${tab.url}`);
return;
}

console.log(`Shift mode triggered on tab ${tab.id}, current URL: $


{tab.url}`);
chrome.storage.sync.get(["funSites", "workSites"], (data) => {
const funSites = data.funSites || [];
const workSites = data.workSites || [];
const currentUrl = new URL(tab.url);
const hostname = currentUrl.hostname.toLowerCase();

let targetUrl = null;


for (let i = 0; i < Math.max(funSites.length, workSites.length); i++) {
const funSite = (funSites[i] || "").toLowerCase().trim();
const workSite = (workSites[i] || "").toLowerCase().trim();

if (funSite && hostname.includes(funSite)) {


console.log(`On fun site: ${hostname}, switching to work site: $
{workSite}`);
targetUrl = workSite ? `https://${workSite}` : null;
break;
} else if (workSite && hostname.includes(workSite)) {
console.log(`On work site: ${hostname}, checking if originated from
fun site...`);
chrome.storage.local.get([`tab-${tab.id}-fromFunSite`, `tab-${tab.id}-
funUrl`], (flagData) => {
console.log(`Retrieved: tab-${tab.id}-fromFunSite: ${flagData[`tab-$
{tab.id}-fromFunSite`]}, tab-${tab.id}-funUrl: ${flagData[`tab-${tab.id}-
funUrl`]}`);
if (flagData[`tab-${tab.id}-fromFunSite`]) {
targetUrl = flagData[`tab-${tab.id}-funUrl`] || (funSite ? `https://$
{funSite}` : null);
console.log(`Switching back to fun site URL: ${targetUrl}`);
chrome.storage.local.remove([`tab-${tab.id}-fromFunSite`, `tab-$
{tab.id}-funUrl`], () => {
console.log(`Cleared flags after switching back.`);
});
} else {
console.log(`Not switching: Work site not originated from paired
fun site: ${hostname}`);
}
if (targetUrl) {
chrome.tabs.update(tab.id, {url: targetUrl}, () => {
if (chrome.runtime.lastError) {

You might also like