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
2 changes: 1 addition & 1 deletion manifest-chrome.partial.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"manifest_version": 3,
"permissions": ["scripting", "webNavigation"],
"permissions": ["scripting", "webNavigation", "alarms"],
"host_permissions": ["http://localhost/*", "https://codeforces.com/*"],
"background": {
"service_worker": "dist/backgroundScript.js",
Expand Down
1 change: 1 addition & 0 deletions manifest-firefox.partial.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"permissions": [
"scripting",
"alarms",
"*://localhost/*",
"*://codeforces.com/*",
"webNavigation"
Expand Down
11 changes: 10 additions & 1 deletion src/backgroundScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ const mainLoop = async () => {
);
};

setInterval(mainLoop, config.loopTimeOut);

chrome.alarms.create('cph-submit-alarm', {
periodInMinutes: 1 / 60,
});

chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === 'cph-submit-alarm') {
await mainLoop();
}
});
90 changes: 65 additions & 25 deletions src/handleSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ export const getSubmitUrl = (problemUrl: string) => {
if (!isContestProblem(problemUrl)) {
return config.cfSubmitPage.href;
}
const url = new URL(problemUrl);
const contestNumber = url.pathname.split('/')[2];
const submitURL = `https://codeforces.com/contest/${contestNumber}/submit`;
// const url = new URL(problemUrl);
// const contestNumber = url.pathname.split('/')[2];
// const submitURL = `https://codeforces.com/contest/${contestNumber}/submit`;
// return submitURL;
const secondLastSlashIndex = problemUrl.lastIndexOf('/',problemUrl.lastIndexOf('/')-1);
const submitURL = problemUrl.substring(0,secondLastSlashIndex) + "/submit";
return submitURL;
};

Expand All @@ -36,38 +39,75 @@ export const handleSubmit = async (

log('isContestProblem', isContestProblem(problemUrl));

let tab = await chrome.tabs.create({
active: true,
url: getSubmitUrl(problemUrl),
});
const submitUrl = getSubmitUrl(problemUrl);
const tabs = await chrome.tabs.query({ url: 'https://codeforces.com/*' });

let tab;
let navigationExpected = false;

if (tabs.length > 0 && tabs[0].id) {
if (tabs[0].url !== submitUrl) {
navigationExpected = true;
}
try {
tab = await chrome.tabs.update(tabs[0].id, {
active: true,
url: submitUrl,
});
} catch (e) {
log('Failed to update tab, maybe it was closed. Creating a new one.', e);
}
}

if (!tab) {
navigationExpected = true;
tab = await chrome.tabs.create({
active: true,
url: submitUrl,
});
}

const tabId = tab.id as number;

chrome.windows.update(tab.windowId, {
focused: true,
});

if (typeof browser !== 'undefined') {
await browser.tabs.executeScript(tab.id, {
file: '/dist/injectedScript.js',
const executePayload = async () => {
if (typeof browser !== 'undefined') {
await browser.tabs.executeScript(tabId, {
file: '/dist/injectedScript.js',
});
} else {
await chrome.scripting.executeScript({
target: {
tabId,
allFrames: true,
},
files: ['/dist/injectedScript.js'],
});
}
chrome.tabs.sendMessage(tabId, {
type: 'cph-submit',
problemName,
languageId,
sourceCode,
url: problemUrl,
});
log('Sending message to tab with script');
};

if (navigationExpected) {
const listener = (id: number, info: any) => {
if (id === tabId && info.status === 'complete') {
chrome.tabs.onUpdated.removeListener(listener);
executePayload();
}
};
chrome.tabs.onUpdated.addListener(listener);
} else {
await chrome.scripting.executeScript({
target: {
tabId,
allFrames: true,
},
files: ['/dist/injectedScript.js'],
});
executePayload();
}
chrome.tabs.sendMessage(tabId, {
type: 'cph-submit',
problemName,
languageId,
sourceCode,
url: problemUrl,
});
log('Sending message to tab with script');

const filter = {
url: [{ urlContains: 'codeforces.com/problemset/status' }],
Expand Down