-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
183 lines (161 loc) · 4.21 KB
/
content.js
File metadata and controls
183 lines (161 loc) · 4.21 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
function doJank(time = 200) {
let start = performance.now();
let now = performance.now();
performance.mark('Start Jank');
while (now - start < time) {
now = performance.now();
}
performance.mark('End Jank');
performance.measure('Jank', 'Start Jank', 'End Jank');
// Send status update to popup
sendStatusToPopup();
}
let interval;
let isActive = false;
let currentSettings = null; // Store current interval settings
// Long task tracking data
let longTasksData = {
tasks: [],
count: 0,
};
// Initialize performance observer for long tasks
function initPerformanceObserver() {
// Observer for long tasks
const longTaskObserver = new PerformanceObserver((entries) => {
entries.getEntries().forEach((entry) => {
longTasksData.count++;
longTasksData.tasks.push({
duration: entry.duration,
startTime: entry.startTime,
});
});
});
// Start observing
try {
longTaskObserver.observe({ entryTypes: ['longtask'] });
} catch (e) {
console.error('PerformanceObserver error:', e);
}
}
// Call this when page loads
initPerformanceObserver();
// Restore interval state on page load
function restoreIntervalState() {
chrome.storage.local.get(['jankSettings'], (result) => {
if (result.jankSettings && result.jankSettings.isActive) {
const settings = result.jankSettings;
startJankInterval(settings.time, settings.interval);
}
});
}
// Start the jank interval
function startJankInterval(time, intervalTime) {
doJank(time);
clearInterval(interval);
isActive = true;
currentSettings = { time, interval: intervalTime };
interval = setInterval(function () {
doJank(time);
}, intervalTime);
// Save settings to storage
chrome.storage.local.set({
jankSettings: {
isActive: true,
time: time,
interval: intervalTime,
},
});
sendStatusToPopup();
}
// Stop the jank interval
function stopJankInterval() {
clearInterval(interval);
isActive = false;
currentSettings = null;
// Clear settings from storage
chrome.storage.local.set({
jankSettings: {
isActive: false,
},
});
sendStatusToPopup();
}
// Restore state when content script loads
restoreIntervalState();
// Group long tasks by duration range
function analyzeLongTaskDistribution() {
const distribution = {
'0-50ms': 0,
'50-100ms': 0,
'100-200ms': 0,
'200-500ms': 0,
'500ms+': 0,
};
longTasksData.tasks.forEach((task) => {
if (task.duration < 50) distribution['0-50ms']++;
else if (task.duration < 100) distribution['50-100ms']++;
else if (task.duration < 200) distribution['100-200ms']++;
else if (task.duration < 500) distribution['200-500ms']++;
else distribution['500ms+']++;
});
return distribution;
}
// Send long task data to popup
function sendLongTaskData() {
const distribution = analyzeLongTaskDistribution();
try {
chrome.runtime.sendMessage(
{
type: 'longTaskData',
data: {
count: longTasksData.count,
distribution: distribution,
},
},
(response) => {
if (chrome.runtime.lastError) {
console.log(
'Extension context invalidated:',
chrome.runtime.lastError.message
);
}
}
);
} catch (e) {
console.log('Error sending long task data to popup:', e);
}
}
// Send current status to the popup
function sendStatusToPopup() {
try {
chrome.runtime.sendMessage(
{
type: 'jankStatus',
isActive: isActive,
},
(response) => {
if (chrome.runtime.lastError) {
console.log(
'Extension context invalidated:',
chrome.runtime.lastError.message
);
}
}
);
} catch (e) {
console.log('Error sending message to popup:', e);
}
}
// Handle messages from the popup
addEventListener('message', (event) => {
const data = event.data;
if (data && data.action === 'doJank') {
startJankInterval(data.time, data.interval);
} else if (data && data.action === 'stopJank') {
stopJankInterval();
} else if (data && data.action === 'getStatus') {
sendStatusToPopup();
} else if (data && data.action === 'getLongTaskData') {
sendLongTaskData();
}
});