Skip to content

Commit 224e337

Browse files
committed
2024
1 parent a7f7306 commit 224e337

File tree

11 files changed

+141
-73
lines changed

11 files changed

+141
-73
lines changed

background.js

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,86 @@
1+
let currentTabId = null;
2+
13
// Event to toggle Lenis when extension's button is clicked
24
chrome.action.onClicked.addListener(async (tab) => {
5+
currentTabId = tab.id;
6+
await injectScripts(tab.id);
37
chrome.tabs.sendMessage(tab.id, { action: "toggleLenis" });
48
});
59

10+
async function injectScripts(tabId) {
11+
try {
12+
await chrome.scripting.executeScript({
13+
target: { tabId: tabId },
14+
files: ['lenis-wrapper.js']
15+
});
16+
await chrome.scripting.executeScript({
17+
target: { tabId: tabId },
18+
files: ['execute.js']
19+
});
20+
} catch (error) {
21+
console.error('Error injecting scripts:', error);
22+
}
23+
}
24+
625
// Function to update the icon
726
function updateIcon(enabled) {
8-
const path = enabled ? {
9-
16: 'icon16.png',
10-
48: 'icon48.png',
11-
128: 'icon128.png'
12-
} : {
13-
16: 'icon16-gray.png',
14-
48: 'icon48-gray.png',
15-
128: 'icon128-gray.png'
16-
};
17-
chrome.action.setIcon({ path: path });
27+
if (!currentTabId) {
28+
console.error('No current tab ID when updating icon');
29+
return;
30+
}
31+
32+
const iconName = enabled ? 'icon' : 'icon-gray';
33+
const iconSizes = [16, 48, 128];
34+
35+
const path = {};
36+
iconSizes.forEach(size => {
37+
path[size] = `${iconName}${size}.png`;
38+
});
39+
40+
console.log('Attempting to set icon with path:', path);
41+
42+
chrome.action.setIcon({
43+
tabId: currentTabId,
44+
path: path
45+
}, () => {
46+
if (chrome.runtime.lastError) {
47+
console.error('Error setting icon:', chrome.runtime.lastError.message);
48+
} else {
49+
console.log('Icon set successfully');
50+
}
51+
});
1852
}
1953

2054
// Listen for changes in storage
2155
chrome.storage.onChanged.addListener((changes, namespace) => {
2256
if (namespace === 'local' && 'smooth' in changes) {
57+
console.log('Smooth scrolling state changed:', changes.smooth.newValue);
2358
updateIcon(changes.smooth.newValue);
2459
}
2560
});
2661

27-
// Set initial icon state on extension load
28-
chrome.runtime.onStartup.addListener(() => {
62+
// Set initial icon state when a tab is activated
63+
chrome.tabs.onActivated.addListener((activeInfo) => {
64+
currentTabId = activeInfo.tabId;
2965
chrome.storage.local.get(['smooth'], (result) => {
66+
console.log('Tab activated, current smooth state:', result.smooth);
3067
updateIcon(result.smooth || false);
3168
});
69+
});
70+
71+
// Update icon when a tab is updated (e.g., page refresh)
72+
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
73+
if (changeInfo.status === 'complete' && tabId === currentTabId) {
74+
chrome.storage.local.get(['smooth'], (result) => {
75+
console.log('Tab updated, current smooth state:', result.smooth);
76+
updateIcon(result.smooth || false);
77+
});
78+
}
79+
});
80+
81+
// Log errors from extension context
82+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
83+
if (message.type === 'error') {
84+
console.error('Error from content script:', message.error);
85+
}
3286
});

execute.js

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,49 @@
1-
// This code will be executed when the extension's button is clicked
2-
31
(function() {
42
let lenis = null;
5-
6-
function loadLenis() {
7-
return new Promise((resolve, reject) => {
8-
if (window.Lenis) {
9-
resolve(window.Lenis);
10-
} else {
11-
const script = document.createElement('script');
12-
script.src = 'https://cdn.jsdelivr.net/npm/lenis';
13-
script.onload = () => resolve(window.Lenis);
14-
script.onerror = reject;
15-
document.head.appendChild(script);
16-
}
17-
});
18-
}
3+
let rafId = null;
194

205
function enableLenis() {
21-
if (!lenis) {
22-
lenis = new window.Lenis();
6+
if (!lenis && typeof Lenis === 'function') {
7+
lenis = new Lenis();
238
function raf(time) {
249
lenis.raf(time);
25-
requestAnimationFrame(raf);
10+
rafId = requestAnimationFrame(raf);
2611
}
27-
requestAnimationFrame(raf);
12+
rafId = requestAnimationFrame(raf);
13+
chrome.storage.local.set({ smooth: true });
14+
console.log("Smooth scrolling enabled");
15+
} else if (!Lenis) {
16+
console.error('Lenis is not available');
2817
}
2918
}
3019

3120
function disableLenis() {
3221
if (lenis) {
22+
if (rafId) {
23+
cancelAnimationFrame(rafId);
24+
rafId = null;
25+
}
3326
lenis.destroy();
3427
lenis = null;
28+
chrome.storage.local.set({ smooth: false });
29+
console.log("Smooth scrolling disabled");
3530
}
3631
}
3732

3833
function toggleLenis() {
3934
chrome.storage.local.get(["smooth"], function(result) {
40-
const newState = !result.smooth;
41-
42-
if (newState) {
43-
loadLenis().then(() => {
44-
enableLenis();
45-
chrome.storage.local.set({ smooth: true });
46-
console.log("Smooth scrolling enabled");
47-
}).catch(error => {
48-
console.error('Failed to load Lenis:', error);
49-
});
50-
} else {
35+
if (result.smooth) {
5136
disableLenis();
52-
chrome.storage.local.set({ smooth: false });
53-
console.log("Smooth scrolling disabled");
37+
} else {
38+
enableLenis();
5439
}
5540
});
5641
}
5742

5843
// Check if we need to initialize Lenis on page load
5944
chrome.storage.local.get(["smooth"], function(result) {
6045
if (result.smooth) {
61-
loadLenis().then(() => {
62-
enableLenis();
63-
}).catch(error => {
64-
console.error('Failed to load Lenis:', error);
65-
});
46+
enableLenis();
6647
}
6748
});
6849

@@ -72,4 +53,12 @@
7253
toggleLenis();
7354
}
7455
});
56+
57+
// Cleanup function
58+
function cleanup() {
59+
disableLenis();
60+
}
61+
62+
// Add event listener for when the script is about to be unloaded
63+
window.addEventListener('beforeunload', cleanup);
7564
})();
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

lenis-wrapper.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)