Skip to content

Commit

Permalink
renamed and shortened code
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian committed Apr 18, 2024
1 parent fec5e8e commit 614b329
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 287 deletions.
96 changes: 48 additions & 48 deletions public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,50 @@ let windowIdToDelete = "";
chrome.runtime.onInstalled.addListener(() => {
// Create a context menu item
chrome.contextMenus.create({
id: 'captureSnippet',
title: 'Capture Snippet',
id: 'captureTab',
title: 'Capture Tab',
contexts: ['selection'],
});
});

// This function is called when a context menu item is clicked
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'captureSnippet') {
if (info.menuItemId === 'captureTab') {
const selectedText = info.selectionText;

// Retrieve the existing snippets from chrome.storage.local
chrome.storage.local.get({ snippets: [] }, (result) => {
const snippets = result.snippets;
// Retrieve the existing tabs from chrome.storage.local
chrome.storage.local.get({ tabs: [] }, (result) => {
const tabs = result.tabs;

// Create a new snippet object with a unique ID, the selected text, and tab/window IDs
const newSnippet = {
// Create a new tab object with a unique ID, the selected text, and tab/window IDs
const newTab = {
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
tabId: tab.id, // Associate the tab with the tab it was created in
windowId: tab.windowId, // Associate the tab with the window it was created in
time: Date.now(),
url: tab.url,
};

// Add the new snippet to the array of snippets
snippets.push(newSnippet);
// Add the new tab to the array of tabs
tabs.push(newTab);

// Save the updated array of snippets to chrome.storage.local
chrome.storage.local.set({ snippets }, () => {
console.log('Snippet saved');
// Save the updated array of tabs to chrome.storage.local
chrome.storage.local.set({ tabs }, () => {
console.log('Tab saved');
});
});
}
});

chrome.tabs.onCreated.addListener((tab) => {
chrome.storage.local.get({ snippets: [] }, (result) => {
const snippets = result.snippets;
chrome.storage.local.get({ tabs: [] }, (result) => {
const tabs = result.tabs;

const currentDate = new Date();
const readableDate = currentDate.toLocaleString();

const newSnippet = {
const newTab = {
id: Date.now(),
text: `${tab.url}\nCreated at: ${readableDate}`,
tabId: tab.id,
Expand All @@ -57,10 +57,10 @@ chrome.tabs.onCreated.addListener((tab) => {
url: tab.url,
};

snippets.push(newSnippet);
tabs.push(newTab);

chrome.storage.local.set({ snippets }, () => {
console.log('Snippet saved with tab and window IDs');
chrome.storage.local.set({ tabs }, () => {
console.log('Tab saved with tab and window IDs');
});
});
});
Expand All @@ -69,12 +69,12 @@ chrome.tabs.onActivated.addListener(activeInfo => {
const tabId = activeInfo.tabId;
const currentTime = new Date();

chrome.storage.local.get({ snippets: [] }, (result) => {
let snippets = result.snippets;
snippets.sort((a, b) => a.time - b.time);
const index = snippets.findIndex(snippet => snippet.id === tabIdToDelete);
chrome.storage.local.get({ tabs: [] }, (result) => {
let tabs = result.tabs;
tabs.sort((a, b) => a.time - b.time);
const index = tabs.findIndex(tab => tab.id === tabIdToDelete);
if (index !== -1) {
snippets.splice(index, 1);
tabs.splice(index, 1);
}

chrome.tabs.get(tabId, tab => {
Expand All @@ -83,13 +83,13 @@ chrome.tabs.onActivated.addListener(activeInfo => {
return;
}

let found = snippets.find(snippet => snippet.tabId === tabId);
let found = tabs.find(tab => tab.tabId === tabId);

if (found) {
found.text = `${tab.title}\nActivated at: ${currentTime.toLocaleString()}`;
found.time = Date.now();
} else {
snippets.push({
tabs.push({
id: tabId,
text: `${tab.title}\nActivated at: ${currentTime.toLocaleString()}`,
tabId: tabId,
Expand All @@ -99,7 +99,7 @@ chrome.tabs.onActivated.addListener(activeInfo => {
});
}

chrome.storage.local.set({ snippets }, () => {
chrome.storage.local.set({ tabs }, () => {
console.log(`Tab ${tabId} activated and time updated.`);
});
});
Expand All @@ -110,27 +110,27 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.title) {
const currentTime = new Date();

chrome.storage.local.get({ snippets: [] }, (result) => {
let snippets = result.snippets;
snippets.sort((a, b) => a.time - b.time);
let found = snippets.find(snippet => snippet.tabId === tabId);
chrome.storage.local.get({ tabs: [] }, (result) => {
let tabs = result.tabs;
tabs.sort((a, b) => a.time - b.time);
let found = tabs.find(tab => tab.tabId === tabId);

const snippetText = `${tab.title}\nUpdated at: ${currentTime.toLocaleString()}`;
const tabText = `${tab.title}\nUpdated at: ${currentTime.toLocaleString()}`;

if (found) {
found.text = snippetText;
found.text = tabText;
} else {
snippets.push({
tabs.push({
id: tabId,
text: snippetText,
text: tabText,
tabId: tabId,
windowId: tab.windowId,
time: Date.now(),
url: tab.url,
});
}

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

chrome.tabs.onRemoved.addListener(tabId => {
tabIdToDelete = tabId;
chrome.storage.local.get({ snippets: [] }, (result) => {
let snippets = result.snippets;
const index = snippets.findIndex(snippet => snippet.tabId === tabId);
chrome.storage.local.get({ tabs: [] }, (result) => {
let tabs = result.tabs;
const index = tabs.findIndex(tab => tab.tabId === tabId);
if (index !== -1) {
snippets.splice(index, 1);
chrome.storage.local.set({ snippets }, () => {
tabs.splice(index, 1);
chrome.storage.local.set({ tabs }, () => {
console.log(`Tab ${tabId} removed and corresponding entry deleted.`);
});
}
Expand All @@ -154,11 +154,11 @@ chrome.tabs.onRemoved.addListener(tabId => {
// This function handles window removal
chrome.windows.onRemoved.addListener(windowId => {
windowIdToDelete = windowId;
chrome.storage.local.get({ snippets: [] }, (result) => {
let snippets = result.snippets;
snippets = snippets.filter(snippet => snippet.windowId !== windowId);
chrome.storage.local.set({ snippets }, () => {
console.log(`All snippets associated with window ${windowId} removed.`);
chrome.storage.local.get({ tabs: [] }, (result) => {
let tabs = result.tabs;
tabs = tabs.filter(tab => tab.windowId !== windowId);
chrome.storage.local.set({ tabs }, () => {
console.log(`All tabs associated with window ${windowId} removed.`);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion public/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ document.addEventListener('mouseup', function (e) {
if (selectedText.length > 0) {
// Send a message to the background script with the selected text
chrome.runtime.sendMessage(
{ action: 'saveSnippet', data: selectedText },
{ action: 'saveTab', data: selectedText },
(response) => {
// Log the response status from the background script
console.log(response.status);
Expand Down
4 changes: 2 additions & 2 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"manifest_version": 3,
"name": "Snippet Saver",
"name": "TabsOFF",
"version": "1.0",
"description": "Save snippets of text from web pages",
"description": "Allows organized ways to close old tabs",
"icons": {
"16": "images/favicon-16x16.png",
"32": "images/favicon-32x32.png",
Expand Down
6 changes: 3 additions & 3 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
background-color: white;
}

.snippet-list {
.tabs-list {
list-style-type: none;
padding: 0;
}

.snippet-item {
.tab-item {
margin-bottom: 1rem;
padding: 0.5rem;
border: 1px solid #ccc;
}

.snippet-item button {
.tab-item button {
margin-left: 0.5rem;
}
84 changes: 42 additions & 42 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,94 +12,94 @@ describe('App', () => {
chrome.flush();
});

it('renders Snippet Collector header', async () => {
chrome.storage.local.get.withArgs('snippets').yields({ snippets: [] });
it('renders Tab Collector header', async () => {
chrome.storage.local.get.withArgs('tabs').yields({ tabs: [] });

render(<App />);
const headerElement = await screen.findByText(/Snippet Collector/i);
const headerElement = await screen.findByText(/Tab Collector/i);
expect(headerElement).toBeInTheDocument();

expect(chrome.storage.local.get).toHaveBeenCalledWith('snippets');
expect(chrome.storage.local.get).toHaveBeenCalledWith('tabs');
});

it('renders sample snippet when snippets are undefined in chrome storage', async () => {
chrome.storage.local.get.withArgs('snippets').yields({ snippets: undefined });
it('renders sample tab when tabs are undefined in chrome storage', async () => {
chrome.storage.local.get.withArgs('tabs').yields({ tabs: undefined });

render(<App />);
const snippetElements = await screen.findAllByRole('listitem');
expect(snippetElements).toHaveLength(1);
expect(snippetElements[0]).toHaveTextContent('Sample snippet');
const tabElements = await screen.findAllByRole('listitem');
expect(tabElements).toHaveLength(1);
expect(tabElements[0]).toHaveTextContent('Sample tab');
});

it('renders snippets from chrome storage', async () => {
const mockSnippets = [
{ id: 1, text: 'Snippet 1' },
{ id: 2, text: 'Snippet 2' },
it('renders tabs from chrome storage', async () => {
const mocktabs = [
{ id: 1, text: 'Tab 1' },
{ id: 2, text: 'Tab 2' },
];
chrome.storage.local.get.withArgs('snippets').yields({ snippets: mockSnippets });
chrome.storage.local.get.withArgs('tabs').yields({ tabs: mocktabs });

render(<App />);
const snippetElements = await screen.findAllByRole('listitem');
expect(snippetElements).toHaveLength(2);
expect(snippetElements[0]).toHaveTextContent('Snippet 1');
expect(snippetElements[1]).toHaveTextContent('Snippet 2');
const tabElements = await screen.findAllByRole('listitem');
expect(tabElements).toHaveLength(2);
expect(tabElements[0]).toHaveTextContent('Tab 1');
expect(tabElements[1]).toHaveTextContent('Tab 2');
});

it('updates snippet in chrome storage when edited', async () => {
const mockSnippets = [
{ id: 1, text: 'Sample snippet' },
{ id: 2, text: 'Snippet 2' },
it('updates tab in chrome storage when edited', async () => {
const mocktabs = [
{ id: 1, text: 'Sample tab' },
{ id: 2, text: 'Tab 2' },
];
chrome.storage.local.get.withArgs('snippets').yields({ snippets: mockSnippets });
chrome.storage.local.get.withArgs('tabs').yields({ tabs: mocktabs });

render(<App />);
const editButtons = await screen.findAllByText('Edit');
expect(editButtons).toHaveLength(2);
const editButton = editButtons[0];
fireEvent.click(editButton);

const textArea = await screen.findByDisplayValue('Sample snippet');
fireEvent.change(textArea, { target: { value: 'Updated snippet' } });
const textArea = await screen.findByDisplayValue('Sample tab');
fireEvent.change(textArea, { target: { value: 'Updated tab' } });

const saveButton = await screen.findByText('Save');
fireEvent.click(saveButton);

expect(chrome.storage.local.set).toHaveBeenCalledWith({
snippets: [{ id: 1, text: 'Updated snippet' }, { id: 2, text: 'Snippet 2' }],
tabs: [{ id: 1, text: 'Updated tab' }, { id: 2, text: 'Tab 2' }],
});
});

it('removes snippet from chrome storage when deleted', async () => {
const mockSnippets = [{ id: 1, text: 'Snippet to delete' }];
chrome.storage.local.get.withArgs('snippets').yields({ snippets: mockSnippets });
it('removes tab from chrome storage when deleted', async () => {
const mocktabs = [{ id: 1, text: 'Tab to delete' }];
chrome.storage.local.get.withArgs('tabs').yields({ tabs: mocktabs });

render(<App />);
const deleteButton = await screen.findByText('Delete');
fireEvent.click(deleteButton);

expect(chrome.storage.local.set).toHaveBeenCalledWith({ snippets: [] });
expect(chrome.storage.local.set).toHaveBeenCalledWith({ tabs: [] });
});

it('sets initial state with sample snippet when local storage is empty', async () => {
chrome.storage.local.get.withArgs('snippets').yields({});
it('sets initial state with sample tab when local storage is empty', async () => {
chrome.storage.local.get.withArgs('tabs').yields({});

render(<App />);
const snippetElements = await screen.findAllByRole('listitem');
expect(snippetElements).toHaveLength(1);
expect(snippetElements[0]).toHaveTextContent('Sample snippet');
const tabElements = await screen.findAllByRole('listitem');
expect(tabElements).toHaveLength(1);
expect(tabElements[0]).toHaveTextContent('Sample tab');
});

it('sets initial state with empty array when snippets key is an empty array in local storage', async () => {
chrome.storage.local.get.withArgs('snippets').yields({ snippets: [] });
it('sets initial state with empty array when tabs key is an empty array in local storage', async () => {
chrome.storage.local.get.withArgs('tabs').yields({ tabs: [] });

render(<App />);
const snippetElements = screen.queryAllByRole('listitem');
expect(snippetElements).toHaveLength(0);
const tabElements = screen.queryAllByRole('listitem');
expect(tabElements).toHaveLength(0);
});

/*it('handles edited snippet with no changes', async () => {
const mockSnippets = [{ id: 1, text: 'Sample snippet' }];
chrome.storage.local.get.withArgs('snippets').yields({ snippets: mockSnippets });
/*it('handles edited tab with no changes', async () => {
const mocktabs = [{ id: 1, text: 'Sample tab' }];
chrome.storage.local.get.withArgs('tabs').yields({ tabs: mocktabs });
render(<App />);
const editButton = await screen.findByText('Edit');
Expand Down
Loading

0 comments on commit 614b329

Please sign in to comment.