Skip to content

Commit 978281a

Browse files
committed
chore: cleanup
1 parent 9361fd6 commit 978281a

File tree

8 files changed

+96
-77
lines changed

8 files changed

+96
-77
lines changed

src/base/plugin/enabled.local.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,34 @@ import wrap from '../../utils/2.pokemon.js';
22
import { registerModule } from '../../utils/plugin.js';
33
import * as settings from '../../utils/settings/index.js';
44

5-
const enable = ['Enable with toast', 'Enable silently'];
5+
const enable = ['Enabled with toast', 'Enabled silently'];
66

77
const setting = settings.register({
88
name: 'New plugin behavior',
99
key: 'enable.plugins',
1010
category: 'Plugins',
11-
data: [...enable, 'Disable with toast', 'Disable silently'],
11+
data: [...enable, 'Disabled with toast', 'Disabled silently'],
1212
type: 'select',
1313
});
1414

1515
wrap(() => {
1616
const name = 'enabled';
1717

1818
function mod(plugin) {
19-
// if (!plugin.version) return;
19+
// if (plugin.version === undefined) return;
2020
const enabled = plugin.settings().add({ key: 'Enabled', default: enable.includes(setting.value()) });
2121

2222
Object.defineProperty(plugin, name, {
23-
get: () => !!enabled.value(), // TODO: Why does it not default to false?
23+
get: () => enabled.value(),
2424
});
2525

2626
const registered = plugin.settings().add({ key: 'registered', hidden: true });
2727
if (!registered.value()) {
28-
const val = enable.includes(setting.value());
2928
if (setting.value().includes('toast')) {
3029
// TODO: Show toast to toggle setting from default
3130
} else {
3231
registered.set(true);
3332
}
34-
// Set initial value
35-
enabled.set(val);
3633
}
3734
}
3835

src/hooks/chat.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function bind(socketChat) {
5353
}
5454

5555
function reconnect() {
56-
if (!isActive() || guestMode.isSet() || reconnectAttempts > 3 || global('socketChat').readyState !== WebSocket.CLOSED) return;
56+
if (!isActive() || guestMode.isSet() || reconnectAttempts > 3 || global('socketChat', { throws: false })?.readyState !== WebSocket.CLOSED) return;
5757
reconnectAttempts += 1;
5858
const socket = new WebSocket(`wss://${location.hostname}/chat`);
5959
globalSet('socketChat', socket);
@@ -109,11 +109,6 @@ function sendMessageWrapper(...args) {
109109
}
110110
}
111111

112-
document.addEventListener('visibilitychange', () => {
113-
if (global('socketChat', { throws: false })?.readyState !== WebSocket.CLOSED) return;
114-
reconnect();
115-
});
116-
117112
eventManager.on(':loaded', () => {
118113
if (typeof socketChat !== 'undefined') {
119114
debug('Chat detected');
@@ -124,6 +119,8 @@ eventManager.on(':loaded', () => {
124119
// Attempt to reconnect when sending messages
125120
globalSet('sendMessage', sendMessageWrapper);
126121
globalSet('sendPrivateMessage', sendMessageWrapper);
122+
// Attempt to reconnect when coming back to this window
123+
document.addEventListener('visibilitychange', () => reconnect());
127124

128125
// Simulate old getHistory
129126
globalSet('appendChat', function appendChat(idRoom = '', chatMessages = [], isPrivate = true) {
@@ -145,28 +142,29 @@ eventManager.on(':loaded', () => {
145142
}, {
146143
throws: false,
147144
});
148-
eventManager.on('Chat:getHistory', ({ room, roomName: name }) => {
149-
// Send text hook
150-
const messages = $(`#${room} .chat-messages`);
151-
$(`#${room} input[type="text"]`).keydown(function sending(e) {
152-
if (e.which !== 13) return;
153-
154-
const data = {
155-
room,
156-
name,
157-
messages,
158-
input: this,
159-
};
160-
if (eventManager.cancelable.emit('Chat:send', data).canceled) {
161-
debug('Canceled send');
162-
$(this).val('');
163-
e.preventDefault();
164-
e.stopPropagation();
165-
}
166-
});
167-
});
168145
}
169146

147+
eventManager.on('Chat:getHistory', ({ room, roomName: name }) => {
148+
// Send text hook
149+
const messages = $(`#${room} .chat-messages`);
150+
$(`#${room} input[type="text"]`).keydown(function sending(e) {
151+
if (e.which !== 13) return;
152+
153+
const data = {
154+
room,
155+
name,
156+
messages,
157+
input: this,
158+
};
159+
if (eventManager.cancelable.emit('Chat:send', data).canceled) {
160+
debug('Canceled send');
161+
$(this).val('');
162+
e.preventDefault();
163+
e.stopPropagation();
164+
}
165+
});
166+
});
167+
170168
eventManager.on('GuestMode', () => {
171169
console.debug('Guest Mode');
172170
guestMode.set(true);

src/structures/chat.local/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export default class Chat {
1010
update(data) {
1111
switch (data.action) {
1212
case 'getMessage':
13-
case 'getPrivateMessage':
13+
case 'getPrivateMessage': {
1414
// TODO: Add new message
1515
break;
16+
}
1617
default: break;
1718
}
1819
}

src/structures/chat.local/message.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,52 @@ import Base from '../base.js';
22
import User from './user.js';
33

44
export default class Message extends Base {
5-
constructor(data, channel) {
5+
#user;
6+
#room;
7+
#message;
8+
#deleted = false;
9+
#action;
10+
#rainbow;
11+
12+
constructor(data, channel = data.channel) {
613
super(data);
7-
this.user = new User(data.user);
8-
this.room = channel;
9-
this.deleted = false;
10-
this.isAction = data.me === true;
11-
this.isRainbow = data.rainbow === true;
12-
this.message = data.message;
14+
this.#user = new User(data.user || data.author);
15+
this.#room = channel;
16+
this.#message = data.chatMessage || data.message;
17+
this.#action = (data.action || data.me) === true;
18+
this.#rainbow = data.rainbow === true;
1319
this.update(data);
1420
}
1521

1622
update(data) {
17-
if (data.deleted !== undefined) this.deleted = data.deleted === true;
23+
if (data.deleted !== undefined) this.#deleted = data.deleted === true;
1824
}
1925

2026
get channel() {
21-
return this.room;
27+
return this.#room;
2228
}
2329

24-
get text() {
25-
return this.message;
30+
get message() {
31+
return this.#message;
2632
}
2733

2834
get action() {
29-
return this.isAction;
35+
return this.#action;
3036
}
3137

32-
get sender() {
33-
return this.user;
38+
get author() {
39+
return this.#user;
3440
}
3541

3642
get deleted() {
37-
return this.isDeleted;
43+
return this.#deleted;
3844
}
3945

4046
get rainbow() {
41-
return this.isRainbow;
47+
return this.#rainbow;
48+
}
49+
50+
get element() {
51+
return document.querySelector(`#${this.channel} #message-${this.id}`);
4252
}
4353
}

src/structures/chat.local/user.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Base from '../base.js';
2+
import { DESIGNER, SUPPORTER } from '../constants/user.js';
23

34
export default class User extends Base {
45
constructor(data) {
@@ -9,4 +10,14 @@ export default class User extends Base {
910

1011
update(data) {
1112
}
13+
14+
get isMod() {
15+
const { priority } = this;
16+
return priority && priority <= SUPPORTER;
17+
}
18+
19+
get isStaff() {
20+
const { priority } = this;
21+
return priority && priority <= DESIGNER; // Why did I pick designer?
22+
}
1223
}

src/utils/1.variables.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ export const footer2 = '<div style="width:100%;text-align:center;font-size:12px;
44
export const hotkeys = [];
55
export const scriptVersion = GM_info.script.version;
66

7+
export const SOCKET_SCRIPT_CLOSED = 3500;
8+
79
export function noop() {}

src/utils/plugin.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,28 @@ const nameRegex = /^[a-z0-9 ]+$/i;
77
const registry = new Map();
88
const modules = [];
99

10+
function load({ name, mod, dependencies = [], runs = 0 }, methods, local) {
11+
if (methods[name] !== undefined) {
12+
console.error(`Skipping "${name}": Already exists`);
13+
return;
14+
}
15+
const required = dependencies.filter((module) => methods[module] === undefined);
16+
if (required.length) {
17+
if (runs < 5) local.push({ name, mod, dependencies: required, runs: runs + 1 });
18+
return;
19+
}
20+
try {
21+
const val = mod(methods);
22+
if (val !== undefined) {
23+
methods[name] = val;
24+
}
25+
} catch (e) {
26+
console.error(`Error loading "${name}":`, e);
27+
}
28+
}
29+
1030
// TODO: RegisteredPlugin
1131
export default function Plugin(name = '') {
12-
// eslint-disable-next-line no-param-reassign
13-
name = name.trim();
14-
if (name === '') throw new Error('Plugin must have a name');
1532
if (name.length > 20) throw new Error(`Plugin name too long (${name.length}/20)`);
1633
if (!nameRegex.test(name)) throw new Error('Name contains illegal characters');
1734
if (registry.has(name)) throw new Error('Name already registered');
@@ -21,28 +38,8 @@ export default function Plugin(name = '') {
2138
};
2239

2340
const local = [...modules];
24-
function load({ name: prop, mod, dependencies = [], runs = 0 }) {
25-
if (methods[prop] !== undefined) {
26-
console.error(`Skipping "${prop}": Already exists`);
27-
return;
28-
}
29-
const required = dependencies.filter((module) => methods[module] === undefined);
30-
if (required.length) {
31-
// eslint-disable-next-line no-param-reassign, no-plusplus
32-
if (runs++ < 5) local.push({ name: prop, mod, dependencies: required, runs });
33-
return;
34-
}
35-
try {
36-
const val = mod(methods);
37-
if (val !== undefined) {
38-
methods[prop] = val;
39-
}
40-
} catch (e) {
41-
console.error(`Error loading "${prop}":`, e);
42-
}
43-
}
4441
for (let i = 0; i < local.length; i++) {
45-
load(local[i]);
42+
load(local[i], methods, local);
4643
}
4744
local.filter(({ runs = 0, dependencies = [] }) => runs === 5 && dependencies.length)
4845
.forEach(({ name: prop, dependencies }) => console.log(`Failed to load module: ${prop} [${dependencies.join(', ')}]`));
@@ -52,7 +49,10 @@ export default function Plugin(name = '') {
5249
return plugin;
5350
}
5451

55-
api.register('plugin', Plugin);
52+
api.register('plugin', (name) => {
53+
if (typeof name !== 'string' || !name.trim()) throw new Error('Plugin must have a name');
54+
return Plugin(name.trim());
55+
});
5656

5757
export function registerModule(name, mod, dependencies) {
5858
if (!name) throw new Error('Module has no name');

src/utils/user.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function isStaff(user) {
2929
}
3030

3131
function isPriority(user, priority) {
32-
if (!user || !user.mainGroup || typeof user.mainGroup.priority !== 'number') throw new Error('Invalid user');
32+
if (typeof user?.mainGroup?.priority !== 'number') throw new Error('Invalid user');
3333
return user.mainGroup.priority <= priority;
3434
}
3535

@@ -50,7 +50,7 @@ export function getArtifacts() {
5050
}
5151

5252
export function getAllArtifacts() { // TODO: This isn't really a "user" function
53-
return getDeckConfig().then(({ allArtifacts: data }) => parse(data));
53+
return getDeckConfig().then(({ allArtifacts: data = [] }) => parse(data));
5454
}
5555

5656
function getDeckConfig() {

0 commit comments

Comments
 (0)