|
| 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 | +}); |
0 commit comments