// ==UserScript== // @name Futaba 不正画像リンクレス非表示 // @namespace futaba-spam-filter // @version 1.6.0 // @description 画像添付+スレッドリンクのレスを検出・非表示 // @match https://img.2chan.net/b/res/*.htm // @match https://kako.futakuro.com/futa/img_b/* // @run-at document-idle // @grant none // ==/UserScript== (() => { 'use strict'; if (window.__FUTABA_SPAM_FILTER_16__) return; window.__FUTABA_SPAM_FILTER_16__ = true; const CONFIG = { autoHide: true, observeNewPosts: true, delInterval: 3000, confirmDel: true }; const isArchive = location.hostname === 'kako.futakuro.com'; const currentThread = location.pathname.match(/\/res\/(\d+)\.htm/i)?.[1] || location.pathname.match(/\/img_b\/(\d+)/i)?.[1]; const states = new Map(); const delQueue = []; let delRunning = false; const delHistoryKey = `futaba-spam-filter-del/${location.host}`; let delHistory = []; try { delHistory = JSON.parse( localStorage.getItem(delHistoryKey) || '[]' ); } catch { delHistory = []; } // ---------------------------------- // 検出 // ---------------------------------- // 本家用:v1.5の判定を維持 function hasAttachedImageOriginal(post) { return [...post.querySelectorAll('a[href],img[src]')] .some(el => { const url = el.tagName === 'IMG' ? el.getAttribute('src') || '' : el.getAttribute('href') || ''; return /\.(jpg|jpeg|png|gif|webp|avif)(?:[?#].*)?$/i .test(url); }); } function hasThreadLinkOriginal(post) { return [...post.querySelectorAll('a[href]')] .some(a => { try { const u = new URL(a.href); const originalThread = /\/b\/res\/\d+\.htm$/i.test(u.pathname); const archiveThread = /\/futa\/img_b\/\d+\/?$/i.test(u.pathname); return originalThread || archiveThread; } catch { return false; } }); } // 過去ログ用: // 引用部分の画像リンク・ファイル名は添付画像と見なさない function hasAttachedImageArchive(post) { const elements = post.querySelectorAll('a[href], img[src]'); return [...elements].some(el => { // 引用文の中にあるものは除外 if (el.closest('blockquote')) return false; const url = el.tagName === 'IMG' ? el.getAttribute('src') || '' : el.getAttribute('href') || ''; return /\.(jpg|jpeg|png|gif|webp|avif)(?:[?#].*)?$/i .test(url); }); } // 過去ログ用: // 引用部分に実際のスレッドURLリンクがあるか確認 function hasThreadLinkArchive(post) { const links = post.querySelectorAll('blockquote a[href]'); return [...links].some(a => { try { const u = new URL(a.href); const originalThread = /\/b\/res\/\d+\.htm$/i.test(u.pathname); const archiveThread = /\/futa\/img_b\/\d+\/?$/i.test(u.pathname); return originalThread || archiveThread; } catch { return false; } }); } function hasAttachedImage(post) { return isArchive ? hasAttachedImageArchive(post) : hasAttachedImageOriginal(post); } function hasThreadLink(post) { return isArchive ? hasThreadLinkArchive(post) : hasThreadLinkOriginal(post); } function getResNumber(post) { const cno = post.querySelector('.cno'); const match = cno?.textContent.match(/No\.(\d+)/); if (match) return match[1]; const text = post.textContent || ''; const fallback = text.match(/No\.(\d+)/); return fallback ? fallback[1] : null; } // ---------------------------------- // 操作ボタン // ---------------------------------- function createButton(label, callback) { const b = document.createElement('button'); b.type = 'button'; b.textContent = label; b.style.cssText = ` margin-left:6px; padding:3px 9px; cursor:pointer; font-size:13px; `; b.addEventListener('click', e => { e.preventDefault(); e.stopPropagation(); callback(); }); return b; } function createPanel(state) { document.querySelectorAll( `[data-ff-panel="${state.resNo}"]` ).forEach(el => el.remove()); const tr = document.createElement('tr'); tr.className = 'futaba-filter-panel'; tr.dataset.ffPanel = state.resNo; const td = document.createElement('td'); td.colSpan = Math.max(1, state.row.cells.length); td.style.cssText = ` padding:5px 8px; border:1px solid #aaa; background:rgba(128,128,128,.12); font-size:13px; `; tr.appendChild(td); state.row.parentNode.insertBefore( tr, state.row.nextSibling ); state.panel = tr; state.panelCell = td; updatePanel(state); } function updatePanel(state) { if (!state.panelCell) return; const cell = state.panelCell; cell.replaceChildren(); const label = document.createElement('span'); label.textContent = state.hidden ? '削除済' : '検出対象'; label.style.color = state.hidden ? '#a22' : '#777'; const toggle = createButton( state.hidden ? '表示' : '非表示', () => { if (state.hidden) { showPost(state); } else { hidePost(state); } } ); const del = createButton( state.delDone ? 'del済' : state.delRunning ? 'del中…' : '削除依頼(del)', () => requestDel(state) ); if (isArchive) { del.title = '過去ログでは削除依頼を実行できません'; } del.disabled = state.delDone || state.delRunning; cell.append(label, toggle, del); } // ---------------------------------- // 表示・非表示 // ---------------------------------- function hidePost(state) { if (state.hidden) return; state.hidden = true; state.row.style.display = 'none'; updatePanel(state); } function showPost(state) { if (!state.hidden) return; state.hidden = false; state.row.style.display = state.originalDisplay; updatePanel(state); } // ---------------------------------- // 本家のdel処理 // ---------------------------------- async function postDel(resNo) { const boardId = location.pathname .split('/') .filter(Boolean)[0]; const response = await fetch( `${location.protocol}//${location.host}/del.php`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: `mode=post&b=${encodeURIComponent(boardId)}` + `&d=${encodeURIComponent(resNo)}` + `&reason=110&responsemode=ajax`, credentials: 'same-origin' } ); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } const buffer = await response.arrayBuffer(); return new TextDecoder('shift-jis') .decode(buffer) .trim(); } function saveDelHistory(resNo) { if (!delHistory.includes(resNo)) { delHistory.push(resNo); } if (delHistory.length > 1000) { delHistory.shift(); } localStorage.setItem( delHistoryKey, JSON.stringify(delHistory) ); } function requestDel(state) { if (isArchive) { alert('過去ログでは削除依頼できません。'); return; } if (state.delDone || state.delRunning) return; if ( CONFIG.confirmDel && !confirm(`No.${state.resNo} を削除依頼しますか?`) ) { return; } if (delQueue.some(s => s.resNo === state.resNo)) { return; } delQueue.push(state); runDelQueue(); } async function runDelQueue() { if (delRunning) return; delRunning = true; while (delQueue.length) { const state = delQueue.shift(); if (state.delDone) continue; state.delRunning = true; updatePanel(state); try { const result = await postDel(state.resNo); if ( result === 'ok' || result.includes( '同じIPアドレスからの削除依頼があります' ) ) { state.delDone = true; saveDelHistory(state.resNo); alert( result === 'ok' ? `No.${state.resNo} の削除依頼を送信しました。` : result ); } else { alert( `No.${state.resNo}\n削除依頼の結果:\n${result}` ); } } catch (error) { alert( `No.${state.resNo} のdelに失敗しました。\n${error}` ); } finally { state.delRunning = false; updatePanel(state); } if (delQueue.length) { await new Promise(resolve => setTimeout(resolve, CONFIG.delInterval) ); } } delRunning = false; } // ---------------------------------- // レス登録 // ---------------------------------- function processPost(post) { if (!post.matches('td.rtd')) return; const resNo = getResNumber(post); if (!resNo) return; const existing = states.get(resNo); if (existing) { if (existing.row.isConnected) return; if (existing.panel?.isConnected) { existing.panel.remove(); } existing.row = post.closest('tr'); if (!existing.row) return; existing.originalDisplay = existing.row.style.display; existing.panel = null; existing.panelCell = null; createPanel(existing); if (existing.hidden) { existing.row.style.display = 'none'; } return; } // 同じレス要素の中に画像とスレッドURLの両方が必要 if ( !hasAttachedImage(post) || !hasThreadLink(post) ) { return; } const row = post.closest('tr'); if (!row || !row.parentNode) return; const state = { resNo, row, originalDisplay: row.style.display, hidden: false, delDone: !isArchive && delHistory.includes(resNo), delRunning: false, panel: null, panelCell: null }; states.set(resNo, state); createPanel(state); if (CONFIG.autoHide) { hidePost(state); } } // ---------------------------------- // スキャン // ---------------------------------- function scan(root = document) { if (!root) return; if (root.matches?.('td.rtd')) { processPost(root); } root.querySelectorAll?.('td.rtd') .forEach(processPost); } scan(); // ---------------------------------- // DOM変更監視 // ---------------------------------- if (CONFIG.observeNewPosts) { const thread = document.querySelector('.thre') || document.body; const observer = new MutationObserver(records => { for (const record of records) { for (const node of record.addedNodes) { if (node.nodeType !== Node.ELEMENT_NODE) { continue; } if ( node.matches?.('.futaba-filter-panel') || node.closest?.('.futaba-filter-panel') ) { continue; } scan(node); } } }); observer.observe(thread, { childList: true, subtree: true }); } console.log( `[Futaba Filter] v1.6 initialized: ` + `${isArchive ? 'Archive' : 'Original'} / ${states.size} posts` ); })();