Skip to content

Instantly share code, notes, and snippets.

@samogot
Last active August 7, 2021 09:37
Show Gist options
  • Save samogot/736dc6289c9fdb9d1f12aba253d530f5 to your computer and use it in GitHub Desktop.
Save samogot/736dc6289c9fdb9d1f12aba253d530f5 to your computer and use it in GitHub Desktop.
Discord deep hacking
// get user interface locale
const userSettingsStore = WebpackModules.findByUniqueProperties(['developerMode','locale']);
switch(userSettingsStore.locale) {
case 'ru':
// ...
break;
default:
// ...
}
// add some item to message options menu
ReactComponents.get('OptionPopout', OptionPopout => {
monkeyPatch(OptionPopout.prototype, 'render', {
after: ({ret}) => {
ret.props.children.push(React.createElement("div", {
className: "btn-item",
onClick: ()=>alert("do someting!")
}, 'New Item'))
}
})
})
// add some item to message context menu
const ContextMenuItemsGroup = WebpackModules.find(m => typeof m == "function" && m.length==1 && m.toString().search(/className\s*:\s*["']item-group["']/) !== -1);
const ContextMenuItem = WebpackModules.find(m => typeof m == "function" && m.length==1 && m.toString().search(/\.label\b.*\.hint\b.*\.action\b/) !== -1);
ReactComponents.get('MessageContextMenu', MessageContextMenu => {
monkeyPatch(MessageContextMenu.prototype, 'render', {
after: ({ret}) => {
ret.props.children.push(React.createElement(ContextMenuItemsGroup, {}, React.createElement(ContextMenuItem, {
label:'New Item',
action: ()=>alert("do someting!")
})))
}
})
})
// Send embed (it's a minimal example without error handling, etc.)
const MessagesUtils = WebpackModules.findByUniqueProperties(['receiveMessage']);
const MessagesQueueUtils = WebpackModules.findByUniqueProperties(['enqueue']);
const MessagesParserUtils = WebpackModules.findByUniqueProperties(['createMessage']);
const message = MessagesParserUtils.createMessage(chnnelId, 'message text', false);
MessagesQueueUtils.enqueue({
type: "send",
message: {
channelId: chnnelId,
content: 'message text',
embed: { /* some embed object here */ },
nonce: message.id,
tts: false
}
}, function(t) {
if(t.ok) MessagesUtils.receiveMessage(chnnelId, t.body)
});
const WebpackModules = new (function WebpackModules() {
const modulesFactories = webpackJsonp([], {
'__extra_id__': (module, exports, require) => {
exports.default = Object.values(require).find(f => f instanceof Array);
}
}, ['__extra_id__']).default;
delete modulesFactories['__extra_id__'];
this.find = filter => {
for(let i = 0; i < modulesFactories.length; ++i) {
var m = webpackJsonp([],[],[i]);
if(m && m.__esModule)
m = m.default;
if(m && filter(m))
return m;
}
return null;
}
this.findByUniqueProperties = propNames => this.find(module => propNames.every(prop => module[prop] != undefined))
this.findByDisplayName = displayName => this.find(module => module.displayName === displayName)
return this;
})();
const monkeyPatch = (what, methodName, {before, after}) => {
console.log('patch', methodName, what, what[methodName]);
const origMethod = what[methodName];
const cancel = () => {
console.log('unpatch', methodName, what, what[methodName]);
what[methodName] = origMethod;
};
what[methodName] = function() {
const data = {obj:this, args:arguments};
if(before) before(data);
data.ret = origMethod.apply(data.obj, data.args);
if(after) after(data);
return data.ret;
}
what[methodName].__monkeyPatched=true;
what[methodName].displayName='patched ' + methodName;
};
const ReactComponents = new (function ReactComponents() {
const components = {};
const listners = {};
const put = component => {
const name = component.displayName;
if(!components[name]) {
components[name] = component;
if(listners[name]){
listners[name].forEach(f => f(component))
listners[name] = null;
}
}
};
this.get = (name, callback = null) => new Promise(resolve => {
const listner = component => {
if(callback) callback(component);
resolve(component);
}
if(components[name]) {
listner(components[name]);
}
else {
if(!listners[name]) listners[name] = [];
listners[name].push(listner);
}
});
this.getAll = (...names) => Promise.all(names.map(name => this.get(name)))
const React = WebpackModules.findByUniqueProperties(['createMixin']);
monkeyPatch(React, 'createElement', {
before: ({args}) => {
if(args[0].displayName)
put(args[0]);
}
});
return this;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment