Skip to content

Commit

Permalink
Merge pull request #17 from drafear/develop
Browse files Browse the repository at this point in the history
Warn on submission, Clar notification
  • Loading branch information
drafear committed Sep 23, 2018
2 parents 8c9707a + 6039f2a commit d074fe5
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 51 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ See [Features](#Features) for more detail.

## Features
- Notify judge result of codes you submit
- ~~Notify clarifications on the contest page you open when you are `owner`~~
- ~~Colorful problem page~~
- Notify new clarifications on the contest page you open
- ~~Sync favorite users~~
- Add a link tab to beta page on non-beta pages
- Dropdown list of problems
- ~~Warn if you submit with specific languages such as `text`, `bash` and so on (configurable)~~
- ~~Download submitted codes~~
- ~~Disable/Enable them~~
- Warn if you select specific languages for submission such as `text`, `bash` and so on (configurable)
- Disable/Enable them

## Developing
To develop this chrome extention, clone this repository first.
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "comfortable-atcoder",
"description": "Atcoderを快適にするChrome拡張",
"description": "Comfort your atcoder life. For more detail, visit https://github.com/drafear/comfortable-atcoder",
"dependencies": {},
"default_locale": "en",
"devDependencies": {
"less-watch-compiler": "1.11.3",
"npm-run-all": "4.1.3",
Expand Down Expand Up @@ -55,6 +56,7 @@
"strict": true,
"rules": {
"quotes": ["error", "single", {"avoidEscape": true}],
"no-alert": 0,
"no-negated-condition": 0,
"capitalized-comments": 0,
"no-constant-condition": ["error", {"checkLoops": false}],
Expand Down
26 changes: 26 additions & 0 deletions src/background/notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function createNotification({data, href}) {
let notificationId;
chrome.notifications.create(data, id => {
notificationId = id;
});
if (href) {
const clickHandler = id => {
if (id !== notificationId) {
return;
}
chrome.tabs.create({url: href});
};
const closeHandler = () => {
chrome.notifications.onClicked.removeListener(clickHandler);
chrome.notifications.onClosed.removeListener(closeHandler);
};
chrome.notifications.onClicked.addListener(clickHandler);
chrome.notifications.onClosed.addListener(closeHandler);
}
}

chrome.runtime.onMessage.addListener(({type, data}) => {
if (type === 'create-notification') {
createNotification(data);
}
});
42 changes: 13 additions & 29 deletions src/background/submission-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ class SubmissionWatcher {
const startTime = Date.now();
let prevTime = this.startTime;
let prevStatus = this.submission.judgeStatus;
await Betalib.sleep(100); // Rejudge用
await CommonLib.sleep(100); // Rejudge用
while (true) {
const submission = await this.getCurrentSubmission();
if (!submission.judgeStatus.isWaiting) {
let notificationId;
let message = '';
// ジャッジ中か
if (submission.judgeStatus.now !== undefined) {
Expand All @@ -59,26 +58,15 @@ class SubmissionWatcher {
if (submission.memoryUsage) {
message += `\n${submission.memoryUsage}`;
}
chrome.notifications.create({
type: 'basic',
iconUrl: makeJudgeStatusImageUrl(submission.judgeStatus.text),
title: submission.probTitle,
message,
}, id => {
notificationId = id;
createNotification({
data: {
type: 'basic',
iconUrl: makeJudgeStatusImageUrl(submission.judgeStatus.text),
title: submission.probTitle,
message,
},
href: submission.detailAbsoluteUrl,
});
const clickHandler = id => {
if (id !== notificationId) {
return;
}
window.open(submission.detailAbsoluteUrl);
};
const closeHandler = () => {
chrome.notifications.onClicked.removeListener(clickHandler);
chrome.notifications.onClosed.removeListener(closeHandler);
};
chrome.notifications.onClicked.addListener(clickHandler);
chrome.notifications.onClosed.addListener(closeHandler);
break;
}
const curTime = Date.now();
Expand All @@ -94,7 +82,7 @@ class SubmissionWatcher {
}
prevTime = curTime;
prevStatus = submission.judgeStatus;
await Betalib.sleep(sleepMilliseconds);
await CommonLib.sleep(sleepMilliseconds);
}
}

Expand Down Expand Up @@ -139,12 +127,8 @@ async function watchSubmissionRegister(submission) {
}
}

chrome.runtime.onMessage.addListener(({type, data}, sender, reply) => {
_ = sender;
_ = reply;
switch (type) {
case 'watch-submission-register':
watchSubmissionRegister(data);
break;
chrome.runtime.onMessage.addListener(({type, data}) => {
if (type === 'watch-submission-register') {
watchSubmissionRegister(data);
}
});
8 changes: 8 additions & 0 deletions src/content/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ window.CommonLib = class {
});
});
}

static sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

static createNotification(params) {
chrome.runtime.sendMessage({type: 'create-notification', data: params});
}
};
4 changes: 0 additions & 4 deletions src/content/betalib.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,4 @@ window.Betalib = class {
});
return res;
}

static sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
};
23 changes: 23 additions & 0 deletions src/content/clar-notify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CommonLib.runIfEnableAndLoad('notify-clarification', async () => {
const contest = Betalib.getCurrentContest();
function getNotifyCount() {
return $('#clar-badge').text();
}
let prev = getNotifyCount();
while (true) {
const cur = getNotifyCount();
if (prev !== cur) {
CommonLib.createNotification({
data: {
type: 'basic',
iconUrl: chrome.extension.getURL('image/question.png'),
title: 'Atcoder',
message: 'New Clarification',
},
href: `https://beta.atcoder.jp/contests/${contest.id}/clarifications`,
});
prev = cur;
}
await CommonLib.sleep(1000);
}
});
34 changes: 34 additions & 0 deletions src/content/submission-warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CommonLib.runIfEnableAndLoad('submission-warning', async () => {
let cnt = 5;
const event = new Event('watch.count.update');
$(document).on('mouseup touchend keyup', () => {
cnt = 5;
document.dispatchEvent(event);
});
function waitForSelect() {
return new Promise(resolve => {
document.addEventListener(event.type, resolve, {once: true});
});
}
while (true) {
const $selects = $('#select-lang select:visible');
for (const select of $selects) {
const $select = $(select);
const $span = $select.next().find('.select2-selection');
const langId = $select.val();
const isWarning = await CommonLib.isEnable(`warn-${langId}`);
if (langId === $select.val()) {
if (isWarning) {
$span.css('background-color', 'hsl(40, 90%, 85%)');
} else {
$span.css('background-color', '');
}
}
}
--cnt;
if (cnt === 0) {
await waitForSelect();
}
await CommonLib.sleep(100);
}
});
Binary file modified src/image/beta.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/question.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "Comfortable Atcoder",
"version": "0.00",
"manifest_version": 2,
"description": "* Notify judge result of codes you submit\n* Notify clarifications on the contest page you open when you are `owner`\n* Colorful problem page\n* Add a link tab to beta page on non-beta pages\n* Dropdown list of problems\n* Warn if you submit with specific languages such as `text`, `bash` and so on (configurable)\n* Download submitted codes\n* Disable/Enable them\n",
"description": "Comfort your atcoder life. For more detail, visit https://github.com/drafear/comfortable-atcoder",
"author": "drafear",
"content_scripts": [
{
"matches": ["*://beta.atcoder.jp/contests/*", "*://*.contest.atcoder.jp/*"],
Expand All @@ -13,7 +14,7 @@
},
{
"matches": ["*://beta.atcoder.jp/contests/*"],
"js": ["content/betalib.js", "content/dropdown-modify.js"],
"js": ["content/betalib.js", "content/dropdown-modify.js", "content/clar-notify.js"],
"css": ["css/dropdown-modify.css"],
"run_at": "document_start"
},
Expand All @@ -22,6 +23,11 @@
"js": ["content/result-notify.js"],
"run_at": "document_start"
},
{
"matches": ["*://beta.atcoder.jp/contests/*/submit*", "*://beta.atcoder.jp/contests/*/tasks/*"],
"js": ["content/submission-warning.js"],
"run_at": "document_start"
},
{
"matches": ["*://*.contest.atcoder.jp/*"],
"exclude_matches": ["*://*.contest.atcoder.jp/users/*"],
Expand All @@ -33,8 +39,10 @@
"background": {
"scripts": [
"lib/jquery.min.js",
"content/all.js",
"content/betalib.js",
"background/oninstall.js",
"background/notification.js",
"background/submission-watcher.js"
],
"persistent": false
Expand Down
13 changes: 3 additions & 10 deletions src/options-page/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,12 @@ const groups = [
],
),
new Group(
'Edit',
'Warning on Submission',
[
new Option(

'D & D .html',
new Switch('enable', 'disable', 'dnd-html'),
),
new Option('Enable', new Switch('enable', 'disable', 'submission-warning')),
...makeWarnOptions(languages),
],
),
new Group(
'Warning on Submission',
makeWarnOptions(languages),
),
];

document.addEventListener('DOMContentLoaded', async () => {
Expand Down

0 comments on commit d074fe5

Please sign in to comment.