-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
82 lines (70 loc) · 2.66 KB
/
background.js
File metadata and controls
82 lines (70 loc) · 2.66 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
// Background service worker for codePTIT++
// Handles sending problem data to CC-compatible IDEs via localhost POST
const DEFAULT_PORTS = [
1327, // cpbooster
4244, // Hightail
6174, // Mind Sport
10042, // acmX
10043, // Caide and AI Virtual Assistant
10045, // CP Editor
27121, // Competitive Programming Helper
];
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'sendToIDE') {
const { payload, customPorts } = message;
const allPorts = [...new Set([...DEFAULT_PORTS, ...(customPorts || [])])];
sendToAllPorts(payload, allPorts).then(sendResponse);
return true; // keep channel open for async response
}
});
async function sendToAllPorts(payload, ports) {
const results = await Promise.allSettled(
ports.map(port => sendToPort(payload, port))
);
const succeeded = [];
const failed = [];
results.forEach((result, i) => {
if (result.status === 'fulfilled') {
succeeded.push(ports[i]);
} else {
failed.push(ports[i]);
}
});
return { succeeded, failed };
}
async function sendToPort(payload, port) {
const response = await fetch(`http://localhost:${port}/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!response.ok && response.status !== 0) {
throw new Error(`Port ${port}: HTTP ${response.status}`);
}
return port;
}
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
// First time install
chrome.tabs.create({
url: 'https://www.youtube.com/playlist?list=PLUMGF3D982PrRjmzCv3ZZQZcfGAZRYCf1'
});
} else if (details.reason === 'update') {
// Updating from a previous version
const previousVersion = details.previousVersion;
console.log(`Extension updated from version ${previousVersion} to ${chrome.runtime.getManifest().version}`);
// Only show the playlist if updating from a version older than 0.4
// Check if previousVersion starts with "0." and the number after is less than 4
// Or if it's less than 0.4 by general version comparison
if (previousVersion) {
const parts = previousVersion.split('.');
const major = parseInt(parts[0]) || 0;
const minor = parseInt(parts[1]) || 0;
if (major === 0 && minor < 4) {
chrome.tabs.create({
url: 'https://www.youtube.com/playlist?list=PLUMGF3D982PrRjmzCv3ZZQZcfGAZRYCf1'
});
}
}
}
});