This commit is contained in:
Kevin R
2020-09-10 00:23:56 +02:00
parent 0c694d86ad
commit e2b638eafb
9 changed files with 177 additions and 30 deletions

View File

@@ -6,6 +6,7 @@ before_script:
- apt-get install -y zip unzip nodejs jsonlint
stages:
- install
- test
- build
- deploy
@@ -36,6 +37,26 @@ hash rules:
changes:
- data/data.min.json
install addon:
stage: install
script:
- npm install
cache:
paths:
- node_modules/
artifacts:
expire_in: 7 days
when: on_success
paths:
- node_modules/
test addon:
stage: test
dependencies:
- install
script:
- npm test
bundle addon:
stage: build
script:
@@ -68,4 +89,4 @@ pages:
- GitLabPages/*
- img/clearurls.svg
- data/data.min.json
- build_tools/minifyDataJSON.js
- build_tools/minifyDataJSON.js

46
__tests__/rules/rule.ts Normal file
View File

@@ -0,0 +1,46 @@
/*
* ClearURLs
* Copyright (c) 2017-2020 Kevin Röbert
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Rule from '../../source/rules/rule';
import SimpleRule from '../../source/rules/simpleRule';
describe('RuleTest', () => {
let rule: Rule
it('should create correct RegExp', () => {
rule = new SimpleRule('test')
expect(rule.value.toString()).toBe(new RegExp('([\/?#]|(&|&amp;))+(test=[^&]*)', 'gi').toString())
})
it('should return correct value on toString', () => {
rule = new SimpleRule('test')
expect(rule.toString()).toBe('([\/?#]|(&|&amp;))+(test=[^&]*)')
})
it('should be set active value correctly', () => {
rule = new SimpleRule('test')
expect(rule.isActive).toBe(true)
rule.deactivate()
expect(rule.isActive).toBe(false)
rule.activate()
expect(rule.isActive).toBe(true)
})
})

View File

@@ -19,6 +19,9 @@
import CircularBuffer from '../utils/circularBuffer'
import LogEntry from './logEntry'
/**
* Models the log.
*/
export default class Log extends CircularBuffer<LogEntry> {
public constructor(capacity: number) {
super(capacity)

View File

@@ -18,6 +18,9 @@
import Rule from '../rules/rule'
/**
* Models a log entry.
*/
export default class LogEntry {
private readonly _before: string
private readonly _after: string
@@ -33,23 +36,23 @@ export default class LogEntry {
this._runtime = runtime
}
get before() : string {
get before(): string {
return this._before
}
get after() : string {
get after(): string {
return this._after
}
get rule() : Rule {
get rule(): Rule {
return this._rule
}
get timestamp() : number {
get timestamp(): number {
return this._timestamp
}
get runtime() : number {
get runtime(): number {
return this._runtime
}
}

View File

@@ -16,14 +16,52 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Models a rule.
*/
export default class Rule {
private _rule: RegExp
private readonly _rule: RegExp
private _active: boolean
constructor(rule: RegExp) {
this._rule = rule
/**
* Creates a new rule.
*
* @param rule - the simple rule
* @param active - if the rule is active or not
*/
constructor(rule: string, active: boolean = true) {
this._rule = new RegExp(rule, "gi")
this._active = active
}
get value() : RegExp {
get value(): RegExp {
return this._rule
}
get isActive(): boolean {
return this._active
}
/**
* Activates the rule.
*/
public activate() {
this._active = true
}
/**
* Deactivates the rule.
*/
public deactivate() {
this._active = false
}
/**
* Returns this rule as string.
*
* @returns the rule as string
*/
public toString(): string {
return this._rule.source
}
}

View File

@@ -0,0 +1,34 @@
/*
* ClearURLs
* Copyright (c) 2017-2020 Kevin Röbert
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Rule from "./rule";
/**
* Models a simple rule.
*/
export default class SimpleRule extends Rule {
/**
* Creates a new simple rule, that already includes start and stop elements.
*
* @param rule - the simple rule
* @param active - if the rule is active or not
*/
constructor(rule: string, active: boolean = true) {
super('([\/?#]|(&|&amp;))+(' + rule + '=[^&]*)', active);
}
}

View File

@@ -24,12 +24,12 @@
*/
import OptionsSync from 'webext-options-sync'
import { Utils } from '../utils/utils'
import {Utils} from '../utils/utils'
/**
* Saves all options in sync storage.
*/
export const syncStorage = new OptionsSync({
export const SyncStorage = new OptionsSync({
defaults: {
rulesHash: '',
badgedStatus: true,
@@ -55,7 +55,7 @@ export const syncStorage = new OptionsSync({
pingRequestTypes: '',
},
migrations: [
// Secound param is normaly "defaults", but currently not used
// Second param is normally "defaults", but currently not used
(savedOptions, _) => {
if (Utils.getBrowser() === 'Firefox') {
if (savedOptions.requestTypes === '') {
@@ -65,8 +65,7 @@ export const syncStorage = new OptionsSync({
if (savedOptions.pingRequestTypes === '') {
savedOptions.pingRequestTypes = Object.values(FirefoxPingRequestTypes).join(',')
}
}
else {
} else {
if (savedOptions.requestTypes === '') {
savedOptions.requestTypes = Object.values(ChromeRequestTypes).join(',')
}

View File

@@ -16,12 +16,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Models a circular buffer of fixed size.
*/
export default class CircularBuffer<T> {
private buffer: T[]
public readonly capacity: number
private readonly buffer: T[]
/**
* Create a new circular buffer, that removes the oldes elements if the capacity is reached.
* Create a new circular buffer, that removes the oldest elements if the capacity is reached.
*
* @param capacity - the capacity of this queue
* @throws error if the capacity is <= 0
@@ -35,12 +38,16 @@ export default class CircularBuffer<T> {
this.buffer = []
}
get size(): number {
return this.buffer.length
}
/**
* Returns true if the buffer is empty.
*
* @returns true if the buffer is empty otherwise false
*/
public isEmpty() : boolean {
public isEmpty(): boolean {
return this.buffer.length === 0
}
@@ -49,7 +56,7 @@ export default class CircularBuffer<T> {
*
* @returns true if the buffer is full otherwise false
*/
public isFull() : boolean {
public isFull(): boolean {
return this.buffer.length === this.capacity
}
@@ -58,7 +65,7 @@ export default class CircularBuffer<T> {
*
* @param item - item that should be enqueued
*/
public enqueue(item: T) : void {
public enqueue(item: T): void {
if (this.isFull()) {
this.buffer.shift()
}
@@ -71,11 +78,7 @@ export default class CircularBuffer<T> {
*
* @returns the buffer
*/
public toArray() : T[] {
public toArray(): T[] {
return [...this.buffer]
}
get size() : number {
return this.buffer.length
}
}

View File

@@ -20,7 +20,7 @@
/*
* This script is responsible for some tools.
*/
import { browser } from 'webextension-polyfill-ts'
import {browser} from 'webextension-polyfill-ts'
declare const InstallTrigger: any
@@ -33,7 +33,7 @@ export class Utils {
*
* @returns Promise with true, iff the current browser runs on android otherwise false
*/
static async isAndroidOS() : Promise<boolean> {
static async isAndroidOS(): Promise<boolean> {
return (await browser.runtime.getPlatformInfo()).os === 'android'
}
@@ -42,7 +42,7 @@ export class Utils {
*
* @returns the browser name
*/
static getBrowser() : string {
static getBrowser(): string {
if (typeof InstallTrigger !== 'undefined') {
return 'Firefox'
}
@@ -58,7 +58,7 @@ export class Utils {
* @param url - the url, that should be decoded
* @returns the decoded URL
*/
static decodeURL(url: string) : string {
static decodeURL(url: string): string {
let rtn = decodeURIComponent(url)
while (Utils.isEncodedURI(rtn)) {
@@ -76,7 +76,7 @@ export class Utils {
* @param uri - the URI to be checked
* @returns true, iff the given URI is encoded otherwise false
*/
static isEncodedURI(uri: string) : boolean {
static isEncodedURI(uri: string): boolean {
return uri !== decodeURIComponent(uri || '')
}