-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.js
242 lines (179 loc) · 9.31 KB
/
inject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const fs = require("fs");
const path = require("path");
/*
Debug using `%localappdata%\Discord\app-1.0.9013\Discord.exe`
*/
const electron = require("electron");
const { BrowserWindow } = electron;
const SettingsPopup = require(path.join(__dirname, "src/parts/SettingsPopup.js"));
const Elements = require(path.join(__dirname, "src/Elements.js"));
const _ConfigServer = require(path.join(__dirname, "src/ConfigServer.js"));
const ConfigSaver = require(path.join(__dirname, "src/ConfigSaver.js"));
const _App = require(path.join(__dirname, "src/parts/App.js"));
const App = new _App.App();
const _BuiltinStyles = require(path.join(__dirname, "src/RCBuiltInStyling.js"));
const BuiltinStyles = new _BuiltinStyles();
const EventEmitter = require(path.join(__dirname, "src/Events.js"));
const ProtoFixes = require(path.join(__dirname, "src/ProtoFixes.js"));
console.log("App", App);
console.log("EventEmitter", EventEmitter);
// fs.writeFileSync(
// path.join(__dirname, "hi.txt"),
// "Hello from the injected script!"
// );
const appdata = path.join(process.env.APPDATA, 'ReCord');
const extensionsDir = path.join(appdata, 'extensions');
const themesDir = path.join(appdata, 'themes');
if (!fs.existsSync(extensionsDir))
fs.mkdirSync(extensionsDir, { recursive: true });
if (!fs.existsSync(themesDir))
fs.mkdirSync(themesDir, { recursive: true });
const extensions = fs.readdirSync(extensionsDir)
.filter((file) => file.endsWith('.ext.js'));
const ConfigServer = new _ConfigServer();
let interval = setInterval(() => {
try {
//check if window loaded
const win = BrowserWindow.getFocusedWindow();
if (!win) return;
if (win.webContents.isLoading()) return;
//console.log(BuiltinStyles.GetRemoveNitroGradientsFunction().toString());
App.Run(`
(${ProtoFixes.toString()})();
window.RC = {};
window.RC.Themes = ${JSON.stringify(ConfigServer.Themes)};
window.RC.EnabledThemes = ${JSON.stringify(ConfigServer.EnabledThemes)};
window.RC.EnabledThemesCSS = ${JSON.stringify(ConfigServer.EnabledThemesCSS)};
window.RC.Settings = ${JSON.stringify(ConfigServer.GeneralSettings)};
window.RC.Elements = new ${Elements.toString()};
window.RC.SettingsPopup = ${SettingsPopup.toString()};
window.EventEmitter = ${EventEmitter.toString()};
(${ConfigSaver.toString()})();
`);
App.Run(BuiltinStyles.Setter());
App.Run(function() {
//document.body.style.backgroundColor = "red";
window.waitForElm = (selector, checkForExists = true) => {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
let observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (!mutation.addedNodes) return
for (let i = 0; i < mutation.addedNodes.length; i++) {
if (checkForExists && document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
} else if (!checkForExists && !document.querySelector(selector)) {
resolve(null);
observer.disconnect();
}
}
})
})
observer.observe(document.body, {
childList: true
, subtree: true
, attributes: false
, characterData: false
})
});
}
window.listenForElm = (selector, callback, checkForExists = true) => {
//run code whenever #selector is CREATED, not when it exists. WHEN IT IS CREATED
//ALSO, if it removed and re-added, it will trigger again
let hasCheckedThisExistance = false;
let observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (!mutation.addedNodes) return
for (let i = 0; i < mutation.addedNodes.length; i++) {
if (checkForExists && document.querySelector(selector) && !hasCheckedThisExistance) {
callback(document.querySelector(selector));
//observer.disconnect(); don't disconnect, but don't trigger until it's removed and re-added
hasCheckedThisExistance = true;
break;
} else if (!checkForExists && !document.querySelector(selector) && !hasCheckedThisExistance) {
callback(null);
///observer.disconnect();
hasCheckedThisExistance = true;
break;
}
if (hasCheckedThisExistance && !document.querySelector(selector))
hasCheckedThisExistance = false;
}
})
})
observer.observe(document.body, {
childList: true
, subtree: true
, attributes: false
, characterData: false
})
};
// document.head.appendChild(document.createElement("script")).innerHTML = EventEmitter.toString();
// console.log("EventEmitter", EventEmitter);
window.RC.Events = new EventEmitter();
waitForElm('[aria-label="Direct Messages"]').then((elm) => {
setTimeout(() => {
window.RC.Events.emit("apps_ready");
}, 100)
})
listenForElm('[aria-label="My Account"]', (elm) => {
console.log("Settings opened!");
window.RC.Events.emit("settings_opened");
})
listenForElm('[aria-label="My Account"]', (elm) => {
window.RC.Events.emit("settings_closed");
}, false)
waitForElm('[style="flex: 1 1 auto;"]>[class*="buttons_"]>button[type="button"][class*="lookFilled_"][class*="grow_"]', true).then((elm) => {
let parent = elm.parentElement;
elm.remove()
let note = document.querySelector('[class*="note_"]').children[0];
note.style.color = "var(--bg-gradient-citrus-sherbert-2)";
note.children[0].innerHTML = "ReCord has crashed unexpectedly. Please reload Discord to continue using ReCord.";
note.children[1].innerHTML = "This can be done by press ALT+F4, ending via command line or task manager and re-opening Discord.";
})
window.RC.Events.on("settings_opened", () => {
console.log("Settings opened!");
// alert('settings opened')
setTimeout(() => {
window.RC.Elements.grabSectionTitleClassAttrs();
window.RC.SettingsPopup();
}, 500)
})
window.RC.Events.on("apps_ready", () => {
let theme_style = document.createElement("style");
let inner = "";
for (let i = 0; i < window.RC.EnabledThemesCSS.length; i++) {
console.log("CSS", window.RC.EnabledThemesCSS[i]);
inner += window.RC.EnabledThemesCSS[i];
}
theme_style.innerHTML = inner;
/*
//TODO: FIX THIS
if (inner.match(/background[-]?(a-zA-Z0-9-)*?:/g)) {
listenForElm('style[data-client-themes="true"][data-rh="true"]', (elm) => {
console.log("Removing old theme style...");
console.log(elm);
elm.innerHTML = "";
elm.remove();
})
let nitro_style = document.createElement("style");
nitro_style.innerHTML = `.custom-theme-background {
--custom-theme-background: '' !important;
}`
document.head.appendChild(nitro_style);
}
*/
document.head.appendChild(theme_style);
})
//alert("Injected script loaded!");
document.querySelector('[class*="tip_"]').innerHTML = "ReCord is starting...";
document.head.appendChild(document.createElement("script")).innerHTML = `console.log("Injected script loaded!");`;
});
clearInterval(interval);
} catch (e) {
console.error(e);
}
}, 50);