-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
45 lines (41 loc) · 1.57 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
// Default state is "enabled" (extension is on)
let extensionEnabled = true;
// Retrieve the state from local storage when the extension is loaded
chrome.storage.local.get(['extensionEnabled'], function(result) {
if (result.extensionEnabled !== undefined) {
extensionEnabled = result.extensionEnabled;
} else {
// Set default value if not found
chrome.storage.local.set({ extensionEnabled: true });
}
});
// Listen for the message to toggle the extension on/off
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'toggleExtension') {
extensionEnabled = message.enabled;
// Update storage to reflect the new state
chrome.storage.local.set({ extensionEnabled: extensionEnabled });
}
if (message.action === "openInTab2" && message.url) {
chrome.tabs.query({ index: 1 }, (tabs) => {
if (tabs.length > 0) {
// If Tab 2 exists, open the URL there
chrome.tabs.update(tabs[0].id, { url: message.url });
} else {
// If Tab 2 doesn't exist, create it and open the URL
chrome.tabs.create({ url: message.url, index: 1 });
}
});
}
if (message.action === "openInTab1" && message.url) {
chrome.tabs.query({ index: 0 }, (tabs) => {
if (tabs.length > 0) {
// If Tab 1 exists, open the URL there
chrome.tabs.update(tabs[0].id, { url: message.url });
} else {
// If Tab 1 doesn't exist, create it and open the URL
chrome.tabs.create({ url: message.url, index: 0 });
}
});
}
});