Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jmerle committed Sep 9, 2018
1 parent f9349c8 commit 630c419
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 66 deletions.
12 changes: 12 additions & 0 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ function checkTab(tabId: number, changeInfo: any, tab: browser.tabs.Tab) {
});
}

function handleHistoryStateUpdate(details: any) {
const { tabId, url } = details;

sendToContent(tabId, MessageAction.CheckTab, {
tabId,
url,
});
}

function parse(tab: browser.tabs.Tab) {
sendToContent(tab.id, MessageAction.Parse);
}
Expand Down Expand Up @@ -50,5 +59,8 @@ function handleMessage(
}

browser.tabs.onUpdated.addListener(checkTab);
browser.webNavigation.onHistoryStateUpdated.addListener(
handleHistoryStateUpdate,
);
browser.pageAction.onClicked.addListener(parse);
browser.runtime.onMessage.addListener(handleMessage);
14 changes: 13 additions & 1 deletion src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ const Nanobar = require('nanobar');
let activeParser: Parser = null;

function checkTab(tabId: number, url: string): void {
sendToBackground(MessageAction.DisablePageAction);

for (const parser of parsers) {
const hasMatchingPattern = parser
.getRegularExpressions()
.some(r => r.test(url));

if (hasMatchingPattern && parser.canHandlePage()) {
const hasMatchingExcludedPattern = false;

/*const hasMatchingExcludedPattern = parser
.getExcludedRegularExpressions()
.some(r => r.test(url));*/

if (
hasMatchingPattern &&
!hasMatchingExcludedPattern &&
parser.canHandlePage()
) {
activeParser = parser;
sendToBackground(MessageAction.EnablePageAction);
break;
Expand Down
5 changes: 3 additions & 2 deletions src/hosts/hosts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config } from '../utils/Config';
import { config } from '../utils/config';
import { CHelperHost } from './CHelperHost';
import { CustomHost } from './CustomHost';
import { Host } from './Host';
Expand All @@ -11,7 +11,8 @@ const defaultPorts = [

export function getHosts(): Promise<Host[]> {
return new Promise((resolve, reject) => {
Config.get<number[]>('customPorts')
config
.get<number[]>('customPorts')
.then(ports => {
const uniquePorts = [...new Set(defaultPorts.concat(ports))];

Expand Down
5 changes: 3 additions & 2 deletions src/models/Task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config } from '../utils/Config';
import { config } from '../utils/config';
import { sendToBackground } from '../utils/messaging';
import { InputConfiguration, OutputConfiguration } from './IOConfiguration';
import { LanguageConfiguration } from './LanguageConfiguration';
Expand Down Expand Up @@ -50,7 +50,8 @@ export class Task implements Sendable {

public send(): Promise<void> {
return new Promise(resolve => {
Config.get<boolean>('debugMode')
config
.get<boolean>('debugMode')
.then(debug => {
if (debug) {
// tslint:disable-next-line no-console
Expand Down
51 changes: 33 additions & 18 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Config } from './utils/Config';
import { config } from './utils/config';

const customPortsInput = document.querySelector('#custom-ports') as HTMLInputElement;
const debugModeInput = document.querySelector('#debug-mode') as HTMLInputElement;
const customPortsInput = document.querySelector(
'#custom-ports',
) as HTMLInputElement;
const debugModeInput = document.querySelector(
'#debug-mode',
) as HTMLInputElement;

customPortsInput.addEventListener('input', function () {
const ports = this
.value
customPortsInput.addEventListener('input', function() {
const ports = this.value
.split(',')
.map(x => x.trim())
.filter(x => x.length > 0)
Expand All @@ -14,26 +17,38 @@ customPortsInput.addEventListener('input', function () {
const uniquePorts = [...new Set(ports)];

if (uniquePorts.some(isNaN) || uniquePorts.some(x => x < 0)) {
(document.querySelector('#custom-ports-error') as HTMLElement).style.display = 'block';
(document.querySelector(
'#custom-ports-error',
) as HTMLElement).style.display = 'block';
} else {
(document.querySelector('#custom-ports-error') as HTMLElement).style.display = 'none';
(document.querySelector(
'#custom-ports-error',
) as HTMLElement).style.display = 'none';

Config.set('customPorts', uniquePorts)
config
.set('customPorts', uniquePorts)
.then(() => {})
.catch(() => {});
}
});

debugModeInput.addEventListener('input', function () {
Config.set('debugMode', this.checked)
debugModeInput.addEventListener('input', function() {
config
.set('debugMode', this.checked)
.then(() => {})
.catch(() => {});
});

Config.get<number[]>('customPorts').then(value => {
customPortsInput.value = value.join(',');
}).catch(() => {});

Config.get<boolean>('debugMode').then(value => {
debugModeInput.checked = value;
}).catch(() => {});
config
.get<number[]>('customPorts')
.then(value => {
customPortsInput.value = value.join(',');
})
.catch(() => {});

config
.get<boolean>('debugMode')
.then(value => {
debugModeInput.checked = value;
})
.catch(() => {});
17 changes: 16 additions & 1 deletion src/parsers/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ export abstract class Parser {
return this.getMatchPatterns().map(matchPatternToRegExp);
}

/*/!**
* Returns the regular expressions which the content script will match against the url.
* If it's a match, this parser will be used on the given url.
*!/
public abstract getRegularExpressions(): RegExp[];
/!**
* The excluded version of getRegularExpressions(). If the url matches one of the
* regular expressions returned by this method, this parser will explicitly not
* be used, even if there is a match in one of the regular expressions in getRegularExpressions().
*!/
public getExcludedRegularExpressions(): RegExp[] {
return [];
}*/

/**
* When one of the regular expressions of this problemParser match the current url, this method is called.
* When one of the regular expressions of this parser match the current url, this method is called.
* If it returns true, it is assumed this page can load this page. This is useful for contest
* parsers where the url might not give away whether the contest problems are already available.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/parsers/contest/QDUOJContestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export class QDUOJContestParser extends Parser {
];
}

public canHandlePage(): boolean {
return (
document.querySelector(
'#contest-main tr.ivu-table-row > td:first-child > div > span',
) !== null
);
}

public parse(url: string, html: string): Promise<Sendable> {
return new Promise(async (resolve, reject) => {
const elem = htmlToElement(html);
Expand Down
24 changes: 0 additions & 24 deletions src/utils/Config.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Config {
private readonly defaultValues: any = {
customPorts: [],
debugMode: false,
};

public get<T extends browser.storage.StorageValue>(key: string): Promise<T> {
return new Promise((resolve, reject) => {
browser.storage.local
.get(key)
.then(data => resolve((data[key] || this.defaultValues[key]) as any))
.catch(reject);
});
}

public set(key: string, value: browser.storage.StorageValue): Promise<void> {
return browser.storage.local.set({ [key]: value });
}
}

export const config = new Config();
9 changes: 2 additions & 7 deletions static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
"author": "Filled by build script",
"homepage_url": "Filled by build script",

"permissions": [
"tabs",
"storage",
"http://localhost/"
],
"permissions": ["tabs", "storage", "webNavigation", "http://localhost/"],

"icons": {
"16": "icons/icon-16.png",
Expand All @@ -29,8 +25,7 @@

"content_scripts": [
{
"matches": ["Filled by build script"],
"exclude_matches": ["Filled by build script"],
"matches": ["<all_urls>"],
"js": ["js/browser-polyfill.min.js", "js/content.js"]
}
],
Expand Down
11 changes: 0 additions & 11 deletions webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import * as path from 'path';
import * as webpack from 'webpack';
import { Parser } from './src/parsers/Parser';

function transformManifest(content: string): string {
const manifest = JSON.parse(content);
Expand All @@ -12,16 +11,6 @@ function transformManifest(content: string): string {
}
});

const parsers: Parser[] = require('./src/parsers/parsers').parsers;

manifest.content_scripts[0].matches = parsers
.map(p => p.getMatchPatterns())
.reduce((a, b) => a.concat(...b), []);

manifest.content_scripts[0].exclude_matches = parsers
.map(p => p.getExcludedMatchPatterns())
.reduce((a, b) => a.concat(...b), []);

const packageData = require('./package.json');

manifest.name = packageData.productName;
Expand Down

0 comments on commit 630c419

Please sign in to comment.