Created
March 9, 2019 12:14
-
-
Save notnownikki/41d927a8af32c5921ad173025674ac61 to your computer and use it in GitHub Desktop.
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
// ==UserScript== | |
// @name Nikki's YouTube chat monitor and responder | |
// @namespace https://nikkiandbunty.com/ | |
// @version 0.1 | |
// @description Monitor YouTube chat and respond to messages | |
// @author You | |
// @match https://www.youtube.com/* | |
// @grant none | |
// ==/UserScript== | |
const betPattern = /^[0-9]$/; | |
const winningPoints = 3; | |
const secondPoints = 1; | |
const styles = ` | |
* { background: #000; color: #fff; font-family: sans-serif; font-size: 32px; } | |
p { padding: 0; margin: 0; margin-bottom: 8px; } | |
`; | |
let statsWindow; | |
let initComplete = false; | |
let acceptingBets = false; | |
let bets = {}; | |
let scores = {}; | |
let horseScores = { | |
'1': 3, | |
'2': 10, | |
'3': 2, | |
'4': 6, | |
'5': 6, | |
'6': 12, | |
'7': 8, | |
'8': 12, | |
'9': 12, | |
}; | |
function nab_displayBets() { | |
statsWindow.document.body.innerHTML = ''; | |
const players = Object.keys( bets ).sort(); | |
for ( let playerIndex in players ) { | |
const player = players[ playerIndex ]; | |
statsWindow.document.write( '<p>' + player + ' bet on ' + bets[ player ] + '</p>' ); | |
} | |
} | |
function nab_resetScores() { | |
scores = {}; | |
nab_persistScores(); | |
nab_displayScores(); | |
} | |
function nab_loadScores() { | |
scores = undefined === localStorage.nab_scores ? {} : JSON.parse( localStorage.nab_scores ); | |
} | |
function nab_persistScores() { | |
localStorage.nab_scores = JSON.stringify( scores ); | |
} | |
function nab_displayScores() { | |
statsWindow.document.body.innerHTML = ''; | |
const players = Object.keys( scores ).sort( ( a, b ) => { return scores[ a ]- scores[ b ]; } ).reverse(); | |
for ( let playerIndex in players ) { | |
const player = players[ playerIndex ]; | |
statsWindow.document.write( '<p>' + scores[ player ] + ' : ' + player + '</p>' ); | |
} | |
} | |
function nab_processMessage( author, message ) { | |
if ( acceptingBets && betPattern.test( message ) ) { | |
bets[ author ] = message; | |
nab_displayBets(); | |
} | |
} | |
function nab_processMessages() { | |
const messages = Array.prototype.slice.call( document.getElementsByClassName('yt-live-chat-text-message-renderer') ).filter( el => el.id === 'message' ).map( el => el.innerText ); | |
const authors = Array.prototype.slice.call( document.getElementsByClassName('yt-live-chat-author-chip') ).filter( el => el.id === 'author-name' ).map( el => el.innerText ); | |
for ( var i = 0; i < messages.length; i++ ) { | |
nab_processMessage( authors[i], messages[i] ); | |
} | |
setTimeout( nab_processMessages, 5000 ); | |
} | |
function nab_bettingToggle( event ) { | |
if ( ! acceptingBets ) { | |
bets = {}; | |
acceptingBets = true; | |
} else { | |
acceptingBets = false; | |
} | |
if ( acceptingBets ) { | |
event.target.innerText = 'Stop betting'; | |
} else { | |
event.target.innerText = 'Start betting'; | |
} | |
console.log( event ); | |
} | |
function nab_awardScores( event ) { | |
if ( acceptingBets ) { | |
return; | |
} | |
const winner = prompt( 'Winner : ' ); | |
const runnerup = prompt( 'Runnerup :' ); | |
for ( let player in bets ) { | |
if ( bets[ player ] === winner ) { | |
scores[ player ] = ( scores[ player ] || 0 ) + horseScores[ winner ]; | |
} | |
if ( bets[ player ] === runnerup ) { | |
scores[ player ] = ( scores[ player ] || 0 ) + 1; | |
} | |
} | |
nab_persistScores(); | |
nab_displayScores(); | |
} | |
function nab_UIInject() { | |
console.log('ui'); | |
const buttons = document.getElementById( 'buttons' ); | |
if ( ! buttons ) { | |
return; | |
} | |
const bettingToggle = document.createElement( 'button' ); | |
bettingToggle.innerText = 'Start betting'; | |
bettingToggle.onclick = nab_bettingToggle; | |
const awardScoresButton = document.createElement( 'button' ); | |
awardScoresButton.innerText = 'Award scores'; | |
awardScoresButton.onclick = nab_awardScores; | |
const resetScoresButton = document.createElement( 'button' ); | |
resetScoresButton.innerText = 'RESET scores - be careful Nikki!'; | |
resetScoresButton.onclick = nab_resetScores; | |
buttons.parentNode.insertBefore( resetScoresButton, buttons.nextSibling ); | |
buttons.parentNode.insertBefore( document.createElement( 'p' ), buttons.nextSibling ); | |
buttons.parentNode.insertBefore( awardScoresButton, buttons.nextSibling ); | |
buttons.parentNode.insertBefore( bettingToggle, buttons.nextSibling ); | |
statsWindow = window.open( '', 'nab_stats' ); | |
statsWindow.document.write( '<html><head><style>' + styles + '</style></head><body></body></html>' ); | |
} | |
function nab_initGame() { | |
nab_loadScores(); | |
// inject UI elements | |
nab_UIInject(); | |
// start the message checking loop | |
setTimeout( nab_processMessages, 1000 ); | |
setTimeout( nab_displayScores, 1000 ); | |
} | |
(function() { | |
'use strict'; | |
if ( initComplete ) { | |
return; | |
} | |
initComplete = true; | |
nab_initGame(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment