This commit is contained in:
Kevin R
2020-08-19 23:41:12 +02:00
parent b7e209bf15
commit 843aeed109
12 changed files with 11233 additions and 1 deletions

View File

@@ -0,0 +1 @@
// import { browser } from 'webextension-polyfill-ts'

82
source/storage/storage.ts Normal file
View File

@@ -0,0 +1,82 @@
/*
* 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/>.
*/
/*jshint esversion: 6 */
/*
* This script is responsible for the storage.
*
* Note: this script also includes some code for backward-compatibility.
*/
import OptionsSync from 'webext-options-sync'
import { Utils } from '../utils/utils'
export default new OptionsSync({
defaults: {
rules: '{}',
rulesHash: '',
badgedStatus: true,
enabled: true,
totalCount: 0,
cleanedCount: 0,
rulesStatus: 'error',
loggingEnabled: false,
log: '',
logLimit: 100,
statisticsEnabled: true,
badgedColor: '#ffa500',
hashURL: 'https://kevinroebert.gitlab.io/ClearUrls/data/rules.minify.hash',
rulesURL: 'https://kevinroebert.gitlab.io/ClearUrls/data/data.minify.json',
contextMenuEnabled: true,
historyListenerEnabled: true,
localHostsSkippingEnabled: true,
allowReferralMarketingEnabled: false,
domainBlockingEnabled: true,
pingBlockingEnabled: true,
eTagFilteringEnabled: true,
watchDogErrorCount: 0,
requestTypes: '',
pingRequestTypes: '',
},
migrations: [
(savedOptions, defaults) => {
if (Utils.getBrowser() === 'Firefox') {
if (savedOptions.requestTypes === '') {
savedOptions.requestTypes = 'font,image,imageset,main_frame,media,object,object_subrequest,other,script,stylesheet,sub_frame,websocket,xml_dtd,xmlhttprequest,xslt'
}
if (savedOptions.pingRequestTypes === '') {
savedOptions.pingRequestTypes = 'ping,beacon'
}
} else {
if (savedOptions.requestTypes === '') {
savedOptions.requestTypes = 'main_frame,sub_frame,stylesheet,script,image,font,object,xmlhttprequest,ping,csp_report,media,websocket,other'
}
if (savedOptions.pingRequestTypes === '') {
savedOptions.pingRequestTypes = 'ping'
}
}
},
OptionsSync.migrations.removeUnused
],
logging: true,
storageName: 'clearurlsData',
})

0
source/test.json Normal file
View File

97
source/utils/utils.ts Normal file
View File

@@ -0,0 +1,97 @@
/*
* 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/>.
*/
/*jshint esversion: 6 */
/*
* This script is responsible for some tools.
*/
import { browser } from 'webextension-polyfill-ts'
// Needed by the sha256 method
const enc = new TextEncoder()
export class Utils {
/**
* Checks if the current browser runs on android.
*
* @returns Promise with true, iff the current browser runs on android otherwise false
*/
static async isAndroidOS() : Promise<boolean> {
return (await browser.runtime.getPlatformInfo()).os === 'android'
}
/**
* Returns the browser name.
*
* @returns the browser name
*/
static getBrowser() : string {
if (typeof InstallTrigger !== 'undefined') {
return 'Firefox'
}
return 'Chrome'
}
/**
* Decodes an URL, also one that is encoded multiple times.
*
* @see https://stackoverflow.com/a/38265168
*
* @param url - the url, that should be decoded
* @returns the decoded URL
*/
static decodeURL(url: string) : string {
let rtn = decodeURIComponent(url)
while (Utils.isEncodedURI(rtn)) {
rtn = decodeURIComponent(rtn)
}
return rtn
}
/**
* Returns true, iff the given URI is encoded
*
* @see https://stackoverflow.com/a/38265168
*
* @param uri - the URI to be checked
* @returns true, iff the given URI is encoded otherwise false
*/
static isEncodedURI(uri: string) : boolean {
return uri !== decodeURIComponent(uri || '')
}
/**
* This method calculates the SHA-256 hash as HEX string of the given message.
* This method uses the native hashing implementations of the SubtleCrypto interface which is supported by all browsers
* that implement the Web Cryptography API specification and is based on:
* https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
*
* @param message - message for which the hash should be calculated
* @returns SHA-256 of the given message
*/
static async sha256(message: string): Promise<string> {
const msgUint8 = enc.encode(message)
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8)
const hashArray = Array.from(new Uint8Array(hashBuffer))
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
}
}