Skip to content

Commit

Permalink
Merge pull request #2302 from ayush-848/master
Browse files Browse the repository at this point in the history
Added email manager
  • Loading branch information
Sulagna-Dutta-Roy authored Jul 11, 2024
2 parents 886dc77 + 4d806ae commit 5bb4629
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Email Manager/background.js
Original file line number Diff line number Diff line change
@@ -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
}
});
71 changes: 71 additions & 0 deletions Email Manager/content.js
Original file line number Diff line number Diff line change
@@ -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();
})();
20 changes: 20 additions & 0 deletions Email Manager/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
100 changes: 100 additions & 0 deletions Email Manager/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Manager</title>
<style>
body {
font-family: Arial, sans-serif;
width: 350px;
margin: 0;
padding: 15px;
background-color: #f5f5f5;
}
h1 {
color: #1a73e8;
text-align: center;
margin-bottom: 20px;
}
#email-list {
max-height: 400px;
overflow-y: auto;
}
.email-item {
background-color: #ffffff;
border: 1px solid #dadce0;
border-radius: 8px;
padding: 12px;
margin-bottom: 10px;
box-shadow: 0 1px 2px 0 rgba(60,64,67,0.3), 0 1px 3px 1px rgba(60,64,67,0.15);
}
.email-subject {
font-weight: bold;
color: #202124;
margin-bottom: 5px;
}
.email-recipients {
font-size: 12px;
color: #5f6368;
margin-bottom: 5px;
}
.email-date {
font-size: 12px;
color: #5f6368;
margin-bottom: 10px;
}
.email-actions {
display: flex;
justify-content: flex-end;
}
button {
background-color: #1a73e8;
color: white;
border: none;
padding: 8px 16px;
margin-left: 8px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.3s;
}
button:hover {
background-color: #174ea6;
}
.delete-btn {
background-color: #ea4335;
}
.delete-btn:hover {
background-color: #d93025;
}
.no-emails {
text-align: center;
color: #5f6368;
font-style: italic;
}
#add-email-btn {
display: block;
margin: 0 auto 20px;
}
#add-email-form input,
#add-email-form textarea {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #dadce0;
border-radius: 4px;
}
#add-email-form textarea {
height: 100px;
resize: vertical;
}
</style>
</head>
<body>
<h1>Email Manager</h1>
<button id="add-email-btn">Add New Email</button>
<div id="email-list"></div>
<script src="popup.js"></script>
</body>
</html>
131 changes: 131 additions & 0 deletions Email Manager/popup.js
Original file line number Diff line number Diff line change
@@ -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 = '<p class="no-emails">Error fetching emails. Please try again.</p>';
return;
}

const emails = data.emails || [];
if (emails.length === 0) {
emailList.innerHTML = '<p class="no-emails">No emails stored yet.</p>';
return;
}
emails.forEach((email, index) => {
const emailDiv = document.createElement('div');
emailDiv.className = 'email-item';
emailDiv.innerHTML = `
<div class="email-subject">${escapeHtml(email.subject)}</div>
<div class="email-recipients">To: ${escapeHtml(email.recipients)}</div>
<div class="email-date">${new Date(email.date).toLocaleString()}</div>
<div class="email-actions">
<button onclick="copyEmail(${index})">Copy</button>
<button class="delete-btn" onclick="deleteEmail(${index})">Delete</button>
</div>
`;
emailList.appendChild(emailDiv);
});
});
}

function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}

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 = `
<form id="add-email-form">
<input type="text" id="email-subject" placeholder="Subject" required>
<input type="text" id="email-recipients" placeholder="Recipients" required>
<textarea id="email-body" placeholder="Email body" required></textarea>
<button type="submit">Save Email</button>
</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();
});
});

0 comments on commit 5bb4629

Please sign in to comment.