Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions manifest-chrome.partial.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"manifest_version": 3,
"permissions": ["scripting", "webNavigation"],
"permissions": ["scripting", "webNavigation", "alarms"], // Added "alarms"
"host_permissions": ["http://localhost/*", "https://codeforces.com/*"],
"background": {
"service_worker": "dist/backgroundScript.js",
"type": "module"
}
}
}
18 changes: 17 additions & 1 deletion src/backgroundScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { CphSubmitResponse, CphEmptyResponse } from './types';
import { handleSubmit } from './handleSubmit';
import log from './log';

// Ensure access to the chrome namespace in case it's not already defined
declare const chrome: any;

const mainLoop = async () => {
let cphResponse;
try {
Expand Down Expand Up @@ -31,7 +34,6 @@ const mainLoop = async () => {

if (response.empty) {
log('Got empty valid response from CPH');

return;
}

Expand All @@ -44,4 +46,18 @@ const mainLoop = async () => {
);
};

// Run mainLoop at regular intervals while the service worker is alive
setInterval(mainLoop, config.loopTimeOut);

// Set up Chrome Alarms API to periodically wake the service worker
chrome.runtime.onInstalled.addListener(() => {
// Create an alarm that triggers every minute
chrome.alarms.create('keepAlive', { periodInMinutes: 1 });
});

chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'keepAlive') {
console.log('Service worker woke up by alarm');
mainLoop(); // Perform background tasks on wake-up
}
});
15 changes: 2 additions & 13 deletions src/handleSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getSubmitUrl = (problemUrl: string) => {
return submitURL;
};

/** Opens the codefoces submit page and injects script to submit code. */
/** Opens the codeforces submit page and injects script to submit code. */
export const handleSubmit = async (
problemName: string,
languageId: number,
Expand Down Expand Up @@ -80,17 +80,6 @@ export const handleSubmit = async (

if (args.tabId === tab.id) {
log('Our tab is navigating');

// const url = new URL(args.url);
// const searchParams = new URLSearchParams(url.search);

// if (searchParams.has("friends")) {
// return;
// }

// log("Navigating to friends mode");

// chrome.tabs.update(args.tabId, { url: args.url + "?friends=on" });
}
}, filter);
};
};
3 changes: 1 addition & 2 deletions src/injectedScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const handleData = (data: ContentScriptData) => {
'submittedProblemIndex',
)[0] as HTMLSelectElement;

// Dont use problemName from data as it includes the contest number.
const problemName = data.url.split('/problem/')[1];
problemIndexEl.value = problemName;
}
Expand All @@ -54,4 +53,4 @@ chrome.runtime.onMessage.addListener((data: any, sender: any) => {
if (data.type == 'cph-submit') {
handleData(data);
}
});
});