discord-sandboxed/views/js/mainRender.js

294 lines
11 KiB
JavaScript
Raw Normal View History

let blockedLibrary = {}
let isConnectedToVoiceServer = false
let keepAliveClientOP = null
let keepAliveRemoteOP = null
let windowName = 0
function convertObjToString(arr) {
let arrStr = `[`
arr.forEach(function(i, idx, array){
let _subStr = i.toString()
arrStr = arrStr.concat("'").concat(_subStr).concat("'")
if (idx !== array.length - 1){
arrStr = arrStr.concat(`,`)
}
})
arrStr = arrStr.concat(`]`).toString()
return arrStr
}
2019-12-21 01:04:26 +00:00
function openMic(webview){
console.log("talking")
2019-12-22 01:43:29 +00:00
document.getElementById("overlay").style.display = "block";
webview.sendInputEvent({keyCode: 'Backspace', type: 'keyDown'});
2020-06-20 19:00:15 +00:00
//webview.sendInputEvent({keyCode: 'Backspace', type: 'char'});
2019-12-21 01:04:26 +00:00
}
2019-10-19 18:51:08 +00:00
function muteMic(webview){
console.log("not talking")
2019-12-22 01:43:29 +00:00
document.getElementById("overlay").style.display = "none";
2019-10-19 18:51:08 +00:00
webview.sendInputEvent({keyCode: 'Backspace', type: 'keyUp'});
2020-06-20 19:00:15 +00:00
//webview.sendInputEvent({keyCode: 'Backspace', type: 'char'});
2019-10-19 18:51:08 +00:00
}
function removeBloat(webview) {
console.log("--Removing bloat")
bloatList = [
'noticeDefault',
'noticeBrand',
'actionButtons-14eAc_',
]
bloatList.forEach(function(tag){
webview.executeJavaScript(`
document.querySelectorAll("div[class^=${tag}]").forEach(e => {
console.log("Removing ", e)
e.style.display = 'none'
})
`)
})
}
2019-10-24 01:27:57 +00:00
// Creates an observer for user list to detect if server is switched
function userListChangeListener(webview) {
webview.executeJavaScript(`
const userList = document.getElementsByClassName("sidebar-2K8pFh")[0]
const userListconfig = { attributes: false, childList: true, subtree: true, characterData: false };
const userListChangeCallback = function(mutationsList, observer) {
console.log('--user list changed');
if (document.querySelectorAll('[aria-label="Disconnect"]').length === 1){
console.log('--user is connected to voice server')
2019-10-24 01:27:57 +00:00
}else {
console.log('--user is not connected to voice server')
2019-10-24 01:27:57 +00:00
}
};
const userListObserver = new MutationObserver(userListChangeCallback);
userListObserver.observe(userList, userListconfig);
`)
}
function userMuteDeafenListener(webview) {
webview.executeJavaScript(`
const userMuteDeafen = document.getElementsByClassName("container-3baos1")[0]
const userMuteDeafenconfig = { attributes: false, childList: true, subtree: true, characterData: false };
const userMuteDeafencallback = function(mutationsList, observer) {
2019-10-25 03:18:02 +00:00
isMicMuted()
2019-10-24 01:27:57 +00:00
};
const userMuteDeafenObserver = new MutationObserver(userMuteDeafencallback);
userMuteDeafenObserver.observe(userMuteDeafen, userMuteDeafenconfig);
`)
}
2019-10-14 19:23:56 +00:00
onload = () => {
2019-12-22 01:43:29 +00:00
document.getElementById("overlay").style.display = "none";
2019-10-14 19:23:56 +00:00
const webview = document.querySelector('webview')
2019-10-15 00:35:25 +00:00
const whiteList = [
'PATCH', // Mute/Unmute/notification/cosmetic guild changes
'DELETE', // Leaving a guild / Deleting messages
'https://discord.com/api/v8/channels/', // Text channel address
'https://discord.com/api/v8/auth/login', // Login address
'https://discord.com/api/v8/invites/', // Accepting guild invite
'https://discord.com/api/v8/voice/regions', // Required when creating new guild
'https://discord.com/api/v8/guilds', // Creating a guild
'https://discord.com/api/v8/gateway' // This may be required to get past login screen if not cached locally
]
const _whiteList = convertObjToString(whiteList)
2019-10-20 03:38:47 +00:00
// Insert JS to detect when discord finishes loading
2019-10-16 20:52:33 +00:00
webview.addEventListener('did-finish-load', function() {
// Discord does not do client-side hashing
webview.executeJavaScript(`
(function(open, send) {
let whiteList = ${_whiteList}
let xhrOpenRequestUrl
let xhrSendResponseUrl
let xhrMethod
let responseData
let _done = false
let _block = true
let _isWhitelisted = false
XMLHttpRequest.prototype.open = function(method, url, async, x, y) {
xhrMethod = method.toString()
xhrOpenRequestUrl = url.toString()
if (xhrOpenRequestUrl.includes("science")) {
console.log("--BLOCKED.OPEN|" + xhrOpenRequestUrl)
return open.apply(this, false)
}
console.log("EVALUATING:", xhrOpenRequestUrl)
whiteList.forEach( wl => {
if (xhrOpenRequestUrl.includes(wl) || xhrMethod.includes(wl)) {
_done = true
_block = false
_isWhitelisted = true
console.log("--ALLOWED.OPEN", xhrOpenRequestUrl + "")
return open.apply(this, arguments)
}
})
if (_done === false) {
console.log("--BLOCKED.OPEN|" + xhrOpenRequestUrl)
return open.apply(this, false)
}
}
XMLHttpRequest.prototype.send = function(data) {
if (_block === true || _isWhitelisted === false) {
console.log("--BLOCKED.SEND", data, xhrOpenRequestUrl, _isWhitelisted)
return send.apply(this, false)
}
if (_block === false && _isWhitelisted === true) {
if (data && !data.toString().includes("password")) {
console.log("--ALLOWED.SEND", data, xhrOpenRequestUrl, _isWhitelisted)
}else {
console.log("--ALLOWED.SEND")
}
return send.apply(this, arguments)
}
}
})(XMLHttpRequest.prototype.open, XMLHttpRequest.prototype.send)
`)
2019-10-20 03:38:47 +00:00
webview.executeJavaScript(`
let dlButton = document.getElementsByClassName("listItem-2P_4kh");
t = setInterval(function(){
if(dlButton.length != 0) {
2019-10-24 01:27:57 +00:00
console.log("--discord-load-complete")
2019-10-20 03:38:47 +00:00
clearInterval(t)
2019-10-25 03:18:02 +00:00
isMicMuted()
2019-10-20 03:38:47 +00:00
}else {
console.log("waiting for load")
}
}, 500);
`)
2019-10-25 03:18:02 +00:00
// Insert a function that will be called later
webview.executeJavaScript(`
function isMicMuted() {
if (document.querySelectorAll('[aria-label="Mute"]')[0].getAttribute("aria-checked") === "false"){
console.log("unmuted")
}else {
console.log("muted")
}
2019-10-25 03:18:02 +00:00
}
`)
})
2019-10-15 00:35:25 +00:00
2019-10-20 03:38:47 +00:00
// Send commands to preload.js
webview.addEventListener('console-message', (e) => {
if (e.message === "--user is connected to voice server") {
2019-10-14 19:59:30 +00:00
console.log("Connected to server")
2019-10-20 03:38:47 +00:00
window.postMessage({ type: "connected"}, "*")
2020-06-20 19:00:15 +00:00
removeBloat(webview)
isConnectedToVoiceServer = true
2019-10-14 19:59:30 +00:00
}
2019-10-14 19:23:56 +00:00
if (e.message === "--user is not connected to voice server") {
2019-10-14 19:23:56 +00:00
console.log("Disconnected from server")
2019-10-20 03:38:47 +00:00
window.postMessage({ type: "disconnected"}, "*")
isConnectedToVoiceServer = false
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")
2019-10-20 03:38:47 +00:00
window.postMessage({ type: "self-muted"}, "*")
2019-10-19 04:30:26 +00:00
}
if (e.message === "unmuted") {
2019-10-20 03:38:47 +00:00
console.log("Self Un-Muted in Discord")
window.postMessage({ type: "self-unmuted"}, "*")
2019-10-19 04:30:26 +00:00
}
// Execute JS into the webview after login
2019-10-24 01:27:57 +00:00
if (e.message === "--discord-load-complete") {
webview.executeJavaScript(`document.getElementsByClassName("listItem-2P_4kh")[document.getElementsByClassName("listItem-2P_4kh").length - 1].style.display = 'none';`) // Remove download button
2019-10-24 01:27:57 +00:00
userListChangeListener(webview)
userMuteDeafenListener(webview)
removeBloat(webview)
2019-10-16 20:52:33 +00:00
}
if (e.message.toString().includes("--BLOCKED.OPEN")) {
let _url = e.message.toString().split(",").find(a =>a.includes("http")).split("https://")[1]
if (!(_url in blockedLibrary)) {
blockedLibrary[_url] = 1
}
blockedLibrary[_url] += 1
window.postMessage({ type: "blockUpdate", payload: {url: _url, count: blockedLibrary[_url]}}, "*")
}
if (keepAliveClientOP === null) {
if (e.message.toString().includes("S->>|")) {
let _data = e.message.split("|")[1]
_data = JSON.parse(_data)
console.log(_data)
if (Number.isInteger(_data.d)){
keepAliveClientOP = _data.op
console.log("Client OP", keepAliveClientOP)
}
}
}
if (keepAliveRemoteOP === null) {
if (e.message.toString().includes("R-<<|")) {
let _data = e.message.split("|")[1]
_data = JSON.parse(_data)
if (Number.isInteger(_data.d)){
keepAliveRemoteOP = _data.op
console.log("Client OP", keepAliveRemoteOP)
}
}
}
2019-10-14 19:23:56 +00:00
})
// Accept commands from mainLoad.js
2019-10-20 03:38:47 +00:00
window.addEventListener(
"message",
event => {
if (event.origin === "file://" && event.source === window) {
if (event.data.type === "devMode" && event.data.text === "true") {
console.log("Dev Mode On")
webview.openDevTools()
}
2019-10-15 00:35:25 +00:00
if (event.data.type === 'unfocused'){
console.log("window unfocused")
document.getElementById('titleBar').style.color = "#7f7f7f"
}
if (event.data.type === 'focused'){
console.log("window focused")
document.getElementById('titleBar').style.color = "#ffffff"
}
if (event.data.type === 'micOpen'){
openMic(webview)
window.postMessage({ type: "confirmMicOpen"}, "*")
}
2019-10-20 03:38:47 +00:00
if (event.data.type === 'micClose'){
muteMic(webview)
window.postMessage({ type: "confirmMicClose"}, "*")
}
2019-10-15 06:21:54 +00:00
if (event.data.type === 'URLCopied') {
fadeBanner("copyConfirmBanner")
}
}
2019-10-20 03:38:47 +00:00
},
false
)
}