PASTEDOWN   83   0
   346 3.37 KB    92

CyTube MLPA Enhanced Drink Counter

By Floorb
Created: 2024-10-06 00:10:17
Updated: 2024-10-09 07:13:48
Expiry: Never

This is a simple user script that adds the actual text of the latest drink call to the drink banner on CyTube.

New version (works on Firefox, untested on Chrome yet)

// ==UserScript==
// @name         CyTube Enhanced Drink Counter
// @namespace    http://neet.horse/
// @version      0.2.0
// @description  Shows CyTube drink calls in the drink bar
// @author       Floorb
// @match        https://cytu.be/r/*
// @icon         https://resources.pink.horse/images/backgrounds/drinkbar_left.png
// @grant        none
// ==/UserScript==
(function() {
  var backupFormatChatMessage = unsafeWindow.formatChatMessage;

  function stripHtml(html) {
    return html.replace(/<\/?[^>]+(>|$)/g, "");
  }

  function hookFormatChatMessage(data, last) {
    var div = unsafeWindow.backupFormatChatMessage(data, last);

    if (div[0].classList.contains('drink')) {
      var text = document.getElementById('drinkcount').innerText;
      document.getElementById('drinkcount')
        .innerText = stripHtml(data.msg) + ' (' + text.replace(/drinks?/i, '').trim() + ')';
    }
    return div;
  }

  exportFunction(hookFormatChatMessage, unsafeWindow, {defineAs: "hookFormatChatMessage"});

  // This is in a setTimeout() because we need to wait for all the Xae shit to finish loading.
  // This will have the effect of redefining the global addChatMessage() function to call our hook.
  setTimeout(() => {
    var script = document.createElement('script');
    script.textContent = `
      window.backupFormatChatMessage = window.formatChatMessage;
      window.formatChatMessage = function(data, last) {
        return hookFormatChatMessage(data, last);
      }
      console.log('[CyTube Enhanced Drink Counter] Hooked formatChatMessage()');
    `;
    document.body.appendChild(script);
  }, 10000);
})();

Old version (only works on Chrome, does not work on Firefox)

// ==UserScript==
// @name         CyTube Enhanced Drink Counter
// @namespace    http://neet.horse/
// @version      0.1.1
// @description  Shows CyTube drink calls in the drink bar
// @author       Floorb
// @match        https://cytu.be/r/*
// @icon         https://resources.pink.horse/images/backgrounds/drinkbar_left.png
// @grant        none
// ==/UserScript==
(function() {
    var backupFormatChatMessage = window.formatChatMessage;

    function stripHtml(html) {
        return html.replace(/<\/?[^>]+(>|$)/g, "");
    }

    function hookFormatChatMessage(data, last) {
        var div = backupFormatChatMessage(data, last);

        if (div[0].classList.contains('drink')) {
            var text = document.getElementById('drinkcount').innerText;
            document.getElementById('drinkcount')
                .innerText = stripHtml(data.msg) + ' (' + text.replace(/drinks?/i, '').trim() + ')';
        }
        return div;
    }

    // This kid keeps fucking overriding my hook so just keep resetting it, probably Xaekai's fault or however you spell it.
    var interval = setInterval(function() {
        if (window.formatChatMessage != hookFormatChatMessage) {
            backupFormatChatMessage = window.formatChatMessage;
            window.formatChatMessage = hookFormatChatMessage;
            console.log('[CyTube Enhanced Drink Counter] Hooked window.formatChatMessage');
            // clearInterval(interval);
        }
    }, 1000);
})();