Skip to content

Instantly share code, notes, and snippets.

@PokeGuys
Created February 20, 2021 10:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PokeGuys/a607f14b87429b7746b01985e418d921 to your computer and use it in GitHub Desktop.
Save PokeGuys/a607f14b87429b7746b01985e418d921 to your computer and use it in GitHub Desktop.
Youtube emoji fix
// ==UserScript==
// @name Youtube emoji fix
// @namespace youtube.emoji.fix
// @version 0.1
// @description Improved Youtube chat performance by replacing SVG emoji to PNG file.
// @author PokeGuys
// @match *://www.youtube.com/live_chat*
// @run-at document-start
// @grant none
// ==/UserScript==
(function() {
'use strict';
// YT_EMOJI_CONFIG_URL represents the URL of youtube emoji list.
const YT_EMOJI_CONFIG_URL = 'https://www.gstatic.com/youtube/img/emojis/emojis-svg-5.json';
// DISABLE_EMOJI_KEYBOARD indicate that emoji is disabled in emoji keyboard.
const DISABLE_EMOJI_KEYBOARD = false;
/**
* Apply emoji patch.
*
* @param rawEmojiList The raw emoji list in Text format.
* @returns Patched emoji list
*/
const applyPatch = (rawEmojiList) => {
// Early exit when `DISABLE_EMOJI_KEYBOARD` is true.
if (DISABLE_EMOJI_KEYBOARD) {
return '[]';
}
// Replace `.svg` to `.png`.
return rawEmojiList.replace(/\.svg/, '.png')
};
// Hook `XMLHttpRequest.open` function to inject our patched code.
const originalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener('load', function() {
// Only affect URL match `YT_EMOJI_CONFIG_URL`
if (this.responseURL.includes(YT_EMOJI_CONFIG_URL)) {
const patchedResponse = applyPatch(this.responseText);
// Dirty hack to modify readonly properties `response`, `responseText`.
Object.defineProperty(this, 'response', {writable: true});
Object.defineProperty(this, 'responseText', {writable: true});
this.response = this.responseText = patchedResponse;
// Release `XMLHttpRequest.open` hook.
XMLHttpRequest.prototype.open = originalOpen;
}
});
// Invoke original `open()` function.
originalOpen.apply(this, arguments);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment