Skip to content

Commit fd12d01

Browse files
committed
Add prevent-dialog scriptlet
@Scriptlet prevent-dialog @description Programmatically close `dialog` elements. @param [selector] Optional. The dialog element must matches `dialog{selector}` for the prevention to take place. @Usage: example.com##+js(prevent-dialog)
1 parent 474723d commit fd12d01

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/js/resources/prevent-dialog.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*******************************************************************************
2+
3+
uBlock Origin - a comprehensive, efficient content blocker
4+
Copyright (C) 2025-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+
23+
import { registerScriptlet } from './base.js';
24+
import { safeSelf } from './safe-self.js';
25+
26+
/**
27+
* @scriptlet prevent-dialog
28+
*
29+
* @description
30+
* Programmatically close `dialog` elements.
31+
*
32+
* @param [selector]
33+
* Optional. The dialog element must matches `dialog{selector}` for the
34+
* prevention to take place.
35+
*
36+
* @usage:
37+
* example.com##+js(prevent-dialog)
38+
*
39+
* */
40+
41+
export function preventDialog(
42+
selector = '',
43+
) {
44+
const safe = safeSelf();
45+
const logPrefix = safe.makeLogPrefix('prevent-dialog', selector);
46+
const prevent = ( ) => {
47+
debouncer = undefined;
48+
const elems = document.querySelectorAll(`dialog${selector}`);
49+
for ( const elem of elems ) {
50+
if ( typeof elem.close !== 'function' ) { continue; }
51+
if ( elem.open === false ) { continue; }
52+
elem.close();
53+
safe.uboLog(logPrefix, 'Closed');
54+
}
55+
};
56+
let debouncer;
57+
const observer = new MutationObserver(( ) => {
58+
if ( debouncer !== undefined ) { return; }
59+
debouncer = requestAnimationFrame(prevent);
60+
});
61+
observer.observe(document, {
62+
attributes: true,
63+
childList: true,
64+
subtree: true,
65+
});
66+
}
67+
registerScriptlet(preventDialog, {
68+
name: 'prevent-dialog.js',
69+
dependencies: [
70+
safeSelf,
71+
],
72+
});

src/js/resources/scriptlets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import './json-edit.js';
2727
import './json-prune.js';
2828
import './noeval.js';
2929
import './object-prune.js';
30+
import './prevent-dialog.js';
3031
import './prevent-fetch.js';
3132
import './prevent-innerHTML.js';
3233
import './prevent-settimeout.js';

0 commit comments

Comments
 (0)