Last active
July 26, 2018 15:19
-
-
Save mgiachetti/4b9e146283ac7cf5e9da2ba5c5d610af to your computer and use it in GitHub Desktop.
Add voice messages to slack. Append this code at the end of the file /Applications/slack.app/Contents/Resources/app.asar.unpacked/index.js (this works on windows too)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node electron | |
const { remote } = require('electron') | |
const currentWindow = remote.getCurrentWindow() | |
// document.addEventListener('mousedown', function() { | |
// remote.BrowserWindow.getAllWindows().forEach(function(win) { | |
// win.webContents.openDevTools({mode: 'detach'}); | |
// }); | |
// remote.getCurrentWindow().getBrowserView().webContents.openDevTools({mode: 'detach'}); | |
// }, true) | |
function inject() { | |
remote.getCurrentWindow().getBrowserView().webContents.executeJavaScript("$('.msg_input_wrapper').length == 0", false, (result) => { | |
if (result) { | |
setTimeout(inject, 200); | |
return; | |
} | |
remote.getCurrentWindow().getBrowserView().webContents.executeJavaScript(script); | |
}); | |
} | |
inject(); | |
var script = ` | |
$(function() { | |
function uploadFile(file) { | |
var fakeDropEvent = new DragEvent('drop'); | |
Object.defineProperty(fakeDropEvent, 'dataTransfer', { | |
value: { files: [file], getData: () => {} } | |
}); | |
Object.defineProperty(fakeDropEvent, 'shiftKey', { | |
value: true | |
}); | |
// temporary disable thread file feature | |
var tmp = TS.boot_data.feature_file_threads; | |
TS.boot_data.feature_file_threads = false; | |
document.body.dispatchEvent(fakeDropEvent); | |
setTimeout(() => { | |
TS.boot_data.feature_file_threads = tmp | |
}); | |
} | |
let recording = false; | |
let mediaRecorder; | |
function recordAudio(e) { | |
e.preventDefault(); | |
if (recording) { | |
recording = false; | |
$('#recordAudio').css('color', ''); | |
mediaRecorder.stop(); | |
return; | |
} | |
recording = true; | |
$('#recordAudio').css('color', 'red'); | |
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => { | |
//const options = { mimeType: 'audio/wav' }; | |
const options = {}; | |
mediaRecorder = new MediaRecorder(stream, options); | |
mediaRecorder.start(); | |
const audioChunks = []; | |
mediaRecorder.addEventListener("dataavailable", event => { | |
console.log('data'); | |
audioChunks.push(event.data); | |
}); | |
//setTimeout(() => { | |
// mediaRecorder.stop(); | |
// console.log('stop') | |
//}, 3000); | |
mediaRecorder.addEventListener("stop", event => { | |
var file = new File(audioChunks, "audio.wav", {'type' : 'audio/wav; codecs=opus', lastModified: Date.now()}) | |
uploadFile(file); | |
}); | |
}); | |
} | |
function inject() { | |
if ($('.msg_input_wrapper').length == 0) { | |
setTimeout(inject, 200); | |
return; | |
} | |
$('.msg_input_wrapper').append(\`<button id="recordAudio" type="button" class="btn_unstyle msg_mentions_button" aria-label="Record Audio" tabindex="3" style=" | |
right: 68px; | |
width: 30px | |
"> | |
<i class="ts_icon ts_icon_microphone"></i> | |
</button> | |
\`); | |
setTimeout(() => { | |
$('#recordAudio').off('click'); | |
$('#recordAudio').click(recordAudio); | |
}, 500); | |
} | |
inject(); | |
}); | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Click on the microphone and start recording, click again to stop and send the message.
