summaryrefslogtreecommitdiff
path: root/irclog/search/search1.js
blob: cf59f90b6b7dc4ba53473ae6094335e338899ec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const logContainer = document.getElementById('log-container');
const queryInput = document.getElementById('query');

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
let query = urlParams.get('q');
if (query === null) {
    query = '';
} else {
    queryInput.value = query.trim();
}

logContainer.innerHTML = '';

function escapeHtml(unsafe) {
    if (!unsafe) return '';
    return unsafe
        .replace(/&/g, "&")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#039;");
}

function ircAction(text) {
    const regex =
    /^(\[[^\]]+\])(\s*<)([^>]+)(>:\s*)(\u0001ACTION\s+)([^\u0001]+)(\u0001)(.*)$/gm;
    const replacement = '$1 * $3 $6$8';
    return text.replace(regex, replacement);
}			  

function linkify(text) {
    const urlRegex = /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(urlRegex, function(url) {
        return `<a href="${url}" target="_blank">${url}</a>`;
    });
}

function aggrLog(data) {
    const logsByDate = {};
    const lines = data.trim().split('\n');

    lines.forEach(line => {
        const match = line.match(/^\.\/(\d{4}\/\d{2}-\d{2})\.txt:(.*)/);
        if (match) {
            const fullDate = match[1].replace('/', '-');
            const logContent = match[2].trim();
            if (!logsByDate[fullDate]) {
                logsByDate[fullDate] = [];
            }
            logsByDate[fullDate].push(logContent);
        }
    });
    const dates = Object.keys(logsByDate);
    const sortedDates = dates.sort().reverse();
    let ret = '';
    sortedDates.forEach(date => {
        year = date.substring(0, 4);
        month = date.substring(5, 7);
        day = date.substring(8, 10);
        url = '../view/?chan=main&y=' + year + '&m=' + month + '&d=' + day;
        ret = ret + '\n=== ' +  date + ' ===' + '\n';
        logsByDate[date].forEach(log => {
            let time = log.substring(1,9);
            let logurl = url + '#' + time;
            ret = ret + '<a class="logline" target="_blank" href="' + logurl + '">'  + log + '</a>\n';
        });
    });
    return ret;
}

function logProcess(text) {
    text = ircAction(text);
    text = escapeHtml(text);
    text = aggrLog(text);
    text = linkify(text);
    return text;
}

function loadLog() {
    if (query === '') return;
    let targetUrl = 'https://raye.mistivia.com/cgi-bin/irclogsearch/?' + encodeURIComponent(query);
    fetch(targetUrl)
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP Error: ${response.status} ${response.statusText} for hash: ${urlHash}`);
            }
            return response.text();
        })
        .then(text => {
            try {
                logContainer.innerHTML = `<pre>${logProcess(text.trim())}</pre>`;
            } catch (e) {
                console.log(e);
            }
            
        })
        .catch(error => {
            console.error('Fetch error:', error);
            logContainer.innerHTML = '';
        });
}

loadLog();

function search() {
    query = queryInput.value.trim();
    window.history.replaceState(null, '', window.location.origin + window.location.pathname + '?q=' + encodeURIComponent(query));
    loadLog();
}

function handleEnter(event) {
    if (event.key === 'Enter' || event.keyCode === 13) {
        event.preventDefault();
        search(); 
    }
}

queryInput.addEventListener('keyup', handleEnter);