discord-sandboxed/renderer.js

133 lines
4.8 KiB
JavaScript
Raw Normal View History

2019-10-15 06:21:54 +00:00
const { remote, ipcRenderer } = require('electron')
2019-10-14 19:23:56 +00:00
2019-10-19 04:30:26 +00:00
function removeBloat(webview) {
webview.executeJavaScript(`
document.getElementsByClassName("anchor-3Z-8Bb anchorUnderlineOnHover-2ESHQB")[0].remove();
document.getElementsByClassName("contents-18-Yxp button-3AYNKb button-2vd_v_")[0].remove();
`)
}
2019-10-14 19:23:56 +00:00
onload = () => {
const webview = document.querySelector('webview')
2019-10-15 00:35:25 +00:00
ipcRenderer.send('asynchronous-message', 'DOMready')
2019-10-16 20:52:33 +00:00
// Execute JS into the webview to detect when logging in is complete
webview.addEventListener('did-finish-load', function() {
webview.executeJavaScript(`
let dlButton = document.getElementsByClassName("listItem-2P_4kh");
t = setInterval(function(){
if(dlButton.length != 0) {
console.log("discord-load-complete")
clearInterval(t)
}else {
console.log("waiting for load")
}
2019-10-19 04:30:26 +00:00
}, 500);
2019-10-16 20:52:33 +00:00
`)
});
2019-10-15 00:35:25 +00:00
2019-10-14 19:23:56 +00:00
webview.addEventListener('console-message', (e) => {
2019-10-14 19:59:30 +00:00
if (e.message === "Constructed RTCPeerConnection") {
console.log("Connected to server")
ipcRenderer.send('asynchronous-message', 'connected')
}
2019-10-14 19:23:56 +00:00
if (e.message === "Close RTCPeerConnection") {
console.log("Disconnected from server")
2019-10-14 19:59:30 +00:00
ipcRenderer.send('asynchronous-message', 'disconnected')
2019-10-14 19:23:56 +00:00
}
2019-10-16 20:52:33 +00:00
2019-10-19 04:30:26 +00:00
if (e.message === "muted") {
console.log("Self Muted in Discord")
ipcRenderer.send('asynchronous-message', 'self-muted')
}
if (e.message === "unmuted") {
console.log("Self Muted in Discord")
ipcRenderer.send('asynchronous-message', 'self-unmuted')
}
if (e.message === "signalingState => stable, negotiation needed: false") {
console.log("Mute/Unmute")
removeBloat(webview)
webview.executeJavaScript(`
if (document.querySelectorAll('[aria-label="Mute"]').length === 0){
console.log("muted")
}else {
console.log("unmuted")
}
`)
}
if (e.message === "DOM changed") {
removeBloat(webview)
}
// Execute JS into the webview after login
// Removes download button and help button
2019-10-16 20:52:33 +00:00
if (e.message === "discord-load-complete") {
webview.executeJavaScript(`
2019-10-19 04:30:26 +00:00
document.getElementsByClassName("listItem-2P_4kh")[document.getElementsByClassName("listItem-2P_4kh").length - 1].remove();
const targetNode = document.getElementsByClassName("scroller-2FKFPG firefoxFixScrollFlex-cnI2ix systemPad-3UxEGl scroller-2TZvBN")[0]
const config = { attributes: true, childList: true, subtree: true };
const callback = function(mutationsList, observer) {
for(let mutation of mutationsList) {
if (mutation.type === 'childList') {
console.log('DOM changed');
}
else if (mutation.type === 'attributes') {
console.log('DOM changed');
}
}
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
2019-10-16 20:52:33 +00:00
`)
2019-10-19 04:30:26 +00:00
removeBloat(webview)
2019-10-16 20:52:33 +00:00
}
2019-10-14 19:23:56 +00:00
})
2019-10-15 00:35:25 +00:00
ipcRenderer.on('ping', (event, msg) => {
if (msg === 'mic-open'){
2019-10-14 21:14:02 +00:00
console.log("talking")
2019-10-14 21:07:19 +00:00
webview.sendInputEvent({keyCode: 'Backspace', type: 'keyDown'});
webview.sendInputEvent({keyCode: 'Backspace', type: 'char'});
2019-10-15 06:21:54 +00:00
document.getElementById("title-bar-status").style.backgroundColor = "green"
document.getElementById("title-bar-controls").style.backgroundColor = "green"
document.getElementById("title-bar").style.backgroundColor = "green"
2019-10-14 21:07:19 +00:00
}
2019-10-15 00:35:25 +00:00
if (msg === 'mic-closed'){
2019-10-14 21:14:02 +00:00
console.log("not talking")
2019-10-14 21:07:19 +00:00
webview.sendInputEvent({keyCode: 'Backspace', type: 'keyUp'});
webview.sendInputEvent({keyCode: 'Backspace', type: 'char'});
2019-10-19 04:30:26 +00:00
document.getElementById("title-bar-status").style.backgroundColor = "#212226"
document.getElementById("title-bar-controls").style.backgroundColor = "#212226"
document.getElementById("title-bar").style.backgroundColor = "#212226"
2019-10-15 06:21:54 +00:00
2019-10-14 21:07:19 +00:00
}
})
2019-10-15 00:35:25 +00:00
ipcRenderer.on('devMode', (event, msg) => {
console.log(`Dev Mode: ${msg}`)
if (msg === true) {
webview.openDevTools()
}
})
2019-10-15 06:21:54 +00:00
}
document.getElementById('minimize-button').addEventListener('click', () => {
remote.getCurrentWindow().minimize()
})
document.getElementById('close-button').addEventListener('click', () => {
remote.app.quit()
})