-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
143 lines (118 loc) · 4.24 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Listen for messages from the popup
let emailInfoFromContent = {};
let last_quoted = new Set();
function quoteLine(toQuote) {
const lines = toQuote.split('\n');
let result = '';
for (let i = 0; i < lines.length; i++) {
result += '> ' + lines[i] + '\n';
}
result += '\n\n';
return result;
}
function removeQuotesIfNeeded(inputString) {
const trimmedString = inputString.trim();
if (trimmedString.startsWith("'") && trimmedString.endsWith("'")) {
return trimmedString.substring(1, trimmedString.length - 1);
} else if (trimmedString.startsWith("\"") && trimmedString.endsWith("\"")) {
return trimmedString.substring(1, trimmedString.length - 1);
}
return inputString;
}
// Function to create the email message
function createEmailMessage(emailInfo, emailContent, name) {
// Construct the email message using emailInfo and emailContent
// Format the email message as required by the Gmail API
// Return the raw email message string
let emailBody = '';
emailBody += 'From: ' + name + '\n';
emailBody += 'To: ' + emailInfo.to.join(', ') + '\n';
emailBody += 'Cc: ' + emailInfo.cc.join(', ') + '\n';
emailBody += 'Subject: ' + emailInfo.subject + '\n';
emailBody += 'In-Reply-To: <' + removeQuotesIfNeeded(emailInfo.inReplyTo) + '>\n';
emailBody += 'References: <' + removeQuotesIfNeeded(emailInfo.inReplyTo) +'>\n\n';
emailBody += emailContent;
emailBody += '\n\n==============================\nReplied with love by Quick Reply Extension\nhttps://github.com/HarryWangATX/quick-reply-extension\n';
console.log(emailBody);
emailBody = btoa(emailBody);
return emailBody;
}
chrome.tabs.onActivated.addListener(function(info) {
console.log("Tab Changed.");
chrome.tabs.get(info.tabId, function(tab) {
if (tab.url && tab.url.startsWith('https://lore.kernel.org')) {
try {
last_quoted = new Set();
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['scripts/content.js']
});
}
catch (error) {
console.log(error);
}
}
});
});
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
console.log(message);
if (message.emailInfo) {
emailInfoFromContent = message.emailInfo;
console.log(emailInfoFromContent);
}
else if (message.requestEmailInfo) {
emailInfoFromContent.text = Array.from(last_quoted);
console.log(emailInfoFromContent);
sendResponse( { emailInfoFromContent } )
}
else if (message.action == 'selectedText') {
last_quoted.add(quoteLine(message.text));
console.log(last_quoted);
emailInfoFromContent.text = last_quoted;
}
else if (message.clearSelected) {
last_quoted = new Set();
}
if (message.emailContent) {
const accessToken = message.accessToken;
const emailInfo = message.emailInfoFromContent; // Extracted email information
const emailContent = message.emailContent; // Content of the email reply
const emailRaw = createEmailMessage(emailInfo, emailContent, message.name);
console.log(emailRaw);
let send_fetch_url = `https://gmail.googleapis.com/gmail/v1/users/me/messages/send`
let send_fetch_options = {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
'raw': emailRaw
})
}
console.log("Gmail API: ", send_fetch_url);
try {
const response = await fetch(send_fetch_url, send_fetch_options);
const data = await response.json();
console.log('Email sent:', data);
if (data.error) {
if (data.error.status == "UNAUTHENTICATED") {
console.log("unauthorized");
chrome.runtime.sendMessage({ clearStorage: true });
}
else {
chrome.runtime.sendMessage({ success: 'error' });
}
}
else {
chrome.runtime.sendMessage({ success: 'success' }); // Sending success response
}
} catch (error) {
console.error('Error sending email:', error);
chrome.runtime.sendMessage({ success: 'error' }); // Sending error response
}
console.log('Sending email:', emailInfo, emailContent);
}
return true;
});