-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
77 lines (63 loc) · 2.58 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
const { Plugin } = require('powercord/entities');
const { get } = require('powercord/http');
const { waitFor, getOwnerInstance, findInTree, sleep } = require('powercord/util');
const { React, getModule, getModuleByDisplayName } = require('powercord/webpack');
const { inject, uninject } = require('powercord/injector');
const { CoinbasePro, WebSocketChannelName, WebSocketEvent } = require('coinbase-pro-node');
const { CRYPTO_CHANNELS, CRYPTO_DEBUG } = require("./constants");
const cryptoStore = require('./cryptoStore/store');
const cryptoStoreActions = require('./cryptoStore/actions');
const Modal = require('./components/Modal');
module.exports = class CryptoLive extends Plugin {
constructor () {
super();
this.channel = {
name: WebSocketChannelName.TICKER,
product_ids: Object.keys(CRYPTO_CHANNELS)
};
this.client = new CoinbasePro();
this.client.ws.on(WebSocketEvent.ON_OPEN, () => this.onCoinbaseConnect());
this.client.ws.on(WebSocketEvent.ON_MESSAGE_TICKER, (m) => this.onCoinbaseTicker(m));
}
pluginWillUnload () {
client.ws.disconnect();
uninject('pc-crypto-modal');
window.TradingView = undefined; // terrible but works ig
const { container } = getModule([ 'container', 'usernameContainer' ], false);
const accountContainer = document.querySelector(`section > .${container}`);
const instance = getOwnerInstance(accountContainer);
instance.forceUpdate();
}
async startPlugin () {
this.client.ws.connect({ debug: CRYPTO_DEBUG });
this.loadStylesheet('style.scss');
this._injectModal();
}
async _injectModal() {
await sleep(1e3); // It ain't stupid if it works
const { container } = await getModule([ 'container', 'usernameContainer' ]);
const accountContainer = await waitFor(`section > .${container}`);
const instance = getOwnerInstance(accountContainer);
await inject('pc-crypto-modal', instance.__proto__, 'render', (_, res) => {
const realRes = findInTree(res, t => t.props && t.props.className === container);
return [
React.createElement(Modal, {
entityID: this.entityID,
base: realRes
}),
res
];
});
instance.forceUpdate();
}
onCoinbaseConnect() {
this.client.ws.subscribe([this.channel]);
}
onCoinbaseTicker(tickerMessage) {
if(tickerMessage.type !== "ticker" || this.channel.product_ids.findIndex(x => x === tickerMessage.product_id) < 0)
return;
if(cryptoStore.storeLoading === true)
cryptoStoreActions.updateCryptoLoading(false);
cryptoStoreActions.updateCryptoPrice(tickerMessage);
}
};