-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
77 lines (70 loc) · 2.36 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
let currentSettings = {
sidebarWidth: 160,
sidebarDisabled: false
};
function loadSettings() {
return new Promise((resolve) => {
chrome.storage.local.get(['sidebarWidth', 'sidebarDisabled'], function(data) {
if (data.sidebarWidth) currentSettings.sidebarWidth = data.sidebarWidth;
if (data.sidebarDisabled !== undefined) currentSettings.sidebarDisabled = data.sidebarDisabled;
console.log('Loaded settings:', currentSettings);
resolve(currentSettings);
});
});
}
function saveSettings() {
return new Promise((resolve) => {
chrome.storage.local.set(currentSettings, function() {
console.log('Settings saved:', currentSettings);
resolve(currentSettings);
});
});
}
function injectContentScript(tabId) {
chrome.tabs.executeScript(tabId, { file: 'sidebar-disable.js' }, function() {
if (chrome.runtime.lastError) {
console.error('Error injecting content script:', chrome.runtime.lastError);
} else {
console.log('Content script injected successfully');
applySettingsToTab(tabId);
}
});
}
function applySettingsToTab(tabId) {
chrome.tabs.sendMessage(tabId, {
action: "updateSidebar",
settings: currentSettings
}, function(response) {
if (chrome.runtime.lastError) {
console.log("Error sending message to tab:", chrome.runtime.lastError);
// If there's an error, try injecting the content script again
injectContentScript(tabId);
} else {
console.log("Settings applied to tab:", response);
}
});
}
// Monitor for Claude.ai page loads
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url && tab.url.includes('claude.ai')) {
injectContentScript(tabId);
}
});
// Handle messages from popup and content scripts
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getSettings") {
loadSettings().then(settings => sendResponse(settings));
return true;
} else if (request.action === "saveSettings") {
currentSettings = {...currentSettings, ...request.settings};
saveSettings().then(() => {
chrome.tabs.query({url: "*://*.claude.ai/*"}, function(tabs) {
tabs.forEach(tab => applySettingsToTab(tab.id));
});
sendResponse({success: true});
});
return true;
}
});
// Initialize settings on startup
loadSettings();