Skip to content

Commit

Permalink
Further work on JS modules
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Oct 7, 2021
1 parent f240e3c commit c4b7ee8
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 145 deletions.
4 changes: 0 additions & 4 deletions .jshintrc
Expand Up @@ -6,12 +6,8 @@
"globals": {
"browser": false, // global variable in Firefox, Edge
"chrome": false, // global variable in Chromium, Chrome, Opera
"log": false,
"safari": false,
"self": false,
"vAPI": false,
"webext": false,
"µBlock": false,
"URLSearchParams": false,
"WebAssembly": false
},
Expand Down
8 changes: 1 addition & 7 deletions platform/chromium/webext.js
Expand Up @@ -24,9 +24,6 @@
// `webext` is a promisified api of `chrome`. Entries are added as
// the promisification of uBO progress.

const webext = (( ) => { // jshint ignore:line
// >>>>> start of private scope

const promisifyNoFail = function(thisArg, fnName, outFn = r => r) {
const fn = thisArg[fnName];
return function() {
Expand Down Expand Up @@ -164,7 +161,4 @@ if ( chrome.tabs.removeCSS instanceof Function ) {
webext.tabs.removeCSS = promisifyNoFail(chrome.tabs, 'removeCSS');
}

return webext;

// <<<<< end of private scope
})();
export default webext;
45 changes: 1 addition & 44 deletions platform/common/vapi-background.js
Expand Up @@ -26,6 +26,7 @@

/******************************************************************************/

import webext from './webext.js';
import { ubolog } from './console.js';

/******************************************************************************/
Expand Down Expand Up @@ -1252,50 +1253,6 @@ vAPI.Net = class {
canSuspend() {
return false;
}
async benchmark() {
if ( typeof µBlock !== 'object' ) { return; }
const requests = await µBlock.loadBenchmarkDataset();
if ( Array.isArray(requests) === false || requests.length === 0 ) {
console.info('No requests found to benchmark');
return;
}
const mappedTypes = new Map([
[ 'document', 'main_frame' ],
[ 'subdocument', 'sub_frame' ],
]);
console.info('vAPI.net.onBeforeSuspendableRequest()...');
const t0 = self.performance.now();
const promises = [];
const details = {
documentUrl: '',
tabId: -1,
parentFrameId: -1,
frameId: 0,
type: '',
url: '',
};
for ( const request of requests ) {
details.documentUrl = request.frameUrl;
details.tabId = -1;
details.parentFrameId = -1;
details.frameId = 0;
details.type = mappedTypes.get(request.cpt) || request.cpt;
details.url = request.url;
if ( details.type === 'main_frame' ) { continue; }
promises.push(this.onBeforeSuspendableRequest(details));
}
return Promise.all(promises).then(results => {
let blockCount = 0;
for ( const r of results ) {
if ( r !== undefined ) { blockCount += 1; }
}
const t1 = self.performance.now();
const dur = t1 - t0;
console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`);
console.info(`\tBlocked ${blockCount} requests`);
console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`);
});
}
};

/******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion platform/firefox/webext.js
Expand Up @@ -21,4 +21,4 @@

'use strict';

const webext = browser; // jshint ignore:line
export default browser;
10 changes: 0 additions & 10 deletions src/background.html
Expand Up @@ -6,17 +6,7 @@
</head>
<body>
<script src="lib/lz4/lz4-block-codec-any.js"></script>
<script src="js/webext.js"></script>
<script src="js/vapi.js"></script>
<script src="js/vapi-common.js"></script>
<script src="js/vapi-background.js" type="module"></script>
<script src="js/vapi-background-ext.js" type="module"></script><!-- platform-specific to extend common code paths -->
<script src="js/utils.js" type="module"></script>
<script src="js/ublock.js" type="module"></script>
<script src="js/storage.js" type="module"></script>
<script src="js/tab.js" type="module"></script>
<script src="js/messaging.js" type="module"></script>
<script src="js/start.js" type="module"></script>
<script src="js/commands.js" type="module"></script>
</body>
</html>
47 changes: 47 additions & 0 deletions src/js/benchmarks.js
Expand Up @@ -28,6 +28,7 @@ import io from './assets.js';
import scriptletFilteringEngine from './scriptlet-filtering.js';
import staticNetFilteringEngine from './static-net-filtering.js';
import µb from './background.js';
import webRequest from './traffic.js';
import { FilteringContext } from './filtering-context.js';
import { LineIterator } from './text-utils.js';
import { sessionFirewall } from './filtering-engines.js';
Expand Down Expand Up @@ -336,3 +337,49 @@ const loadBenchmarkDataset = (( ) => {
};

/******************************************************************************/

µb.benchmarkOnBeforeRequest = async function() {
const requests = await loadBenchmarkDataset();
if ( Array.isArray(requests) === false || requests.length === 0 ) {
console.info('No requests found to benchmark');
return;
}
const mappedTypes = new Map([
[ 'document', 'main_frame' ],
[ 'subdocument', 'sub_frame' ],
]);
console.info('webRequest.onBeforeRequest()...');
const t0 = self.performance.now();
const promises = [];
const details = {
documentUrl: '',
tabId: -1,
parentFrameId: -1,
frameId: 0,
type: '',
url: '',
};
for ( const request of requests ) {
details.documentUrl = request.frameUrl;
details.tabId = -1;
details.parentFrameId = -1;
details.frameId = 0;
details.type = mappedTypes.get(request.cpt) || request.cpt;
details.url = request.url;
if ( details.type === 'main_frame' ) { continue; }
promises.push(webRequest.onBeforeRequest(details));
}
return Promise.all(promises).then(results => {
let blockCount = 0;
for ( const r of results ) {
if ( r !== undefined ) { blockCount += 1; }
}
const t1 = self.performance.now();
const dur = t1 - t0;
console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`);
console.info(`\tBlocked ${blockCount} requests`);
console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`);
});
};

/******************************************************************************/
1 change: 1 addition & 0 deletions src/js/cachestorage.js
Expand Up @@ -27,6 +27,7 @@

import lz4Codec from './lz4.js';
import µb from './background.js';
import webext from './webext.js';

/******************************************************************************/

Expand Down
2 changes: 1 addition & 1 deletion src/js/commands.js
Expand Up @@ -23,8 +23,8 @@

/******************************************************************************/

import { hostnameFromURI } from './uri-utils.js';
import µb from './background.js';
import { hostnameFromURI } from './uri-utils.js';

/******************************************************************************/

Expand Down
1 change: 1 addition & 0 deletions src/js/cosmetic-filtering.js
Expand Up @@ -23,6 +23,7 @@

/******************************************************************************/

import './utils.js';
import logger from './logger.js';
import µb from './background.js';

Expand Down
8 changes: 6 additions & 2 deletions src/js/messaging.js
Expand Up @@ -36,10 +36,10 @@ import staticExtFilteringEngine from './static-ext-filtering.js';
import staticFilteringReverseLookup from './reverselookup.js';
import staticNetFilteringEngine from './static-net-filtering.js';
import µb from './background.js';
import webRequest from './traffic.js';
import { denseBase64 } from './base64-custom.js';
import { redirectEngine } from './redirect-engine.js';
import { StaticFilteringParser } from './static-filtering-parser.js';
import { webRequest } from './traffic.js';

import {
permanentFirewall,
Expand Down Expand Up @@ -129,7 +129,11 @@ const onMessage = function(request, sender, callback) {
return;

case 'scriptlet':
µb.scriptlets.inject(request.tabId, request.scriptlet, callback);
vAPI.tabs.executeScript(request.tabId, {
file: `/js/scriptlets/${request.scriptlet}.js`
}).then(result => {
callback(result);
});
return;

case 'sfneBenchmark':
Expand Down
6 changes: 5 additions & 1 deletion src/js/pagestore.js
Expand Up @@ -27,6 +27,7 @@ import contextMenu from './contextmenu.js';
import logger from './logger.js';
import staticNetFilteringEngine from './static-net-filtering.js';
import µb from './background.js';
import webext from './webext.js';
import { orphanizeString } from './text-utils.js';
import { redirectEngine } from './redirect-engine.js';

Expand Down Expand Up @@ -639,7 +640,10 @@ const PageStore = class {
} else {
this.allowLargeMediaElementsUntil = Date.now();
}
µb.scriptlets.injectDeep(this.tabId, 'load-large-media-all');
vAPI.tabs.executeScript(this.tabId, {
file: '/js/scriptlets/load-large-media-all.js',
allFrames: true,
});
}

// https://github.com/gorhill/uBlock/issues/2053
Expand Down
14 changes: 13 additions & 1 deletion src/js/start.js
Expand Up @@ -23,6 +23,18 @@

/******************************************************************************/

import './vapi-common.js';
import './vapi-background.js';
import './vapi-background-ext.js';

// The following modules are loaded here until their content is better organized
import './commands.js';
import './messaging.js';
import './storage.js';
import './tab.js';
import './ublock.js';
import './utils.js';

import cacheStorage from './cachestorage.js';
import contextMenu from './contextmenu.js';
import io from './assets.js';
Expand All @@ -31,9 +43,9 @@ import staticExtFilteringEngine from './static-ext-filtering.js';
import staticFilteringReverseLookup from './reverselookup.js';
import staticNetFilteringEngine from './static-net-filtering.js';
import µb from './background.js';
import webRequest from './traffic.js';
import { redirectEngine } from './redirect-engine.js';
import { ubolog } from './console.js';
import { webRequest } from './traffic.js';

import {
permanentFirewall,
Expand Down
1 change: 1 addition & 0 deletions src/js/tab.js
Expand Up @@ -28,6 +28,7 @@ import logger from './logger.js';
import scriptletFilteringEngine from './scriptlet-filtering.js';
import staticNetFilteringEngine from './static-net-filtering.js';
import µb from './background.js';
import webext from './webext.js';
import { PageStore } from './pagestore.js';

import {
Expand Down
4 changes: 3 additions & 1 deletion src/js/traffic.js
Expand Up @@ -1132,6 +1132,8 @@ const strictBlockBypasser = {
/******************************************************************************/

const webRequest = {
onBeforeRequest,

start: (( ) => {
vAPI.net = new vAPI.Net();
vAPI.net.suspend();
Expand All @@ -1155,6 +1157,6 @@ const webRequest = {

/******************************************************************************/

export { webRequest };
export default webRequest;

/******************************************************************************/
75 changes: 2 additions & 73 deletions src/js/ublock.js
Expand Up @@ -627,7 +627,7 @@ const matchBucket = function(url, hostname, bucket, start) {
return bits;
};

µb.parseBlockingProfiles = (( ) => {
{
const parse = function() {
const s = µb.hiddenSettings.blockingProfiles;
const profiles = [];
Expand All @@ -648,77 +648,6 @@ const matchBucket = function(url, hostname, bucket, start) {
parse();

self.addEventListener('hiddenSettingsChanged', ( ) => { parse(); });

return parse;
})();

/******************************************************************************/

µb.scriptlets = (function() {
const pendingEntries = new Map();

const Entry = class {
constructor(tabId, scriptlet, callback) {
this.tabId = tabId;
this.scriptlet = scriptlet;
this.callback = callback;
this.timer = vAPI.setTimeout(this.service.bind(this), 1000);
}
service(response) {
if ( this.timer !== null ) {
clearTimeout(this.timer);
this.timer = null;
}
pendingEntries.delete(makeKey(this.tabId, this.scriptlet));
this.callback(response);
}
};

const makeKey = function(tabId, scriptlet) {
return tabId + ' ' + scriptlet;
};

const report = function(tabId, scriptlet, response) {
const key = makeKey(tabId, scriptlet);
const entry = pendingEntries.get(key);
if ( entry === undefined ) { return; }
entry.service(response);
};

const inject = function(tabId, scriptlet, callback) {
if ( typeof callback === 'function' ) {
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
callback();
return;
}
const key = makeKey(tabId, scriptlet);
const entry = pendingEntries.get(key);
if ( entry !== undefined ) {
if ( callback !== entry.callback ) {
callback();
}
return;
}
pendingEntries.set(key, new Entry(tabId, scriptlet, callback));
}
vAPI.tabs.executeScript(tabId, {
file: `/js/scriptlets/${scriptlet}.js`
});
};

// TODO: think about a callback mechanism.
const injectDeep = function(tabId, scriptlet) {
vAPI.tabs.executeScript(tabId, {
file: `/js/scriptlets/${scriptlet}.js`,
allFrames: true
});
};

return {
inject: inject,
injectDeep: injectDeep,
report: report
};
})();
}

/******************************************************************************/

0 comments on commit c4b7ee8

Please sign in to comment.