Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

4. Injections

Sammy edited this page Jan 3, 2022 · 9 revisions

Now to the Most interesting part, Injections!

Injections help you add Options to Context Menus or other cool stuffs. In this Chapter, we're going to inject into the sendMessage Module and make it replace every "lmao" with "kek". (Sounds silly but it works perfectly as an introduction

First, we need to import the injector and uninjector into our index.js

const { inject, uninject } = require("powercord/injector");

Injecting

Next, grab the messages Module, this is going to be a little different than normally but the concept is the same.

const { messages } = require('powercord/webpack');

Now lets get to the Actual injecting. Injecting is done in the startPlugin() Method

const { Plugin } = require('powercord/entities');
const { inject, uninject } = require('powercord/injector');
const { messages } = require('powercord/webpack');

module.exports = class ExamplePlugin extends Plugin {
  startPlugin() {
    inject('injectionID', messages, 'sendMessage', (args) => {
      if (args[1].content.includes('lmao')) { // args[1] is the actual message
        args[1].content.replace('lmao', 'kek');
      }
      return args; // Always return the Args (or res, if you're post-patching)
    }, true); // Prepatching
  }
}

And thats it!

inject() takes 4 parameters. The 1st being the injectionID, it can be any string. 2nd is the Module you want to inject into. Third is the Method from that module you want to inject into, here it is 'sendMessage' but can differ with other Modules. The 4th param is the patch, the parameters for that function differ if you PrePatch or Postpatch. The Last Parameter is a bool that decides if you want to PrePatch or Postpatch, true being Prepatch, false being Postpatch.

If you are Post-Patching things, the parameters for the patch are (arg, res) and you have to return res after you're done with patching. Postpatching means the patch will run after the method.

If you are Pre-Patching, the parameters only consist of (args), you have to return args after you're done with your patching. Prepatching runs the Patch before the method runs.

Here, the content we are going to send is in the second array position, so we are going to look there for "lmao" and replace it with "kek", then put the changed thingy back and return the Args (this is important, else it breaks everything)

Uninjecting

Another very important thing is Uninjecting, we dont want things to double or triple inject. So in order to uninject, we use the uninject method in pluginWillUnload.

[...]
  pluginWillUnload() {
    uninject('injectionID');
  }
[...]

Simple right? Just make sure you give uninject the same injectionID you entered when you injected!

Clone this wiki locally