Patch DOMTokenList methods to use the correct (not normalised) classes

rauenzi/BetterDiscordApp#159, rauenzi/BetterDiscordApp#161
This commit is contained in:
Samuel Elliott 2019-04-19 22:10:16 +01:00
parent 3eb9105daa
commit 2b0d3eb268
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
1 changed files with 32 additions and 0 deletions

View File

@ -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;