<commit_before>// ==UserScript==
// @name        NATOBot Controls
// @namespace   http://tinygiant.io
// @description Adds quick links for [tp|fp|ne] to NATOBot reports.
// @include     http*://chat.stackoverflow.com/rooms/111347/sobotics
// @version     1.0.0.0
// @grant       none
// ==/UserScript==
/* jshint esnext: true */

const ready = CHAT.Hub.roomReady;

CHAT.Hub.roomReady = {
    fire: function(...args) {
        ready.fire(...args);

        function eventHandler(event) {
            if(event.room_id !== CHAT.CURRENT_ROOM_ID || // event is not in this room
               event.event_type !== 1                   || // event is not a message
               event.user_id !== 6817005) return;        // event is not from NATOBot

            const content = document.createElement('div');
            content.innerHTML = event.content;

            if(!/^\[ NATOBot/.test(content.textContent.trim())) return; // event is not a report

            function send(message) {
                $.ajax({
                    'type': 'POST',
                    'url': `/chats/${CHAT.CURRENT_ROOM_ID}/messages/new`,
                    'data': fkey({text: message}),
                    'dataType': 'json'
                });
            }

            function clickHandler(message) {
                return function(event) {
                    event.preventDefault();

                    send(message);
                };
            }

            function createLink(message) {
                const node = document.createElement('a');
                node.href = "#";
                node.textContent = message;
                node.addEventListener('click', clickHandler(`:${event.message_id} ${message}`), false);
                return node;
            }

            setTimeout(() => {
                const message = document.querySelector(`#message-${event.message_id} .content`);

                const wrap = document.createElement('span');
                wrap.appendChild(document.createTextNode(' [ '));
                wrap.appendChild(createLink('tp'));
                wrap.appendChild(document.createTextNode(' | '));
                wrap.appendChild(createLink('fp'));
                wrap.appendChild(document.createTextNode(' | '));
                wrap.appendChild(createLink('ne'));
                wrap.appendChild(document.createTextNode(' ] '));
                message.insertBefore(wrap, message.firstChild);
            }, 0);
        }

        function handleLoadedEvents(handler) {
            [...(document.querySelectorAll('.user-container') || [])].forEach(container => {
                [...(container.querySelectorAll('.message') || [])].forEach(message => handler({
                    room_id: CHAT.CURRENT_ROOM_ID,
                    event_type: 1,
                    user_id: +(container.className.match(/user-(\d+)/) || [])[1],
                    message_id: +(message.id.match(/message-(\d+)/) || [])[1],
                    content: message.querySelector('.content').innerHTML
                }));
            });
        }

        CHAT.addEventHandlerHook(eventHandler);

        handleLoadedEvents(eventHandler);
    }
};
<commit_msg>Inject script into page for safari<commit_after>// ==UserScript==
// @name        NATOBot Controls
// @namespace   http://tinygiant.io
// @description Adds quick links for [tp|fp|ne] to NATOBot reports.
// @include     http*://chat.stackoverflow.com/rooms/111347/sobotics
// @version     1.0.0.1
// @grant       none
// ==/UserScript==
/* jshint esnext: true */

function NATOBotControls() {
    const ready = CHAT.Hub.roomReady;

    CHAT.Hub.roomReady = {
        fire: function(...args) {
            ready.fire(...args);

            function eventHandler(event) {
                if(event.room_id !== CHAT.CURRENT_ROOM_ID || // event is not in this room
                   event.event_type !== 1                   || // event is not a message
                   event.user_id !== 6817005) return;        // event is not from NATOBot

                const content = document.createElement('div');
                content.innerHTML = event.content;

                if(!/^\[ NATOBot/.test(content.textContent.trim())) return; // event is not a report

                function send(message) {
                    $.ajax({
                        'type': 'POST',
                        'url': `/chats/${CHAT.CURRENT_ROOM_ID}/messages/new`,
                        'data': fkey({text: message}),
                        'dataType': 'json'
                    });
                }

                function clickHandler(message) {
                    return function(event) {
                        event.preventDefault();

                        send(message);
                    };
                }

                function createLink(message) {
                    const node = document.createElement('a');
                    node.href = "#";
                    node.textContent = message;
                    node.addEventListener('click', clickHandler(`:${event.message_id} ${message}`), false);
                    return node;
                }

                setTimeout(() => {
                    const message = document.querySelector(`#message-${event.message_id} .content`);

                    const wrap = document.createElement('span');
                    wrap.appendChild(document.createTextNode(' [ '));
                    wrap.appendChild(createLink('tp'));
                    wrap.appendChild(document.createTextNode(' | '));
                    wrap.appendChild(createLink('fp'));
                    wrap.appendChild(document.createTextNode(' | '));
                    wrap.appendChild(createLink('ne'));
                    wrap.appendChild(document.createTextNode(' ] '));
                    message.insertBefore(wrap, message.firstChild);
                }, 0);
            }

            function handleLoadedEvents(handler) {
                [...(document.querySelectorAll('.user-container') || [])].forEach(container => {
                    [...(container.querySelectorAll('.message') || [])].forEach(message => handler({
                        room_id: CHAT.CURRENT_ROOM_ID,
                        event_type: 1,
                        user_id: +(container.className.match(/user-(\d+)/) || [])[1],
                        message_id: +(message.id.match(/message-(\d+)/) || [])[1],
                        content: message.querySelector('.content').innerHTML
                    }));
                });
            }

            CHAT.addEventHandlerHook(eventHandler);

            handleLoadedEvents(eventHandler);
        }
    };
}

const script = document.createElement('script');
script.textContent = `(${ NATOBotControls.toString() })();`;
console.log(script.textContent);
document.body.appendChild(script);

{'fraction_non_alphanumeric': 0.14185741361714535, 'fraction_numerical': 0.006414929289983963, 'mean_word_length': 1.5473449684366878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7023728', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1679, 'n_words': 298}