Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AniPetrosyan committed Apr 9, 2024
2 parents 77c739e + 0ff7448 commit a6da1fe
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ chrome.contextMenus.onClicked.addListener((info, tab) => {
id: Date.now(),
text: selectedText,
tabId: tab.id, // Associate the snippet with the tab it was created in
windowId: tab.windowId // Associate the snippet with the window it was created in
windowId: tab.windowId, // Associate the snippet with the window it was created in
time: Date.now(),
url: tab.url,
};

// Add the new snippet to the array of snippets
Expand All @@ -51,6 +53,8 @@ chrome.tabs.onCreated.addListener((tab) => {
text: `${tab.url}\nCreated at: ${readableDate}`,
tabId: tab.id,
windowId: tab.windowId,
time: Date.now(),
url: tab.url,
};

snippets.push(newSnippet);
Expand All @@ -65,29 +69,38 @@ chrome.tabs.onActivated.addListener(activeInfo => {
const tabId = activeInfo.tabId;
const currentTime = new Date();

chrome.storage.local.get({snippets: []}, (result) => {
chrome.storage.local.get({ snippets: [] }, (result) => {
let snippets = result.snippets;

const index = snippets.findIndex(snippet => snippet.id === tabIdToDelete);
if (index !== -1) {
snippets.splice(index, 1);
}

let found = snippets.find(snippet => snippet.tabId === tabId);
chrome.tabs.get(tabId, tab => {
if (chrome.runtime.lastError || !tab) {
console.error(chrome.runtime.lastError ? chrome.runtime.lastError.message : "Tab not found");
return;
}

if (found) {
found.text = `${tab.url}\nActivated at: ${currentTime.toLocaleString()}`;
} else {
snippets.push({
id: tabId,
text: `${tab.url}\nActivated at: ${currentTime.toLocaleString()}`,
tabId: tabId,
windowId: activeInfo.windowId
});
}
let found = snippets.find(snippet => snippet.tabId === tabId);

chrome.storage.local.set({snippets}, () => {
console.log(`Tab ${tabId} activated and time updated.`);
if (found) {
found.text = `${tab.url}\nActivated at: ${currentTime.toLocaleString()}`;
found.time = Date.now();
} else {
snippets.push({
id: tabId,
text: `${tab.url}\nActivated at: ${currentTime.toLocaleString()}`,
tabId: tabId,
windowId: activeInfo.windowId,
time: Date.now(),
});
}

chrome.storage.local.set({ snippets }, () => {
console.log(`Tab ${tabId} activated and time updated.`);
});
});
});
});
Expand All @@ -102,12 +115,15 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {

if (found) {
found.text = `${tab.url}\nUpdated at: ${currentTime.toLocaleString()}`;
found.time = Date.now();
} else {
snippets.push({
id: tabId,
text: `${tab.url}\nUpdated at: ${currentTime.toLocaleString()}`,
tabId: tabId,
windowId: tab.windowId
windowId: tab.windowId,
time: Date.now(),
url: tab.url,
});
}

Expand Down

0 comments on commit a6da1fe

Please sign in to comment.