-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
221 lines (184 loc) · 7.27 KB
/
index.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
const { Plugin } = require('powercord/entities');
const { findInReactTree } = require("powercord/util");
const { React, FluxDispatcher, getModule, constants, messages: MessageEvents } = require('powercord/webpack');
const { inject, uninject } = require('powercord/injector');
const MiniPopover = getModule((m) => m.default && m.default.displayName === "MiniPopover", false);
const MessageContent = getModule((m) => m.type && m.type.displayName === "MessageContent", false);
const Bottom = require('./bottom_wasm');
const Settings = require('./components/Settings.jsx');
const BottomButton = require('./components/BottomButton.jsx');
const Indicator = require("./components/Indicator.jsx");
const Handler = new (require('./bottomHandler'))();
const escapeRegex = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const count = (string, subString) => {
var n = 0;
var idx = 0;
var pos = 0;
step = subString.length;
while (true) {
pos = string.indexOf(subString, pos);
if (pos >= 0) {
n++;
pos += step;
} else break;
}
return n;
}
function inlineEncode(p, s, text) {
var np = count(text, p);
var ns = count(text, s);
if (np === 0 || ns === 0) return text;
var pl = p.length;
var sl = s.length;
let result = [];
let idx = 0;
while (true) {
var startIndex = text.indexOf(p, idx);
if (startIndex < 0) {
result.push(text.slice(idx));
break;
}
var endIndex = text.indexOf(s, startIndex + pl);
if (endIndex < 0) {
result.push(text.slice(idx));
break;
}
result.push(text.slice(idx, startIndex));
startIndex += pl;
result.push(Bottom.encode(text.slice(startIndex, endIndex)));
endIndex += sl;
idx = endIndex;
}
return result.join('');
}
module.exports = class PowerBottom extends Plugin {
constructor() {
super();
this.ConnectedBottomButton = this.settings.connectStore(BottomButton);
}
async startPlugin () {
powercord.api.settings.registerSettings(this.entityID, {
category: this.entityID,
label: 'Power Bottom',
render: Settings,
}
);
const { upload } = await getModule([ 'cancel', 'upload' ]);
const { makeFile } = await getModule([ 'classifyFile', 'makeFile' ]);
powercord.api.commands.registerCommand({
command: 'bottom',
description: 'Translate and send text as bottom 🥺',
usage: '{c} [text]',
executor: (args) => {
var translated = Bottom.encode(args.join(' '));
return {
send: true,
result: translated,
}
}
}
);
inject(
"power-bottom-send-message",
MessageEvents,
"sendMessage",
(args) => {
if (this.settings.get('auto-encode-send') && !args[1].bottomTranslation) {
let sendType = this.settings.get('encode-send-type', 0);
var content = args[1].content;
switch (sendType) {
case 0: // all
content = Bottom.encode(content);
break;
case 1: // inline greedy
var prefix = escapeRegex(this.settings.get('inline-bottom-prefix', '👉'));
var suffix = escapeRegex(this.settings.get('inline-bottom-suffix', '👈'));
var reg = new RegExp(`${prefix}(.+)${suffix}`, 'gm');
content = content.replace(reg, (str, p1, o, s) => Bottom.encode(p1));
break;
case 2: // inline parsed
var prefix = this.settings.get('inline-bottom-prefix', '👉');
var suffix = this.settings.get('inline-bottom-suffix', '👈');
content = inlineEncode(prefix, suffix, content);
break;
}
if (content.length >= constants.MAX_MESSAGE_LENGTH && this.settings.get('send-file')) {
args[1].content = '';
const textDocument = new Blob([ content ], {type: 'text/plain'});
upload(args[0], makeFile(textDocument, this.settings.get('send-file-name', 'bottom.txt'), true), args[1]);
return false;
}
args[1].content = content;
args[1].bottomTranslation = true;
MessageEvents.sendMessage(...args);
return false;
}
return args;
},
true
);
inject(
"power-bottom-translate-button",
MiniPopover,
"default",
(args, res) => {
const props = findInReactTree(res, (r) => r && r.message);
if (!props) return res;
res.props.children.unshift(
React.createElement(this.ConnectedBottomButton, {
message: props.message,
Handler,
})
);
return res;
}
);
MiniPopover.default.displayName = "MiniPopover";
inject(
"power-bottom-message-content",
MessageContent,
"type",
(args, res) => {
try {
if (!Handler.cache[args[0].message.channel_id][args[0].message.id].top){
try {
res.props.children.push(
React.createElement(Indicator, {
bottom: !Handler.isTranslated(args[0].message),
layers: Handler.cache[args[0].message.channel_id][args[0].message.id].layers,
})
);
} catch {}
}
} finally {
return res;
}
}
);
MessageContent.type.displayName = "MessageContent";
FluxDispatcher.subscribe('MESSAGE_UPDATE', this.handleMessageUpdate)
}
handleMessageUpdate (evt) {
if (
!evt.bottomTranslation &&
Handler.cache[evt.message.channel_id] &&
Handler.cache[evt.message.channel_id][evt.message.id]
) {
Handler.removeMessage(
evt.message.channel_id,
evt.message.id,
false
);
}
}
pluginWillUnload () {
uninject("power-bottom-send-message");
uninject("power-bottom-translate-button");
uninject("power-bottom-message-content");
uninject("power-bottom-file-upload");
powercord.api.commands.unregisterCommand('bottom');
powercord.api.settings.unregisterSettings(this.entityID);
FluxDispatcher.unsubscribe('MESSAGE_UPDATE', this.handleMessageUpdate)
Handler.clearCache();
}
}