-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
86 lines (75 loc) · 2.42 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
let currentTabId = null;
// Event to toggle Lenis when extension's button is clicked
chrome.action.onClicked.addListener(async (tab) => {
currentTabId = tab.id;
await injectScripts(tab.id);
chrome.tabs.sendMessage(tab.id, { action: "toggleLenis" });
});
async function injectScripts(tabId) {
try {
await chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['lenis-wrapper.js']
});
await chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['execute.js']
});
} catch (error) {
console.error('Error injecting scripts:', error);
}
}
// Function to update the icon
function updateIcon(enabled) {
if (!currentTabId) {
console.error('No current tab ID when updating icon');
return;
}
const iconName = enabled ? 'icon' : 'icon-gray';
const iconSizes = [16, 48, 128];
const path = {};
iconSizes.forEach(size => {
path[size] = `${iconName}${size}.png`;
});
console.log('Attempting to set icon with path:', path);
chrome.action.setIcon({
tabId: currentTabId,
path: path
}, () => {
if (chrome.runtime.lastError) {
console.error('Error setting icon:', chrome.runtime.lastError.message);
} else {
console.log('Icon set successfully');
}
});
}
// Listen for changes in storage
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'local' && 'smooth' in changes) {
console.log('Smooth scrolling state changed:', changes.smooth.newValue);
updateIcon(changes.smooth.newValue);
}
});
// Set initial icon state when a tab is activated
chrome.tabs.onActivated.addListener((activeInfo) => {
currentTabId = activeInfo.tabId;
chrome.storage.local.get(['smooth'], (result) => {
console.log('Tab activated, current smooth state:', result.smooth);
updateIcon(result.smooth || false);
});
});
// Update icon when a tab is updated (e.g., page refresh)
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tabId === currentTabId) {
chrome.storage.local.get(['smooth'], (result) => {
console.log('Tab updated, current smooth state:', result.smooth);
updateIcon(result.smooth || false);
});
}
});
// Log errors from extension context
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'error') {
console.error('Error from content script:', message.error);
}
});