mirror of https://github.com/gorhill/uBlock.git
Further work on JS modules
Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/1664
This commit is contained in:
parent
f240e3caf5
commit
c4b7ee80ea
|
@ -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
|
||||
},
|
||||
|
|
|
@ -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() {
|
||||
|
@ -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;
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
/******************************************************************************/
|
||||
|
||||
import webext from './webext.js';
|
||||
import { ubolog } from './console.js';
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -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`);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
|
|
@ -21,4 +21,4 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
const webext = browser; // jshint ignore:line
|
||||
export default browser;
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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';
|
||||
|
@ -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`);
|
||||
});
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
|
||||
import lz4Codec from './lz4.js';
|
||||
import µb from './background.js';
|
||||
import webext from './webext.js';
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
|
|
@ -23,8 +23,8 @@
|
|||
|
||||
/******************************************************************************/
|
||||
|
||||
import { hostnameFromURI } from './uri-utils.js';
|
||||
import µb from './background.js';
|
||||
import { hostnameFromURI } from './uri-utils.js';
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
|
||||
/******************************************************************************/
|
||||
|
||||
import './utils.js';
|
||||
import logger from './logger.js';
|
||||
import µb from './background.js';
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
@ -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':
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -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';
|
||||
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -1132,6 +1132,8 @@ const strictBlockBypasser = {
|
|||
/******************************************************************************/
|
||||
|
||||
const webRequest = {
|
||||
onBeforeRequest,
|
||||
|
||||
start: (( ) => {
|
||||
vAPI.net = new vAPI.Net();
|
||||
vAPI.net.suspend();
|
||||
|
@ -1155,6 +1157,6 @@ const webRequest = {
|
|||
|
||||
/******************************************************************************/
|
||||
|
||||
export { webRequest };
|
||||
export default webRequest;
|
||||
|
||||
/******************************************************************************/
|
||||
|
|
|
@ -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 = [];
|
||||
|
@ -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
|
||||
};
|
||||
})();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
|
Loading…
Reference in New Issue