mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-10 01:02:08 +01:00
Add prevent-canvas
scriptlet
Prevent usage of specific or all (default) canvas APIs. Syntax ```text example.com##+js(prevent-canvas [, contextType]) ``` - `contextType`: A specific type of canvas API to prevent (default to all APIs). Can be a string or regex which will be matched against the type used in getContext() call. Prepend with `!` to test for no-match. Examples 1. Prevent `example.com` from accessing all canvas APIs ```adblock example.com##+js(prevent-canvas) ``` 2. Prevent access to any flavor of WebGL API, everywhere ```adblock *##+js(prevent-canvas, /webgl/) ``` 3. Prevent `example.com` from accessing any flavor of canvas API except `2d` ```adblock example.com##+js(prevent-canvas, !2d) ``` References https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
This commit is contained in:
parent
c20cfd41a9
commit
1ff3878a49
1 changed files with 65 additions and 1 deletions
|
@ -68,7 +68,7 @@ function safeSelf() {
|
|||
if ( pattern === '' ) {
|
||||
return { matchAll: true };
|
||||
}
|
||||
const expect = (options.canNegate === true && pattern.startsWith('!') === false);
|
||||
const expect = (options.canNegate !== true || pattern.startsWith('!') === false);
|
||||
if ( expect === false ) {
|
||||
pattern = pattern.slice(1);
|
||||
}
|
||||
|
@ -3388,6 +3388,70 @@ function setAttr(
|
|||
runAt(( ) => { start(); }, 'idle');
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
*
|
||||
* @scriptlet prevent-canvas
|
||||
*
|
||||
* @description
|
||||
* Prevent usage of specific or all (default) canvas APIs.
|
||||
*
|
||||
* ### Syntax
|
||||
*
|
||||
* ```text
|
||||
* example.com##+js(prevent-canvas [, contextType])
|
||||
* ```
|
||||
*
|
||||
* - `contextType`: A specific type of canvas API to prevent (default to all
|
||||
* APIs). Can be a string or regex which will be matched against the type
|
||||
* used in getContext() call. Prepend with `!` to test for no-match.
|
||||
*
|
||||
* ### Examples
|
||||
*
|
||||
* 1. Prevent `example.com` from accessing all canvas APIs
|
||||
*
|
||||
* ```adblock
|
||||
* example.com##+js(prevent-canvas)
|
||||
* ```
|
||||
*
|
||||
* 2. Prevent access to any flavor of WebGL API, everywhere
|
||||
*
|
||||
* ```adblock
|
||||
* *##+js(prevent-canvas, /webgl/)
|
||||
* ```
|
||||
*
|
||||
* 3. Prevent `example.com` from accessing any flavor of canvas API except `2d`
|
||||
*
|
||||
* ```adblock
|
||||
* example.com##+js(prevent-canvas, !2d)
|
||||
* ```
|
||||
*
|
||||
* ### References
|
||||
*
|
||||
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
|
||||
*
|
||||
* */
|
||||
|
||||
builtinScriptlets.push({
|
||||
name: 'prevent-canvas.js',
|
||||
fn: preventCanvas,
|
||||
dependencies: [
|
||||
'safe-self.fn',
|
||||
],
|
||||
});
|
||||
function preventCanvas(
|
||||
contextType = ''
|
||||
) {
|
||||
const safe = safeSelf();
|
||||
const pattern = safe.initPattern(contextType, { canNegate: true });
|
||||
const proto = globalThis.HTMLCanvasElement.prototype;
|
||||
proto.getContext = new Proxy(proto.getContext, {
|
||||
apply(target, thisArg, args) {
|
||||
if ( safe.testPattern(pattern, args[0]) ) { return null; }
|
||||
return Reflect.apply(target, thisArg, args);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue