summaryrefslogtreecommitdiffstats
path: root/platform/browser
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 05:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 05:47:55 +0000
commit31d6ff6f931696850c348007241195ab3b2eddc7 (patch)
tree615cb1c57ce9f6611bad93326b9105098f379609 /platform/browser
parentInitial commit. (diff)
downloadublock-origin-31d6ff6f931696850c348007241195ab3b2eddc7.tar.xz
ublock-origin-31d6ff6f931696850c348007241195ab3b2eddc7.zip
Adding upstream version 1.55.0+dfsg.upstream/1.55.0+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'platform/browser')
-rw-r--r--platform/browser/main.js123
-rw-r--r--platform/browser/test.html71
2 files changed, 194 insertions, 0 deletions
diff --git a/platform/browser/main.js b/platform/browser/main.js
new file mode 100644
index 0000000..d6f6acb
--- /dev/null
+++ b/platform/browser/main.js
@@ -0,0 +1,123 @@
+/*******************************************************************************
+
+ uBlock Origin - a browser extension to block requests.
+ Copyright (C) 2014-present Raymond Hill
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see {http://www.gnu.org/licenses/}.
+
+ Home: https://github.com/gorhill/uBlock
+*/
+
+'use strict';
+
+/******************************************************************************/
+
+import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js';
+import punycode from './lib/punycode.js';
+
+import staticNetFilteringEngine from './js/static-net-filtering.js';
+import { FilteringContext } from './js/filtering-context.js';
+import { LineIterator } from './js/text-utils.js';
+import * as sfp from './js/static-filtering-parser.js';
+
+import {
+ CompiledListReader,
+ CompiledListWriter
+} from './js/static-filtering-io.js';
+
+/******************************************************************************/
+
+function compileList(rawText, writer) {
+ const lineIter = new LineIterator(rawText);
+ const parser = new sfp.AstFilterParser({
+ interactive: true,
+ maxTokenLength: staticNetFilteringEngine.MAX_TOKEN_LENGTH,
+ });
+ const compiler = staticNetFilteringEngine.createCompiler();
+
+ while ( lineIter.eot() === false ) {
+ let line = lineIter.next();
+
+ while ( line.endsWith(' \\') ) {
+ if ( lineIter.peek(4) !== ' ' ) { break; }
+ line = line.slice(0, -2).trim() + lineIter.next().trim();
+ }
+ parser.parse(line);
+
+ if ( parser.isFilter() === false ) { continue; }
+ if ( parser.isNetworkFilter() === false ) { continue; }
+ if ( compiler.compile(parser, writer) ) { continue; }
+ if ( compiler.error !== undefined ) {
+ console.info(JSON.stringify({
+ realm: 'message',
+ type: 'error',
+ text: compiler.error
+ }));
+ }
+ }
+
+ return writer.toString();
+}
+
+function applyList(name, raw) {
+ const writer = new CompiledListWriter();
+ writer.properties.set('name', name);
+ const compiled = compileList(raw, writer);
+ const reader = new CompiledListReader(compiled);
+ staticNetFilteringEngine.fromCompiled(reader);
+}
+
+function enableWASM(path) {
+ return Promise.all([
+ publicSuffixList.enableWASM(`${path}/lib/publicsuffixlist`),
+ staticNetFilteringEngine.enableWASM(`${path}/js`),
+ ]);
+}
+
+function pslInit(raw) {
+ if ( typeof raw !== 'string' || raw.trim() === '' ) {
+ console.info('Unable to populate public suffix list');
+ return;
+ }
+ publicSuffixList.parse(raw, punycode.toASCII);
+ console.info('Public suffix list populated');
+}
+
+function restart(lists) {
+ // Remove all filters
+ reset();
+
+ if ( Array.isArray(lists) && lists.length !== 0 ) {
+ // Populate filtering engine with filter lists
+ for ( const { name, raw } of lists ) {
+ applyList(name, raw);
+ }
+ // Commit changes
+ staticNetFilteringEngine.freeze();
+ staticNetFilteringEngine.optimize();
+ }
+
+ return staticNetFilteringEngine;
+}
+
+function reset() {
+ staticNetFilteringEngine.reset();
+}
+
+export {
+ FilteringContext,
+ enableWASM,
+ pslInit,
+ restart,
+};
diff --git a/platform/browser/test.html b/platform/browser/test.html
new file mode 100644
index 0000000..32b1aba
--- /dev/null
+++ b/platform/browser/test.html
@@ -0,0 +1,71 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>uBO Static Network Filtering Engine</title>
+</head>
+<body>
+<script type="module">
+ import {
+ FilteringContext,
+ enableWASM,
+ pslInit,
+ restart,
+ } from './main.js';
+
+ (async ( ) => {
+ await enableWASM('.');
+
+ await fetch('./data/effective_tld_names.dat').then(response => {
+ return response.text();
+ }).then(pslRaw => {
+ pslInit(pslRaw);
+ });
+
+ const snfe = await Promise.all([
+ fetch('./data/easylist.txt').then(response => {
+ return response.text();
+ }),
+ fetch('./data/easyprivacy.txt').then(response => {
+ return response.text();
+ }),
+ ]).then(rawLists => {
+ return restart([
+ { name: 'easylist', raw: rawLists[0] },
+ { name: 'easyprivacy', raw: rawLists[1] },
+ ]);
+ });
+
+ // Reuse filtering context: it's what uBO does
+ const fctxt = new FilteringContext();
+
+ // Tests
+ // Not blocked
+ fctxt.setDocOriginFromURL('https://www.bloomberg.com/');
+ fctxt.setURL('https://www.bloomberg.com/tophat/assets/v2.6.1/that.css');
+ fctxt.setType('stylesheet');
+ if ( snfe.matchRequest(fctxt) !== 0 ) {
+ console.log(snfe.toLogData());
+ }
+
+ // Blocked
+ fctxt.setDocOriginFromURL('https://www.bloomberg.com/');
+ fctxt.setURL('https://securepubads.g.doubleclick.net/tag/js/gpt.js');
+ fctxt.setType('script');
+ if ( snfe.matchRequest(fctxt) !== 0 ) {
+ console.log(snfe.toLogData());
+ }
+
+ // Unblocked
+ fctxt.setDocOriginFromURL('https://www.bloomberg.com/');
+ fctxt.setURL('https://sourcepointcmp.bloomberg.com/ccpa.js');
+ fctxt.setType('script');
+ if ( snfe.matchRequest(fctxt) !== 0 ) {
+ console.log(snfe.toLogData());
+ }
+
+ restart();
+ })();
+</script>
+</body>
+</html>