Skip to content

Commit 3c42175

Browse files
committed
Add icons and persist across pages and refreshes
1 parent 7d2af2a commit 3c42175

File tree

14 files changed

+137
-15
lines changed

14 files changed

+137
-15
lines changed

background.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Background service worker for managing extension icon state
2+
3+
// Listen for messages from content scripts
4+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
5+
if (message.type === 'jankStatus') {
6+
updateIcon(message.isActive);
7+
}
8+
sendResponse({ received: true });
9+
return true;
10+
});
11+
12+
// Update the extension icon based on active state
13+
function updateIcon(isActive) {
14+
if (isActive) {
15+
// Set icon to green when active
16+
chrome.action.setIcon({
17+
path: {
18+
16: 'icons/icon-green-16.png',
19+
32: 'icons/icon-green-32.png',
20+
48: 'icons/icon-green-48.png',
21+
128: 'icons/icon-green-128.png',
22+
},
23+
});
24+
} else {
25+
// Reset to default icon when inactive
26+
chrome.action.setIcon({
27+
path: {
28+
16: 'icons/icon-16.png',
29+
32: 'icons/icon-32.png',
30+
48: 'icons/icon-48.png',
31+
128: 'icons/icon-128.png',
32+
},
33+
});
34+
}
35+
}
36+
37+
// Check storage on startup to restore icon state
38+
chrome.runtime.onStartup.addListener(() => {
39+
chrome.storage.local.get(['jankSettings'], (result) => {
40+
if (result.jankSettings && result.jankSettings.isActive) {
41+
updateIcon(true);
42+
} else {
43+
updateIcon(false);
44+
}
45+
});
46+
});
47+
48+
// Also check when the service worker is installed
49+
chrome.runtime.onInstalled.addListener(() => {
50+
chrome.storage.local.get(['jankSettings'], (result) => {
51+
if (result.jankSettings && result.jankSettings.isActive) {
52+
updateIcon(true);
53+
} else {
54+
updateIcon(false);
55+
}
56+
});
57+
});

content.js

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function doJank(time = 200) {
1414

1515
let interval;
1616
let isActive = false;
17+
let currentSettings = null; // Store current interval settings
1718

1819
// Long task tracking data
1920
let longTasksData = {
@@ -45,6 +46,58 @@ function initPerformanceObserver() {
4546
// Call this when page loads
4647
initPerformanceObserver();
4748

49+
// Restore interval state on page load
50+
function restoreIntervalState() {
51+
chrome.storage.local.get(['jankSettings'], (result) => {
52+
if (result.jankSettings && result.jankSettings.isActive) {
53+
const settings = result.jankSettings;
54+
startJankInterval(settings.time, settings.interval);
55+
}
56+
});
57+
}
58+
59+
// Start the jank interval
60+
function startJankInterval(time, intervalTime) {
61+
doJank(time);
62+
clearInterval(interval);
63+
isActive = true;
64+
currentSettings = { time, interval: intervalTime };
65+
66+
interval = setInterval(function () {
67+
doJank(time);
68+
}, intervalTime);
69+
70+
// Save settings to storage
71+
chrome.storage.local.set({
72+
jankSettings: {
73+
isActive: true,
74+
time: time,
75+
interval: intervalTime,
76+
},
77+
});
78+
79+
sendStatusToPopup();
80+
}
81+
82+
// Stop the jank interval
83+
function stopJankInterval() {
84+
clearInterval(interval);
85+
isActive = false;
86+
currentSettings = null;
87+
88+
// Clear settings from storage
89+
chrome.storage.local.set({
90+
jankSettings: {
91+
isActive: false,
92+
},
93+
});
94+
95+
sendStatusToPopup();
96+
}
97+
98+
// Restore state when content script loads
99+
restoreIntervalState();
100+
48101
// Group long tasks by duration range
49102
function analyzeLongTaskDistribution() {
50103
const distribution = {
@@ -119,18 +172,9 @@ addEventListener('message', (event) => {
119172
const data = event.data;
120173

121174
if (data && data.action === 'doJank') {
122-
clearInterval(interval);
123-
isActive = true;
124-
125-
interval = setInterval(function () {
126-
doJank(data.time);
127-
}, data.interval);
128-
129-
sendStatusToPopup();
175+
startJankInterval(data.time, data.interval);
130176
} else if (data && data.action === 'stopJank') {
131-
clearInterval(interval);
132-
isActive = false;
133-
sendStatusToPopup();
177+
stopJankInterval();
134178
} else if (data && data.action === 'getStatus') {
135179
sendStatusToPopup();
136180
} else if (data && data.action === 'getLongTaskData') {

icons/icon-128.png

1.48 KB
Loading

icons/icon-16.png

198 Bytes
Loading

icons/icon-32.png

375 Bytes
Loading

icons/icon-48.png

585 Bytes
Loading

icons/icon-green-128.png

1.47 KB
Loading

icons/icon-green-16.png

198 Bytes
Loading

icons/icon-green-32.png

374 Bytes
Loading

icons/icon-green-48.png

573 Bytes
Loading

0 commit comments

Comments
 (0)