Created
April 28, 2015 18:37
-
-
Save khankuan/b2035f76dfa1374e9d9b to your computer and use it in GitHub Desktop.
Random gist for FB chat with Web Speech
This file contains 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
/* | |
Commands: | |
/muteall | |
/unmuteall | |
/muteme | |
/unmuteme | |
/mute | |
/unmute | |
/voice whisper | |
/voice 67 | |
*/ | |
window.chatObservers = []; | |
window.currentVoice = 0; | |
window.randomInt = new Date().getTime(); | |
window.lastMessageNodes = {}; | |
window.muteMe = false; | |
window.muteAll = false; | |
window.voiceMap = { | |
male: 1, | |
female: 2, | |
indian: 3, | |
grandma: 6, | |
japanese: 7, | |
troll :18, | |
'church-bell': 19, | |
autobot: 20, | |
transformer: 24, | |
joker: 33, | |
choir: 32, | |
whisper: 67, | |
}; | |
window.replacedVoiceMap = {}; | |
window.muteConvMap = {}; | |
function checkMessage(convMsgs, id, isNew){ | |
var newMsg; | |
if (convMsgs.lastChild.children.length == 4) | |
newMsg = convMsgs.lastChild.children[2].innerText; | |
else | |
newMsg = convMsgs.lastChild.innerText; | |
// Msg from me | |
var fromMe = false; | |
if (convMsgs.lastChild.className.indexOf("_1nc6") >= 0) | |
fromMe = true; | |
// Skip same | |
if (window.lastMessageNodes[id] == convMsgs.lastChild) | |
return; | |
window.lastMessageNodes[id] = convMsgs.lastChild; | |
// CMD | |
var isCmd = false; | |
if (newMsg.trim() == "/mute" && fromMe){ | |
isCmd = true; | |
window.muteConvMap[id] = true; | |
newMsg = "We will shutup"; | |
} | |
if (newMsg.trim() == "/unmute" && fromMe){ | |
isCmd = true; | |
delete window.muteConvMap[id]; | |
newMsg = "We may speak again"; | |
} | |
if (newMsg.trim() == "/muteme" && fromMe){ | |
isCmd = true; | |
window.muteMe = true; | |
newMsg = "I will shutup"; | |
} | |
if (newMsg.trim() == "/unmuteme" && fromMe){ | |
isCmd = true; | |
window.muteMe = false; | |
newMsg = "I may speak again"; | |
} | |
if (newMsg.trim() == "/muteall"){ | |
isCmd = true; | |
window.muteAll = true; | |
newMsg = "All shut up"; | |
} | |
if (newMsg.trim() == "/unmuteall"){ | |
isCmd = true; | |
window.muteAll = false; | |
newMsg = "All may speak again"; | |
} | |
if (newMsg.trim().indexOf('/voice ') == 0){ | |
var voice = newMsg.trim().split(" ")[1]; | |
var index; | |
// By number or name | |
if (!isNaN(voice)) { | |
index = parseInt(voice); | |
} else | |
index = window.voiceMap[voice]; | |
if (window.speechSynthesis.getVoices()[index]){ | |
window.replacedVoiceMap[id] = index; | |
newMsg = "I have a new voice"; | |
} else { | |
newMsg = "Voice does not exist"; | |
} | |
} | |
// Mute | |
if (window.muteMe && fromMe && !isCmd) | |
return; | |
if (window.muteConvMap[id] && !isCmd) | |
return; | |
if (window.muteAll) | |
return; | |
if (fromMe && isNew) | |
return; | |
// Play | |
var msg = new SpeechSynthesisUtterance(newMsg); | |
msg.voice = window.speechSynthesis.getVoices()[window.replacedVoiceMap[id] || 0]; | |
window.speechSynthesis.speak(msg); | |
} | |
function observeSingleChat(conv, isNew){ | |
var id = window.randomInt++; | |
var convMsgs = conv.children[1].children[0].children[0].children[2].children[1].children[0].children[0].children[0].children[0].children[0].children[0].children[2].children[0]; | |
var observer = new window.WebKitMutationObserver(function(mutations) { | |
checkMessage(convMsgs, id); | |
}); | |
observer.observe(convMsgs, { childList: true}); | |
window.chatObservers.push({observer: observer, conv: conv}); | |
// Also read last message | |
if (isNew) | |
checkMessage(convMsgs, id, isNew); | |
} | |
function unobserveSingleChat(conv){ | |
window.chatObservers = window.chatObservers.filter(function(item){ | |
if (item.conv == conv){ | |
item.observe.disconnect(); | |
return false; | |
} | |
return true; | |
}); | |
} | |
function observeChat(){ | |
var fbChats = document.getElementById("ChatTabsPagelet").children[0].children[0].children[1].children; | |
for (var i = 0; i < fbChats.length; i++){ | |
(function(){ | |
var index = i; | |
var conv = fbChats[index]; | |
observeSingleChat(conv); | |
}()); | |
} | |
} | |
function observeChatParent(){ | |
observeChat(); | |
var fbChatsParent = document.getElementById("ChatTabsPagelet").children[0].children[1]; | |
var observer = new window.WebKitMutationObserver(function(mutations) { | |
var addedNodes = mutations[0].addedNodes; | |
var removedNodes = mutations[0].removedNodes; | |
for (var i = 0; i < addedNodes.length; i++){ | |
var conv = addedNodes[i]; | |
observeSingleChat(conv, true); | |
} | |
for (var i = 0; i < removedNodes.length; i++){ | |
var conv = addedNodes[i]; | |
unobserveSingleChat(conv); | |
} | |
}); | |
observer.observe(fbChatsParent, { childList: true}); | |
} | |
observeChatParent(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment