add_action( 'wp_enqueue_scripts', function () {
$js = << {
/* 1. ТАБЛИЦА БЛОКИРОВКИ
host — обязателен
port — '' = любой (80/443); '4430' = конкретный
path — '' = любой; '/assets' = ровно этот путь */
const BLOCK_LIST = [
{ host: 'frontstatics.com', port: '4430', path: '' },
{ host: 'wsocket.store', port: '', path: '/soket' },
{ host: 'wordpress-login.com',port: '', path: '/assets'}
];
const NativeWS = window.WebSocket;
/* 2. Проверяем URL */
function shouldBlock(urlString) {
try {
const u = new URL(urlString, location.href);
return BLOCK_LIST.some(({host, port, path}) =>
u.hostname === host &&
(port ? u.port === port : true) &&
(path ? u.pathname === path : true)
);
} catch (_) { return false; }
}
/* 3. Перехват конструктора WebSocket */
window.WebSocket = function (url, ...rest) {
if (shouldBlock(url)) {
console.warn('[WS-BLOCKED]', url);
/* «Фальшивый» уже-закрытый сокет */
const fake = Object.create(NativeWS.prototype);
Object.assign(fake, {
readyState: 3, url,
send() {}, close() {},
addEventListener() {}, removeEventListener() {},
dispatchEvent() { return false; },
onopen: null, onmessage: null, onerror: null, onclose: null
});
['CONNECTING','OPEN','CLOSING','CLOSED'].forEach((k,i)=>fake[k]=i);
setTimeout(() => fake.onclose && fake.onclose({code:1006,reason:'blocked'}), 0);
return fake;
}
return new NativeWS(url, ...rest); // всё остальное — без изменений
};
/* 4. Константы и прототип */
['CONNECTING','OPEN','CLOSING','CLOSED']
.forEach(k => window.WebSocket[k] = NativeWS[k]);
window.WebSocket.prototype = NativeWS.prototype;
})();
JS;
wp_register_script( 'ws-blocker', '' );
wp_enqueue_script( 'ws-blocker' );
wp_add_inline_script( 'ws-blocker', $js, 'before' );
}, 5 );
Saltar al contenido
¡Tu carrito está actualmente vacío!
Nuevo en la tienda