Skip to content

Instantly share code, notes, and snippets.

@dsc
Created March 20, 2021 16:56
Show Gist options
  • Save dsc/a10b38d4f6c47dfa39adde4976aa4e0e to your computer and use it in GitHub Desktop.
Save dsc/a10b38d4f6c47dfa39adde4976aa4e0e to your computer and use it in GitHub Desktop.
Greasemonkey Script to regularly truncate the chat (and improve site perf)
// ==UserScript==
// @name Teenychat TinyChat Chat Teenifier
// @description Truncates chat regularly to eliminate slowdown.
// @author David Schoonover <[email protected]>
// @namespace http://less.ly/tinychat/teenychat
// @version 0.1
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js
// @require https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js
// @run-at document-end
// @grant none
// @noframes
// @match https://tinychat.com/room/*
// ==/UserScript==
(function($){
var DEFAULT_LOG_MSG_LIMIT = window.LOG_MSG_LIMIT = 100;
var log = function(){ console.log( '[%cTeenychat%c]', 'color: #22efb4', 'color: inherit', ...arguments); };
log.warn = function(){ console.warn( '[%cTeenychat%c]', 'color: #22efb4', 'color: inherit', ...arguments); };
log.error = function(){ console.error( '[%cTeenychat%c]', 'color: #22efb4', 'color: inherit', ...arguments); };
log.log = log;
// log(`[$: ${$.fn.jquery}]`, `[jQuery: ${jQuery.fn.jquery}]`);
log(`[$: ${$.fn.jquery}]`, `[jQuery: ${jQuery.fn.jquery}]`, `[lodash: ${_.VERSION}]`);
// log(`[$: ${$.fn.jquery}]`);
function shadow(sel, ctx){
return $(sel, ctx || null)[0].shadowRoot;
}
function truncateChat(){
var limit = Number.parseInt(window.LOG_MSG_LIMIT);
limit = limit > -1 ? limit : DEFAULT_LOG_MSG_LIMIT;
try {
// log('truncating chat!');
var $chat = $('#chat-instant:not(.show) ~ #chat > #chat-content', $('tc-chatlog', $('tinychat-webrtc-app')[0].shadowRoot)[0].shadowRoot)
, $msgs = $chat.children()
, $kill = $msgs.not(':first-child, dom-repeat').not($msgs.slice(-1 * limit))
;
if ($kill.length) {
log('Truncating chat to', limit ,'messages... Found', $kill.length, 'messages to remove!');
$kill.remove();
}
} catch(lol) {
log.error('LOG TRUNCATION ERROR! (limit:', limit, ')', lol);
}
}
function truncateChat2(){
var limit = Number.parseInt(window.LOG_MSG_LIMIT);
limit = limit > -1 ? limit : DEFAULT_LOG_MSG_LIMIT;
try {
// log('truncating chatt 2!');
var app = document.body.querySelectorAll('tinychat-webrtc-app')[0].shadowRoot
, log = app.querySelectorAll('tc-chatlog')[0].shadowRoot
, chat = log.querySelectorAll('#chat-instant:not(.show) ~ #chat > #chat-content')[0].shadowRoot
, $chat = $(chat)
, $msgs = $chat.children()
, $kill = $msgs.not(':first-child, dom-repeat').not($msgs.slice(-1 * limit))
;
if ($kill.length) {
log('Truncating chat (v2) to', limit ,'messages... Found', $kill.length, 'messages to remove!');
$kill.remove();
}
} catch(lol) {
log.error('LOG TRUNCATION v2 ERROR! (limit:', limit, ')', lol);
}
}
// setTimeout(() => ... }, 250)
window.TRUNCATE_CHAT_PID = setInterval( truncateChat, 10000 );
_.extend(window, {
$j:$, jQ:$, shadow, truncateChat, truncateChat2
});
// window.$j = window.jQ = $;
})( jQuery.noConflict(true) );
@dsc
Copy link
Author

dsc commented Mar 20, 2021

oh, ha. this still has two versions of the code, with and without jQuery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment