// ==UserScript== // @name 5ch.net donguri Hit Response Getter // @namespace https://greasyfork.org/users/1310758 // @description Fetches and filters hit responses from donguri 5ch boards // @match https://donguri.5ch.net/cannonlogs // @connect 5ch.net // @license MIT License // @author pachimonta // @grant GM_xmlhttpRequest // @grant GM_addStyle // @version 2024-06-04_003 // @downloadURL none // ==/UserScript== (function() { 'use strict'; // Storage for bbs list and subject list const bbsList = {}; const subjectList = {}; const completedURL = {}; const column = ['order', 'term', 'date', 'bbs', 'bbsname', 'key', 'id', 'hunter', 'target', 'subject']; // Helper functions const $ = (selector, context = document) => context.querySelector(selector); const $$ = (selector, context = document) => [...context.querySelectorAll(selector)]; const table = $('table'); const thead = $('thead', table); const tbody = $('tbody', table); const addWeekdayToDatetime = (datetimeStr) => { const firstColonIndex = datetimeStr.indexOf(':'); const splitIndex = firstColonIndex - 2; const datePart = datetimeStr.slice(0, splitIndex); const timePart = datetimeStr.slice(splitIndex); const [year, month, day] = datePart.split('/').map(Number); const date = new Date(year, month - 1, day); const weekdays = ['日', '月', '火', '水', '木', '金', '土']; const weekday = weekdays[date.getDay()]; return `${datePart}(${weekday}) ${timePart}`; }; const appendCell = (elem, txt = null, elementName = 'td') => { if (elem.parentElement.tagName === 'THEAD') { elementName = 'th'; } const e = elem.appendChild(document.createElement(elementName)); if (txt !== null) { e.textContent = txt; } return e; }; if ($('tr th:nth-of-type(1)', thead)) { // 順,期,date(投稿時刻),bbs,bbs名,key,ハンターID,ハンター名,ターゲット,subject // order,term,date,bbs,bbsname,key,id,hunter,target,subject const tr = $('tr:nth-of-type(1)', thead); $('th:nth-of-type(1)', tr).textContent = '順'; $('th:nth-of-type(1)', tr).removeAttribute('style'); $('th:nth-of-type(2)', tr).textContent = '期'; $('th:nth-of-type(2)', tr).removeAttribute('style'); appendCell(tr, 'date(投稿時刻)'); appendCell(tr, 'bbs'); appendCell(tr, 'bbs名'); appendCell(tr, 'key'); appendCell(tr, 'ハンターID'); appendCell(tr, 'ハンター名'); appendCell(tr, 'ターゲット'); appendCell(tr, 'subject'); table.insertAdjacentHTML('beforebegin', `
bbs=newsplus
のように入力すると、マッチするログのみ表示します。#
)より後に同じように指定することでも機能します。,
) で区切ることで複数条件の指定ができます。'); table.parentNode.insertBefore(search, table); if (location.hash) { input.value = decodeURIComponent(location.hash.substring(1)); filterRows(input); } window.addEventListener('hashchange', () => { input.value = decodeURIComponent(location.hash.substring(1)); filterRows(input); }); } }; // Async function to wait until the subject list is loaded const waitForSubject = async (bbs, key) => { let retryCount = 0; while (retryCount < 30 && !(bbs in subjectList && `${key}.dat` in subjectList[bbs])) { await new Promise(resolve => setTimeout(resolve, 100)); retryCount++; } }; // GM_xmlhttpRequest wrapper to handle HTTP Get requests const lastRow = $('tr:last-child', tbody); const getDat = (url, func, mime = 'text/plain; charset=shift_jis', tr = null) => { if (typeof tr === 'object' && url in completedURL) { const date = tr.dataset.date; const bbs = tr.dataset.bbs; const key = tr.dataset.key; (async () => { await waitForSubject(bbs, key); const origin = bbsList[bbs] || "https://origin"; const bbsName = bbsList[`${bbs}_txt`] || "???"; const subject = subjectList[bbs][`${key}.dat`] || "???"; tr.dataset.origin = origin; tr.dataset.bbsname = bbsName; $('td:nth-of-type(5)', tr).textContent = bbsName; tr.dataset.subject = subject; const anchor = document.createElement('a'); anchor.href = `${origin}/test/read.cgi/${bbs}/${key}/#date=${date}`; anchor.target = '_blank'; anchor.textContent = subject; $('td:nth-of-type(10)', tr).insertAdjacentElement('beforeend', anchor); })(); return; } GM_xmlhttpRequest({ method: 'GET', url: url, timeout: 86400 * 1000, overrideMimeType: mime, onload: function(response) { if (tr !== null) { const date = tr.dataset.date; const bbs = tr.dataset.bbs; const key = tr.dataset.key; func(response, tr, bbs, key, date); if (Object.is(tr, lastRow) && $('#myfilter').value.indexOf('=') > -1) { filterRows($('#myfilter')); } } else { func(response); } }, onerror: function(error) { console.error('An error occurred during the request:', error); } }); }; const parser = new DOMParser(); const charReferRegex = /?[a-zA-Z0-9]+;?/; const crlfRegex = /[\r\n]+/; const logSplitRegex = /\s*<>\s*/; // Process subject line to update subject list and modify the row content const subjectFunc = (response, tr) => { const date = tr.dataset.date; const bbs = tr.dataset.bbs; const key = tr.dataset.key; completedURL[response.finalUrl] = true; if (response.status === 200) { const lastmodify = response.responseText; lastmodify.split(crlfRegex).forEach(line => { let [key, subject] = line.split(logSplitRegex, 2); if (charReferRegex.test(subject)) { subject = parser.parseFromString(subject, 'text/html').documentElement.innerText; } subjectList[bbs][key] = subject; }); const origin = bbsList[bbs] || "https://origin"; const bbsName = bbsList[`${bbs}_txt`] || "???"; const subject = subjectList[bbs][`${key}.dat`] || "???"; tr.dataset.origin = origin; tr.dataset.bbsname = bbsName; $('td:nth-of-type(5)', tr).textContent = bbsName; tr.dataset.subject = subject; const anchor = document.createElement('a'); anchor.href = `${origin}/test/read.cgi/${bbs}/${key}/#date=${date}`; anchor.target = '_blank'; anchor.textContent = subject; $('td:nth-of-type(10)', tr).insertAdjacentElement('beforeend', anchor); } else { console.error('Failed to load data. Status code:', response.status); } }; // Function to handle each table row for subject processing const nextFunc = async () => { Array.from($$('tr', tbody)).forEach(tr => { const bbs = tr.dataset.bbs; if (!Object.hasOwn(subjectList, bbs)) { subjectList[bbs] = {}; } getDat(`${bbsList[bbs]}/${bbs}/lastmodify.txt`, subjectFunc, 'text/plain; charset=shift_jis', tr); }); createFilterInput(); document.querySelector('table').addEventListener('dblclick', function(event) { event.preventDefault(); if (!$('#myfilter')) { return; } const target = event.target; if (target.tagName === 'TD') { const index = Array.prototype.indexOf.call(target.parentNode.children, target); const txt = `${column[index]}=${target.textContent}`; location.hash += location.hash.indexOf('=') > -1 ? `,${txt}` : txt; } }); }; const bbsLinkRegex = /\.(?:5ch\.net|bbspink\.com)\/([a-zA-Z0-9_-]+)\/$/; // Function to process the bbsmenu response const bbsmenuFunc = (response) => { if (response.status === 200) { const html = document.createElement('html'); html.innerHTML = response.responseText; $$('a[href*=".5ch.net/"],a[href*=".bbspink.com/"]', html).forEach(bbsLink => { const match = bbsLink.href.match(bbsLinkRegex); if (match) { bbsList[match[1]] = new URL(bbsLink.href).origin; bbsList[`${match[1]}_txt`] = bbsLink.textContent.trim(); } }); if (Object.keys(bbsList).length === 0) { console.error('No boards found.'); return; } nextFunc(); } else { console.error('Failed to fetch bbsmenu. Status code:', response.status); } }; // Initial data fetch from bbsmenu getDat('https://menu.5ch.net/bbsmenu.html', bbsmenuFunc, 'text/html; charset=shift_jis'); })();