Skip to content

Commit 735f61b

Browse files
committed
Add prevent-clipboard-write scriptlet
Requires a trusted source. * @Scriptlet prevent-clipboard-write * * @description * Prevent the clipboard from being overwritten. * * @PARAM needle * A pattern or regex to match against the text for the prevention to occur. * * @PARAM domAlert * Optional. A vararg to be used to alert the user in case a clipboard write * operation was prevented. The parameter is composed of two parts separated by * `|`: the first part is a CSS selector used to target a DOM element which * content will be replaced with the text found in the second part. * * @example * ##+js(prevent-clipboard-write, /^bash <<</, domAlert, body|Clickfix attempt defused) As discussed with filter list maintainers.
1 parent 496814d commit 735f61b

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*******************************************************************************
2+
3+
uBlock Origin - a browser extension to block requests.
4+
Copyright (C) 2019-present Raymond Hill
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see {http://www.gnu.org/licenses/}.
18+
19+
Home: https://github.com/gorhill/uBlock
20+
*/
21+
22+
import { proxyApplyFn } from './proxy-apply.js';
23+
import { registerScriptlet } from './base.js';
24+
import { safeSelf } from './safe-self.js';
25+
26+
/******************************************************************************/
27+
28+
/**
29+
* @scriptlet prevent-clipboard-write
30+
*
31+
* @description
32+
* Prevent the clipboard from being overwritten.
33+
*
34+
* @param needle
35+
* A pattern or regex to match against the text for the prevention to occur.
36+
*
37+
* @param domAlert
38+
* Optional. A vararg to be used to alert the user in case a clipboard write
39+
* operation was prevented. The parameter is composed of two parts separated by
40+
* `|`: the first part is a CSS selector used to target a DOM element which
41+
* content will be replaced with the text found in the second part.
42+
*
43+
* @example
44+
* ##+js(prevent-clipboard-write, /^bash <<</, domAlert, body|Clickfix attempt defused)
45+
*
46+
* */
47+
48+
function preventClipboardWrite(needle = '') {
49+
const safe = safeSelf();
50+
const logPrefix = safe.makeLogPrefix('prevent-clipboard-write');
51+
const pattern = safe.initPattern(needle);
52+
const extraArgs = safe.getExtraArgs(Array.from(arguments), 1);
53+
proxyApplyFn('navigator.clipboard.writeText', function(context) {
54+
const text = `${context.callArgs[0]}`.trim();
55+
if ( safe.testPattern(pattern, text) !== true ) {
56+
return context.reflect();
57+
}
58+
if ( extraArgs.domAlert ) {
59+
const match = /^([^|]+)\s*\|\s*(.+)/.exec(extraArgs.domAlert);
60+
if ( match ) {
61+
const elem = document.querySelector(match[1]);
62+
if ( elem ) {
63+
elem.textContent = match[2];
64+
}
65+
}
66+
}
67+
safe.uboLog(logPrefix, 'Prevented:\n\t', text);
68+
});
69+
}
70+
registerScriptlet(preventClipboardWrite, {
71+
name: 'prevent-clipboard-write.js',
72+
requiresTrust: true,
73+
dependencies: [
74+
proxyApplyFn,
75+
safeSelf,
76+
],
77+
});

src/js/resources/scriptlets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import './noeval.js';
2929
import './object-prune.js';
3030
import './prevent-addeventlistener.js';
3131
import './prevent-bab.js';
32+
import './prevent-clipboard-write.js';
3233
import './prevent-dialog.js';
3334
import './prevent-fetch.js';
3435
import './prevent-innerHTML.js';

0 commit comments

Comments
 (0)