-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssb-interop.js
146 lines (124 loc) · 4.71 KB
/
ssb-interop.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
/**
* The preload script needs to stay in regular ole JavaScript, because it is
* the point of entry for electron-compile.
*/
const allowedChildWindowEventMethod = [
'windowWithTokenBeganLoading',
'windowWithTokenFinishedLoading',
'windowWithTokenCrashed',
'windowWithTokenDidChangeGeometry',
'windowWithTokenBecameKey',
'windowWithTokenResignedKey',
'windowWithTokenWillClose'
];
if (window.location.href !== 'about:blank') {
const preloadStartTime = process.hrtime();
require('./assign-metadata').assignMetadata();
if (window.parentWebContentsId) {
//tslint:disable-next-line:no-console max-line-length
const warn = () => console.warn(`Deprecated: direct access to global object 'parentInfo' will be disallowed. 'parentWebContentsId' will be available until new interface is ready.`);
Object.defineProperty(window, 'parentInfo', {
get: () => {
warn();
return {
get webContentsId() {
warn();
return parentWebContentsId;
}
};
}
});
}
const { ipcRenderer, remote } = require('electron');
ipcRenderer
.on('SLACK_NOTIFY_CHILD_WINDOW_EVENT', (event, method, ...args) => {
try {
if (!TSSSB || !TSSSB[method]) throw new Error('Webapp is not fully loaded to execute method');
if (!allowedChildWindowEventMethod.includes(method)) {
throw new Error('Unsupported method');
}
TSSSB[method](...args);
} catch (error) {
console.error(`Cannot execute method`, { error, method }); //tslint:disable-line:no-console
}
});
ipcRenderer
.on('SLACK_REMOTE_DISPATCH_EVENT', (event, data, origin, browserWindowId) => {
const evt = new Event('message');
evt.data = JSON.parse(data);
evt.origin = origin;
evt.source = {
postMessage: (message) => {
if (!desktop || !desktop.window || !desktop.window.postMessage) throw new Error('desktop not ready');
return desktop.window.postMessage(message, browserWindowId);
}
};
window.dispatchEvent(evt);
event.sender.send('SLACK_REMOTE_DISPATCH_EVENT');
});
const { init } = require('electron-compile');
const { assignIn } = require('lodash');
const path = require('path');
const { isPrebuilt } = require('../utils/process-helpers');
//tslint:disable-next-line:no-console
process.on('uncaughtException', (e) => console.error(e));
/**
* Patch Node.js globals back in, refer to
* https://electron.atom.io/docs/api/process/#event-loaded.
*/
const processRef = window.process;
process.once('loaded', () => {
window.process = processRef;
});
window.perfTimer.PRELOAD_STARTED = preloadStartTime;
// Consider "initial team booted" as whether the workspace is the first loaded after Slack launches
ipcRenderer.once('SLACK_PRQ_TEAM_BOOT_ORDER', (_event, order) => {
window.perfTimer.isInitialTeamBooted = order === 1;
});
ipcRenderer.send('SLACK_PRQ_TEAM_BOOTED'); // Main process will respond SLACK_PRQ_TEAM_BOOT_ORDER
const resourcePath = path.join(__dirname, '..', '..');
const mainModule = require.resolve('../ssb/main.ts');
const isDevMode = loadSettings.devMode && isPrebuilt();
init(resourcePath, mainModule, !isDevMode);
}
// First make sure the wrapper app is loaded
document.addEventListener("DOMContentLoaded", function() {
// Then get its webviews
let webviews = document.querySelectorAll(".TeamView webview");
// Fetch our CSS in parallel ahead of time
const cssPath = 'https://imoriarty.github.io/slack_dark_theme_custom/slack_dark.css';
let cssPromise = fetch(cssPath).then(response => response.text());
let customCustomCSS = `
:root {
/* Modify these to change your theme colors: */
--primary: #09F;
--text: #CCC;
--background: #080808;
--background-elevated: #222;
}
`
// Insert a style tag into the wrapper view
cssPromise.then(css => {
let s = document.createElement('style');
s.type = 'text/css';
s.innerHTML = css + customCustomCSS;
document.head.appendChild(s);
});
// Wait for each webview to load
webviews.forEach(webview => {
webview.addEventListener('ipc-message', message => {
if (message.channel == 'didFinishLoading')
// Finally add the CSS into the webview
cssPromise.then(css => {
let script = `
let s = document.createElement('style');
s.type = 'text/css';
s.id = 'slack-custom-css';
s.innerHTML = \`${css + customCustomCSS}\`;
document.head.appendChild(s);
`
webview.executeJavaScript(script);
})
});
});
});