From 2b0d3eb268c7cd708ae29df2916957210fcf72c3 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Fri, 19 Apr 2019 22:10:16 +0100 Subject: [PATCH] Patch DOMTokenList methods to use the correct (not normalised) classes rauenzi/BetterDiscordApp#159, rauenzi/BetterDiscordApp#161 --- client/src/ui/classnormaliser.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/client/src/ui/classnormaliser.js b/client/src/ui/classnormaliser.js index 6764e084..02fab968 100644 --- a/client/src/ui/classnormaliser.js +++ b/client/src/ui/classnormaliser.js @@ -12,12 +12,14 @@ import { Module, Reflection } from 'modules'; const normalizedPrefix = 'da'; const randClass = new RegExp(`^(?!${normalizedPrefix}-)((?:[A-Za-z]|[0-9]|-)+)-(?:[A-Za-z]|[0-9]|-|_){6}$`); +const normalisedClass = /^(([a-zA-Z0-9]+)-[^\s]{6}) da-([a-zA-Z0-9]+)$/; export default class ClassNormaliser extends Module { init() { this.patchClassModules(Reflection.module.getModule(this.moduleFilter.bind(this), false)); this.normalizeElement(document.querySelector('#app-mount')); + this.patchDOMMethods(); } patchClassModules(modules) { @@ -26,6 +28,36 @@ export default class ClassNormaliser extends Module { } } + patchDOMMethods() { + const add = DOMTokenList.prototype.add; + + DOMTokenList.prototype.add = function(...tokens) { + for (const token of tokens) { + let match; + if (typeof token === 'string' && (match = token.match(normalisedClass)) && match[2] === match[3]) return add.call(this, match[1]); + return add.call(this, token); + } + }; + + const remove = DOMTokenList.prototype.remove; + + DOMTokenList.prototype.remove = function(...tokens) { + for (const token of tokens) { + let match; + if (typeof token === 'string' && (match = token.match(normalisedClass)) && match[2] === match[3]) return remove.call(this, match[1]); + return remove.call(this, token); + } + }; + + const contains = DOMTokenList.prototype.contains; + + DOMTokenList.prototype.contains = function(token) { + let match; + if (typeof token === 'string' && (match = token.match(normalisedClass)) && match[2] === match[3]) return contains.call(this, match[1]); + return contains.call(this, token); + }; + } + shouldIgnore(value) { if (!isNaN(value)) return true; if (value.endsWith('px') || value.endsWith('ch') || value.endsWith('em') || value.endsWith('ms')) return true;