JSFiddle - React, Tailwind, and code Playground
by Thomas Aylott
HTML
<style>
html{background:#333}
</style>
<!-- <script src="lib/monkey keycodes.js"></script> -->
<script>
var $DeviceSerialNumber = '*'
var hook = {emit:function(){}}
if (top.commands && top.commands.hook) initWithHook(top.commands.hook)
function initWithHook(_hook){
hook = _hook
hook.on('android-devices::connections::changed', function(connections){
var html = ''
connections = connections.split(',')
connections.unshift('*')
for (var index=-1, serial; serial = connections[++index];) {
html += '<option value=' + serial + (serial == $DeviceSerialNumber ?' selected':'') +'>'
+ serial
+ '</option>'
}
$DeviceSerialNumberSelect.innerHTML = html
})
}
monkey.shouldRecord = true
monkey.shouldDoItLive = true
function monkey(command){
if (Array.isArray(command)) command = command.join('\n');
weakMonkey(command)
if (monkey.shouldDoItLive) strongMonkey(command)
}
function strongMonkey(command){
hook.emit('monkey::' + $DeviceSerialNumber + '::do', command);
}
weakMonkey.MAX_TIMEOUT = 1000
function weakMonkey(command){
if (!monkey.shouldRecord) return;
var sleepTime = 0
if (weakMonkey._timeStamp) sleepTime = Date.now() - weakMonkey._timeStamp
weakMonkey._timeStamp = Date.now()
if (sleepTime > weakMonkey.MAX_TIMEOUT) sleepTime = weakMonkey.MAX_TIMEOUT
if (sleepTime) commandText.value += 'sleep ' + sleepTime + '\n';
commandText.value += command + '\n'
}
function reverseMonkey(){
commandText.value = commandText.value.split('\n').reverse().join('\n').replace(/(down|up)\b/gi, function(string){
if (string == 'up') return 'down'
if (string == 'down') return 'up'
if (string == 'UP') return 'DOWN'
if (string == 'DOWN') return 'UP'
})
}
function fasterMonkey(){
commandText.value = commandText.value.replace(/\b(sleep\s+)(\d+(?:\.\d+)?)/g, function(match, text, sleepNumber){
return text + (+sleepNumber / 2)
})
}
function slowerMonkey(){
commandText.value =...