diff --git a/manifest-chrome.partial.json b/manifest-chrome.partial.json index 6296876..56ffdf7 100644 --- a/manifest-chrome.partial.json +++ b/manifest-chrome.partial.json @@ -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" } -} +} \ No newline at end of file diff --git a/src/backgroundScript.ts b/src/backgroundScript.ts index 7171167..896b218 100644 --- a/src/backgroundScript.ts +++ b/src/backgroundScript.ts @@ -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 { @@ -31,7 +34,6 @@ const mainLoop = async () => { if (response.empty) { log('Got empty valid response from CPH'); - return; } @@ -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 + } +}); \ No newline at end of file diff --git a/src/handleSubmit.ts b/src/handleSubmit.ts index 3b0c677..c31dfa7 100644 --- a/src/handleSubmit.ts +++ b/src/handleSubmit.ts @@ -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, @@ -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); -}; +}; \ No newline at end of file diff --git a/src/injectedScript.ts b/src/injectedScript.ts index b17b2f4..5cc2815 100644 --- a/src/injectedScript.ts +++ b/src/injectedScript.ts @@ -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; } @@ -54,4 +53,4 @@ chrome.runtime.onMessage.addListener((data: any, sender: any) => { if (data.type == 'cph-submit') { handleData(data); } -}); +}); \ No newline at end of file