forked from Arashiryuu/crap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsMaterialThemeCodeblocks.plugin.js
298 lines (244 loc) · 8.37 KB
/
jsMaterialThemeCodeblocks.plugin.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//META{"name":"JSMaterialThemeCodeblocksRedux","displayName":"JSMaterialThemeCodeblocksRedux","website":"https://github.com/Arashiryuu","source":"https://github.com/Arashiryuu/crap"}*//
/*@cc_on
@if (@_jscript)
// Offer to self-install for clueless users that try to run this directly.
var shell = WScript.CreateObject('WScript.Shell');
var fs = new ActiveXObject('Scripting.FileSystemObject');
var pathPlugins = shell.ExpandEnvironmentStrings('%APPDATA%\\BetterDiscord\\plugins');
var pathSelf = WScript.ScriptFullName;
// Put the user at ease by addressing them in the first person
shell.Popup('It looks like you\'ve mistakenly tried to run me directly. \n(Don\'t do that!)', 0, 'I\'m a plugin for BetterDiscord', 0x30);
if (fs.GetParentFolderName(pathSelf) === fs.GetAbsolutePathName(pathPlugins)) {
shell.Popup('I\'m in the correct folder already.\nJust reload Discord with Ctrl+R.', 0, 'I\'m already installed', 0x40);
} else if (!fs.FolderExists(pathPlugins)) {
shell.Popup('I can\'t find the BetterDiscord plugins folder.\nAre you sure it\'s even installed?', 0, 'Can\'t install myself', 0x10);
} else if (shell.Popup('Should I copy myself to BetterDiscord\'s plugins folder for you?', 0, 'Do you need some help?', 0x34) === 6) {
fs.CopyFile(pathSelf, fs.BuildPath(pathPlugins, fs.GetFileName(pathSelf)), true);
// Show the user where to put plugins in the future
shell.Exec('explorer ' + pathPlugins);
shell.Popup('I\'m installed!\nJust reload Discord with Ctrl+R.', 0, 'Successfully installed', 0x40);
}
WScript.Quit();
@else@*/
var JSMaterialThemeCodeblocksRedux = (() => {
/* Setup */
const config = {
main: 'index.js',
info: {
name: 'JSMaterialThemeCodeblocksRedux',
authors: [
{
name: 'Arashiryuu',
discord_id: '238108500109033472',
github_username: 'Arashiryuu',
twitter_username: ''
}
],
version: '1.0.2',
description: 'Applies the "Material Theme" to JavaScript codeblocks.',
github: 'https://github.com/Arashiryuu',
github_raw: 'https://raw.githubusercontent.com/Arashiryuu/crap/master/Miscellanious/jsMaterialThemeCodeblocks.plugin.js'
}
};
const log = function() {
/**
* @type {Array}
*/
const args = Array.prototype.slice.call(arguments);
args.unshift(`%c[${config.info.name}]`, 'color: #3A71C1; font-weight: 700;');
return console.log.apply(this, args);
};
/* Build */
const buildPlugin = ([Plugin, Api]) => {
const { Toasts, Logger, Patcher, DOMTools, ReactTools, DiscordModules, WebpackModules, DiscordSelectors, PluginUtilities, ReactComponents } = Api;
const has = Object.prototype.hasOwnProperty;
const messagesWrapper = WebpackModules.getByProps('messages', 'messagesWrapper');
return class JSMaterialThemeCodeblocksRedux extends Plugin {
constructor() {
super();
this.promises = {
state: { cancelled: false },
cancel() { this.state.cancelled = true; },
restore() { this.state.cancelled = false; }
};
this.selectors = [
'.hljs[class~="js" i] .hljs-keyword',
'.hljs[class~="jsx" i] .hljs-keyword',
'.hljs[class~="javascript" i] .hljs-keyword',
'.hljs[class~="js" i] .hljs-built_in',
'.hljs[class~="jsx" i] .hljs-built_in',
'.hljs[class~="javascript" i] .hljs-built_in'
];
this.keywords = {
'instanceof':'instanceof',
'arguments':'arguments',
'parseInt':'parseInt',
'require':'require',
'typeof':'typeof',
'isNaN':'isNaN',
'this':'this',
'void':'void',
'new':'new',
'in':'in',
'of':'of'
};
this.stylesheet;
}
/* Methods */
onStart() {
Toasts.info(`${this.name} ${this.version} has started!`, { icon: true, timeout: 2e3 });
this.injectCSS();
this.patchMessages(this.promises.state);
this.addClasses();
}
async patchMessages(state) {
const Component = await ReactComponents.getComponentByName('Messages', `.${messagesWrapper.messagesWrapper.replace(/\s/, '.')}`);
const { component: Message } = Component;
if (state.cancelled) return;
Patcher.after(Message.prototype, 'render', (that, args, value) => {
setImmediate(() => {
this.addClasses();
this.paramParse();
});
return value;
});
Component.forceUpdateAll();
}
updateMessages() {
const messages = document.querySelectorAll(`.${messagesWrapper.messagesWrapper.replace(/\s/, '.')}`);
if (!messages.length) return;
for (let i = 0, len = messages.length; i < len; i++) ReactTools.getOwnerInstance(messages[i]).forceUpdate();
}
addClasses() {
for (const selector of this.selectors) {
const selection = DOMTools.queryAll(selector);
for (const selected of selection) {
const { innerText: text } = selected;
const className = this.getClass(text);
if (className && !DOMTools.hasClass(selected, className)) DOMTools.addClass(selected, className);
}
}
}
getClass(name) {
if (has.call(this.keywords, name)) return this.keywords[name];
return null;
}
paramParse() {
let params = DOMTools.queryAll('.hljs-params:not(.hljs-no-param)');
if (!params.length) return;
const noParam = Array.from(params).filter((param) => param.textContent === '()');
if (noParam.length) {
for (const param of noParam) DOMTools.addClass(param, 'hljs-no-param');
}
params = DOMTools.queryAll('.hljs-params:not(.hljs-no-param)');
if (!params.length) return;
for (const param of params) {
if (param.innerHTML.indexOf(',') > -1) {
const split = param.innerHTML.split(',');
const len = split.length;
for (let i = 0; i < len; i++) split[i] = split[i].trim();
param.innerHTML = split.join('<span class="hljs-separator">, </span>');
}
}
}
injectCSS() {
let sheet = document.getElementById(this.short);
if (sheet) return;
sheet = DOMTools.parseHTML(`<link id="${this.short}" rel="preload stylesheet" as="style" href="${this.cdn}"/>`);
return (DOMTools.appendTo(sheet, document.head), true);
}
removeCSS() {
const sheet = document.getElementById(this.short);
if (sheet) return (sheet.remove(), true);
return false;
}
onStop() {
Patcher.unpatchAll();
this.updateMessages();
this.removeCSS();
Toasts.info(`${this.name} ${this.version} has stopped!`, { icon: true, timeout: 2e3 });
}
getProps(obj, path) {
return path.split(/\s?\.\s?/).reduce((object, prop) => object && object[prop], obj);
}
/* Getters */
get [Symbol.toStringTag]() {
return 'Plugin';
}
get cdn() {
return 'https://raw.githack.com/Arashiryuu/crap/master/Miscellanious/jsMaterialThemeCodeblocksCSS/src.css';
}
get name() {
return config.info.name;
}
get short() {
let string = '';
for (let i = 0, len = config.info.name.length; i < len; i++) {
const char = config.info.name[i];
if (char === char.toUpperCase()) string += char;
}
return string;
}
get author() {
return config.info.authors.map((author) => author.name).join(', ');
}
get version() {
return config.info.version;
}
get description() {
return config.info.description;
}
};
};
/* Finalize */
return !global.ZeresPluginLibrary
? class {
getName() {
return this.name.replace(/\s+/g, '');
}
getAuthor() {
return this.author;
}
getVersion() {
return this.version;
}
getDescription() {
return this.description;
}
stop() {
log('Stopped!');
}
load() {
window.BdApi.alert('Missing Library', `The library plugin needed for ${config.info.name} is missing.<br /><br /> <a href="https://betterdiscord.net/ghdl?url=https://raw.githubusercontent.com/rauenzi/BDPluginLibrary/master/release/0PluginLibrary.plugin.js">Click here to download the library!</a>`);
}
start() {
log('Started!');
}
/* Getters */
get [Symbol.toStringTag]() {
return 'Plugin';
}
get name() {
return config.info.name;
}
get short() {
let string = '';
for (let i = 0, len = config.info.name.length; i < len; i++) {
const char = config.info.name[i];
if (char === char.toUpperCase()) string += char;
}
return string;
}
get author() {
return config.info.authors.map((author) => author.name).join(', ');
}
get version() {
return config.info.version;
}
get description() {
return config.info.description;
}
}
: buildPlugin(global.ZeresPluginLibrary.buildPlugin(config));
})();
/*@end@*/