diff --git a/Email Manager/background.js b/Email Manager/background.js new file mode 100644 index 00000000..f3dd0956 --- /dev/null +++ b/Email Manager/background.js @@ -0,0 +1,25 @@ +chrome.runtime.onInstalled.addListener(() => { + chrome.storage.sync.get('emails', (data) => { + if (!data.emails) { + chrome.storage.sync.set({ emails: [] }); + } + }); +}); + +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { + if (request.action === "storeEmail") { + chrome.storage.sync.get("emails", (data) => { + const emails = data.emails || []; + emails.push(request.email); + chrome.storage.sync.set({ emails: emails }, () => { + if (chrome.runtime.lastError) { + console.error('Error storing email:', chrome.runtime.lastError); + sendResponse({ success: false }); + } else { + sendResponse({ success: true }); + } + }); + }); + return true; // Indicates that the response is asynchronous + } +}); \ No newline at end of file diff --git a/Email Manager/content.js b/Email Manager/content.js new file mode 100644 index 00000000..c6fe0e51 --- /dev/null +++ b/Email Manager/content.js @@ -0,0 +1,71 @@ +(function() { + if (window.location.protocol === 'chrome:') { + console.log('Extension does not run on chrome:// pages'); + return; + } + + function storeEmail() { + // Get the email subject + const subjectElement = document.querySelector('input[name="subjectbox"]'); + const subject = subjectElement ? subjectElement.value : ''; + + // Get the email body + const bodyElement = document.querySelector('div[role="textbox"]'); + const body = bodyElement ? bodyElement.innerText : ''; + + // Get the recipient(s) + const recipientElements = document.querySelectorAll('div[aria-label="To"] span[email]'); + const recipients = Array.from(recipientElements).map(el => el.getAttribute('email')).join(', '); + + // Create the email object + const email = { + subject: subject, + body: body, + recipients: recipients, + date: new Date().toISOString() + }; + + // Send the email data to the background script + chrome.runtime.sendMessage({action: "storeEmail", email: email}, (response) => { + if (chrome.runtime.lastError) { + console.error('Error storing email:', chrome.runtime.lastError); + } else if (response && response.success) { + console.log('Email stored successfully'); + alert('Email stored successfully!'); + } + }); + } + + function addStoreButton() { + const composeBox = document.querySelector('.compose-header'); + if (composeBox && !composeBox.querySelector('#store-email-btn')) { + const storeButton = document.createElement('button'); + storeButton.id = 'store-email-btn'; + storeButton.textContent = 'Store Email'; + storeButton.style.cssText = ` + background-color: #1a73e8; + color: white; + border: none; + padding: 8px 16px; + margin-left: 8px; + border-radius: 4px; + cursor: pointer; + font-size: 14px; + `; + storeButton.onclick = storeEmail; + composeBox.appendChild(storeButton); + } + } + + const observer = new MutationObserver((mutations) => { + mutations.forEach((mutation) => { + if (mutation.addedNodes.length) { + addStoreButton(); + } + }); + }); + + observer.observe(document.body, { childList: true, subtree: true }); + + addStoreButton(); +})(); \ No newline at end of file diff --git a/Email Manager/manifest.json b/Email Manager/manifest.json new file mode 100644 index 00000000..38a6ae77 --- /dev/null +++ b/Email Manager/manifest.json @@ -0,0 +1,20 @@ +{ + "manifest_version": 2, + "name": "Email Manager", + "version": "1.0", + "description": "Store and manage important emails", + "permissions": ["storage", "activeTab", "tabs"], + "background": { + "scripts": ["background.js"], + "persistent": false + }, + "content_scripts": [ + { + "matches": ["*://mail.google.com/*"], + "js": ["content.js"] + } + ], + "browser_action": { + "default_popup": "popup.html" + } +} \ No newline at end of file diff --git a/Email Manager/popup.html b/Email Manager/popup.html new file mode 100644 index 00000000..4e6f3190 --- /dev/null +++ b/Email Manager/popup.html @@ -0,0 +1,100 @@ + + + + + + Email Manager + + + +

Email Manager

+ +
+ + + \ No newline at end of file diff --git a/Email Manager/popup.js b/Email Manager/popup.js new file mode 100644 index 00000000..09b90f7d --- /dev/null +++ b/Email Manager/popup.js @@ -0,0 +1,131 @@ +function displayEmails() { + const emailList = document.getElementById('email-list'); + emailList.innerHTML = ''; + + chrome.storage.sync.get("emails", (data) => { + if (chrome.runtime.lastError) { + console.error('Error fetching emails:', chrome.runtime.lastError); + emailList.innerHTML = '

Error fetching emails. Please try again.

'; + return; + } + + const emails = data.emails || []; + if (emails.length === 0) { + emailList.innerHTML = '

No emails stored yet.

'; + return; + } + emails.forEach((email, index) => { + const emailDiv = document.createElement('div'); + emailDiv.className = 'email-item'; + emailDiv.innerHTML = ` +
${escapeHtml(email.subject)}
+
To: ${escapeHtml(email.recipients)}
+
${new Date(email.date).toLocaleString()}
+
+ + +
+ `; + emailList.appendChild(emailDiv); + }); + }); +} + +function escapeHtml(unsafe) { + return unsafe + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); +} + +function copyEmail(index) { + chrome.storage.sync.get("emails", (data) => { + if (chrome.runtime.lastError) { + console.error('Error fetching emails:', chrome.runtime.lastError); + alert('Error copying email. Please try again.'); + return; + } + + const emails = data.emails || []; + const email = emails[index]; + const emailContent = `To: ${email.recipients}\nSubject: ${email.subject}\nDate: ${email.date}\n\n${email.body}`; + navigator.clipboard.writeText(emailContent).then(() => { + alert('Email copied to clipboard!'); + }).catch((err) => { + console.error('Error copying to clipboard:', err); + alert('Error copying to clipboard. Please try again.'); + }); + }); +} + +function deleteEmail(index) { + if (confirm('Are you sure you want to delete this email?')) { + chrome.storage.sync.get("emails", (data) => { + if (chrome.runtime.lastError) { + console.error('Error fetching emails:', chrome.runtime.lastError); + alert('Error deleting email. Please try again.'); + return; + } + + const emails = data.emails || []; + emails.splice(index, 1); + chrome.storage.sync.set({ emails: emails }, () => { + if (chrome.runtime.lastError) { + console.error('Error saving emails:', chrome.runtime.lastError); + alert('Error deleting email. Please try again.'); + } else { + displayEmails(); + } + }); + }); + } +} + +function showAddEmailForm() { + console.log('Showing add email form'); + const form = ` +
+ + + + +
+ `; + document.getElementById('email-list').innerHTML = form; + + document.getElementById('add-email-form').addEventListener('submit', function(e) { + e.preventDefault(); + console.log('Form submitted'); + const newEmail = { + subject: document.getElementById('email-subject').value, + recipients: document.getElementById('email-recipients').value, + body: document.getElementById('email-body').value, + date: new Date().toISOString() + }; + + chrome.storage.sync.get("emails", (data) => { + const emails = data.emails || []; + emails.push(newEmail); + chrome.storage.sync.set({ emails: emails }, () => { + if (chrome.runtime.lastError) { + console.error('Error saving new email:', chrome.runtime.lastError); + alert('Error saving new email. Please try again.'); + } else { + console.log('New email saved successfully'); + displayEmails(); + } + }); + }); + }); +} + +document.addEventListener('DOMContentLoaded', () => { + console.log('DOM fully loaded'); + displayEmails(); + document.getElementById('add-email-btn').addEventListener('click', () => { + console.log('Add New Email button clicked'); + showAddEmailForm(); + }); +}); \ No newline at end of file