This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
googleChatExtender.js
124 lines (102 loc) · 3.98 KB
/
googleChatExtender.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
/*
NAME: Google Chat Extender
DESCRIPTION: GCE allow you to customize your Google Chat app with theme, plugins and tweaks.
VERSION: 0.6
AUTHOR: Toinane
*/
/*
DEEP CUSTOMIZATION IN MAIN.JS
# How to change the "@" bagde on the Dock icon?
search and find "ld.dock.setBadge("@");" in main.js
Change "@" with another char/number. Done.
# How to use ipcMain/ipcRenderer from Electron?
search and find "nodeIntegration: !1" in main.js
Change "!1" to "true". Done. /!\ Use this as a last resort!
All plugins will can use NodeJS and can be a risk for users.
*/
'use strict'
const util = require('util');
const fs = require('fs');
const {app, webContents} = require('electron');
const readdir = util.promisify(fs.readdir);
const readFile = util.promisify(fs.readFile);
let chat;
log('Google Chat Extender found');
function log(text, type) {
let css = {
'warn': 'color: red; background: yellow;text-transform:uppercase;font-size:2em;',
'error': 'color: white; background: red;text-transform:uppercase;font-size:2em;',
'ok': 'color: white; background: #52CE80;text-transform:uppercase;font-size:2em;'
}
console.log('['+process.uptime().toFixed(3).padStart(10)+'][INFO ][GCE ###]', text);
if(chat !== undefined) chat.executeJavaScript('console.log("%c'+text+'", "'+css[type]+'");');
}
async function findPlugins(chat) {
let files = await readdir(__dirname + '/plugins');
let plugins = await Promise.all(files.map(async file => {
if(!(/.*.js/gm.exec(file))) return log(file + ' is not a plugin. Plugin must be a .js file.', 'error')
let content = await readFile(__dirname + `/plugins/${file}`, 'utf-8');
return getPluginConfig(file, content);
}));
return plugins;
}
function getPluginVariables(plugin) {
const regex = /(const __)(.*)( = )(.*)(;)(.*\/\*)(.*)(.*\*\/)/gm;
let m, vars = [];
while ((m = regex.exec(plugin)) !== null) {
if (m.index === regex.lastIndex) regex.lastIndex++;
let current = [];
let type = 'text';
m.forEach(function(match, groupIndex) { current.push(match) });
let content = current[4].trim();
if(content.charAt(0) === '"' && content.charAt(content.length-1) === '"') {
type = 'textarea';
content = content.slice(1).slice(0, -1);
}
else if(content === 'true' || content === 'false') type = 'checkbox';
else if(Number.isInteger(parseInt(content))) type = 'number';
else if(content.charAt(0) === "'" && content.charAt(content.length-1) === "'") content = content.slice(1).slice(0, -1);
vars.push({
name: current[2].trim(),
content: content,
description: current[7].trim(),
type: type
})
}
return vars;
}
function getPluginConfig(file, plugin) {
let name = /(NAME: )(.*)/gm.exec(plugin);
let description = /(DESCRIPTION: )(.*)/gm.exec(plugin);
let version = /(VERSION: )(.*)/gm.exec(plugin);
let author = /(AUTHOR: )(.*)/gm.exec(plugin);
let vars = getPluginVariables(plugin);
if(!name[2]) return log(file + '\'s name missing. We can\'t add this plugin in parameters.', 'warn');
return {
name: name[2],
codeName: name[2].replace(' ', '.').toLowerCase(),
description: description[2],
version: version[2],
author: author[2],
variables: vars,
content: plugin
};
}
async function launchPlugins(chat) {
log('Initializing Google Chat Extender', 'warn');
let plugins = await findPlugins(chat);
let googleChatExtenderCore = await readFile(__dirname+'/googleChatExtenderPlugin.js', 'utf-8');
chat.executeJavaScript(`const plugins = ${JSON.stringify(plugins)}; ` + googleChatExtenderCore);
log('Google Chat Extender Loaded!', 'ok');
}
app.on('browser-window-created', function(event, win) {
let web = win.webContents;
//web.openDevTools();
web.on('dom-ready', () => {
if(!web.history[0].includes('https://chat.google.com/')) return; // Load GCE only on Chat window. Delete this line if GCE doesn't work.
launchPlugins(web);
});
web.on('will-navigate', () => {
setTimeout(() => launchPlugins(web), 500);
});
});