Last active
August 29, 2015 14:05
-
-
Save jsyang/055ca00121eabb1ec504 to your computer and use it in GitHub Desktop.
ColorRunPlBot.js
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
javascript:(function(b){function a(d){return d.style.backgroundColor.match(/[\d]+/g).reduce(function(g,f){return g- -f},0)}function c(j){var l=document.querySelector("#color-1");var h=document.querySelector("#color-2");var f=a.bind(null,l);var g=a.bind(null,h);var k=new MouseEvent("click",{view:window});this.react=function(){if(f()>g()){if(this.shouldQuit){h.dispatchEvent(k)}else{l.dispatchEvent(k)}}else{if(this.shouldQuit){l.dispatchEvent(k)}else{h.dispatchEvent(k)}}};this.shouldQuit=false;var d=new MutationObserver(function(e){if(j>0){this.onStyleMutation(e[0]);j--}else{this.stopGame(e[0])}}.bind(this));d.observe(l,{attributes:true});this.injectStyle(".color { opacity: 0.1 };");this.stopGame=function(i){this.shouldQuit=true;d.disconnect();this.injectStyle(".color { opacity: 1 };");this.onStyleMutation(i)}.bind(this)}c.prototype.injectStyle=function(f){var d=document.createElement("style");d.textContent=f;document.body.appendChild(d)};c.prototype.onStyleMutation=function(d){if(d.attributeName==="style"){window.requestAnimationFrame(this.react.bind(this))}};b.BOT=c})(window);new BOT(1000); |
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
/** | |
* Bot for ColorRun.pl | |
* jsyang.ca@gmail.com | |
* | |
* Bored on Aug 30, 2014. | |
*/ | |
(function(namespace){ | |
function brightnessMetric(el) { | |
return (el.style.backgroundColor | |
.match(/[\d]+/g) | |
.reduce(function(prev, curr) { return prev - (-curr); }, 0) | |
); | |
}; | |
function ColorRunPlBot(reactionsRemaining) { | |
var color1 = document.querySelector('#color-1'); | |
var color2 = document.querySelector('#color-2'); | |
var getLeftBrightness = brightnessMetric.bind(null, color1); | |
var getRightBrightness = brightnessMetric.bind(null, color2); | |
var clickEvent = new MouseEvent('click', { | |
'view': window | |
}); | |
// Reaction to color change. | |
this.react = function() { | |
if(getLeftBrightness() > getRightBrightness()){ | |
if(this.shouldQuit) { | |
color2.dispatchEvent(clickEvent); | |
} else { | |
color1.dispatchEvent(clickEvent); | |
} | |
} else { | |
if(this.shouldQuit) { | |
color1.dispatchEvent(clickEvent); | |
} else { | |
color2.dispatchEvent(clickEvent); | |
} | |
} | |
}; | |
this.shouldQuit = false; | |
// Observe color changes and react to them. | |
var styleObserver = new MutationObserver(function(mutations){ | |
if(reactionsRemaining > 0) { | |
this.onStyleMutation(mutations[0]); | |
reactionsRemaining--; | |
} else { | |
this.stopGame(mutations[0]); | |
} | |
}.bind(this)); | |
styleObserver.observe(color1, { attributes: true }); | |
// Inject style to stop seisures. | |
this.injectStyle('.color { opacity: 0.1 };'); | |
// Programmatically end the game during gameplay. | |
this.stopGame = function(mutationRecord) { | |
this.shouldQuit = true; | |
styleObserver.disconnect(); | |
this.injectStyle('.color { opacity: 1 };'); | |
this.onStyleMutation(mutationRecord); | |
}.bind(this); | |
} | |
ColorRunPlBot.prototype.injectStyle = function(styleText) { | |
var injectedStyle = document.createElement('style'); | |
injectedStyle.textContent = styleText; | |
document.body.appendChild(injectedStyle); | |
}; | |
ColorRunPlBot.prototype.onStyleMutation = function(mutationRecord) { | |
if(mutationRecord.attributeName === 'style') { | |
window.requestAnimationFrame(this.react.bind(this)); | |
} | |
}; | |
namespace.BOT = ColorRunPlBot; | |
})(window); | |
// Click play button to begin. | |
new BOT(1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment