-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
71 lines (55 loc) · 1.97 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
chrome.action.onClicked.addListener(async (tab) => {
try {
const existingTabs = await chrome.tabs.query({ url: 'https://mail.google.com/*' });
if (existingTabs.length > 0) {
await chrome.tabs.update(existingTabs[0].id, { active: true });
setTimeout(() => {
chrome.tabs.sendMessage(existingTabs[0].id, { action: 'openComposeAndInsert' });
}, 1000);
} else {
const gmailTab = await chrome.tabs.create({ url: 'https://mail.google.com' });
chrome.tabs.onUpdated.addListener(function listener(tabId, info) {
if (tabId === gmailTab.id && info.status === 'complete') {
chrome.tabs.onUpdated.removeListener(listener);
setTimeout(() => {
chrome.tabs.sendMessage(tabId, { action: 'openComposeAndInsert' });
}, 3000);
}
});
}
} catch (error) {
console.error('Error:', error);
}
});
function waitForElement(selector, timeout = 10000) {
return new Promise((resolve, reject) => {
const startTime = Date.now();
const checkElement = () => {
const element = document.querySelector(selector);
if (element) {
resolve(element);
} else if (Date.now() - startTime > timeout) {
reject(new Error(`Element ${selector} not found within ${timeout}ms`));
} else {
setTimeout(checkElement, 100);
}
};
checkElement();
});
}
async function openComposeAndShowDialog() {
try {
const composeButton = await waitForElement('div[jscontroller="eIu7Db"]');
composeButton.click();
await waitForElement('div[contenteditable="true"][role="textbox"]');
setTimeout(showHtmlDialog, 1000);
} catch (error) {
console.error('Error opening compose window:', error);
}
}
function handleExtensionMessage(message, sender, sendResponse) {
if (message.action === 'openComposeAndInsert') {
openComposeAndShowDialog();
}
}
chrome.runtime.onMessage.addListener(handleExtensionMessage);