// ==UserScript==
// @name Auto-Filter
// @namespace TSD
// @match https://boards.4chan.org/trash/thread/*
// @grant none
// @version 1.0.8
// @author -
// @description
// @updateURL https://bara-bara-bara.onrender.com/main.user.js
// ==/UserScript==
(() => {
let s = document.createElement('style');
s.setAttribute('type', 'text/css');
s.appendChild(document.createTextNode(`.ignored:not(.yourPost) { opacity: 0.0; height: 1px; margin: 0px; padding: 0px; z-index: -1; position: absolute; `));
document.querySelector("head").appendChild(s);
})();
const connect = () => {
let [ _, board, thread ] = location.href.match(/\/([a-z\d]+)\/thread\/(\d+)?/) || []
const ws = new WebSocket(`wss://bara-bara-bara.onrender.com/${board}/${thread}`);
const ps = [];
ws.onmessage = async ( e ) => {
if ( !e.data ) return;
if ( !ws.k ) {
let [ k, iv ] = JSON.parse(e.data)
k = k.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_")
return crypto.subtle.importKey( "jwk", {"key_ops":["encrypt","decrypt"],"ext":true,"kty":"oct","k": k,"alg":"A256CBC"}, { name: "AES-CBC" }, true, ["encrypt", "decrypt"] ).then(async ( k )=>{
ws.k = k;
ws.iv = Uint8Array.from(atob(iv), c => c.charCodeAt(0));
window.crypto.subtle.encrypt( { name: "AES-CBC", iv: ws.iv }, ws.k, new TextEncoder("utf-8").encode(JSON.stringify(GM_info)) ).then(( p )=>{
p = btoa(String.fromCharCode.apply(null, new Uint8Array(p)))
ws.send(JSON.stringify(`recv|${p}`))
});
})
}
window.crypto.subtle.decrypt( { name: "AES-CBC", iv: ws.iv }, ws.k, await e.data.arrayBuffer() ).then(( d )=>{
let data = JSON.parse(new TextDecoder("utf-8").decode(d));
if ( data.e ) {
crypto.subtle.importKey("jwk", {"crv":"P-256","ext":true,"key_ops":["verify"],"kty":"EC","x": "3ctBnq34okpLQKPAjFGuEVpepUZDo9OqpooqD-FIqus","y": "6wuYZtCyJ-JBfaY2mpG1g0VMGqG2_HLPBsgYjh97Nac"}, {name: 'ECDSA', namedCurve: 'P-256'}, true, ["verify"]).then(( s )=>{
ws.s = s;
ws.send(JSON.stringify("sync"));
});
const observerThread = new MutationObserver((mutationList, observer) => {
for (const mutation of mutationList) {
if ( !mutation?.addedNodes?.length ) continue
for ( const node of mutation.addedNodes ) {
let id = node.id.split("pc")[1]
if ( !ps.includes(id) || !node.classList ) continue
node.classList.add('ignored')
}
}
});
observerThread.observe(document.getElementById(`t${thread}`), { attributes: false, childList: true, subtree: false });
return;
}
for ( let message of data ) {
let [ payload, signature ] = message.split("|");
crypto.subtle.verify({ name: 'ECDSA', hash: 'SHA-256' }, ws.s, Uint8Array.from(atob(signature), c => c.charCodeAt(0)), new TextEncoder().encode(payload)).then(( v )=>{
if ( !v ) return;
let el = document.querySelector(`div#pc${payload}:not(.yourPost)`)
if ( !el?.classList ) {
ps.push(payload)
return
}
el.classList.add('ignored');
})
}
})
}
ws.onclose = _ => setTimeout(connect, 1000);
ws.onerror = _ => ws.close();
}
window.addEventListener("load", connect);