Last active
March 18, 2018 00:41
-
-
Save drewB/38c61541a0575a88c32753409d648d1a to your computer and use it in GitHub Desktop.
phantomjs script for arming Alarm.com panel
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
var page; | |
page = require('webpage').create(); | |
page.viewportSize = { width: 320, height: 780 }; | |
phantom.waitFor = function(callback) { | |
do { | |
// Clear the event queue while waiting. | |
// This can be accomplished using page.sendEvent() | |
this.page.sendEvent('mousemove'); | |
} while (!callback()); | |
} | |
phantom.waitForSomeTime = function() { | |
//super hacky way to "sleep" while waiting for page to load | |
count = 0 | |
do { | |
// Clear the event queue while waiting. | |
// This can be accomplished using page.sendEvent() | |
this.page.sendEvent('mousemove'); | |
count += 1 | |
} while (count < 10000); | |
} | |
console.log('opening page...'); | |
page.open('https://www.alarm.com/login.aspx', function(status) { | |
if (status === 'success') { | |
console.log('page open...'); | |
page.evaluate(function() { | |
var config; | |
config = { | |
username: 'username', | |
password: 'password' | |
}; | |
document.querySelector('input[name="ctl00$ContentPlaceHolder1$loginform$txtUserName"]').value = config.username; | |
document.querySelector('input[name="txtPassword"]').value = config.password; | |
document.querySelector('input[name="ctl00$ContentPlaceHolder1$loginform$signInButton"]').click(); | |
}); | |
console.log('filled form and clicked login'); | |
phantom.waitFor(function() { | |
return page.evaluate(function() { | |
return document.querySelectorAll('.disarmed.icon-circle').length > 0; | |
}) | |
}) | |
console.log("Signed in and ready to change arm state"); | |
phantom.waitForSomeTime(); // for some reason just waiting '.disarmed.icon-circle' doesn't seem to wait long enough | |
page.evaluate(function() {document.querySelector('.disarmed.icon-circle').click()}); | |
console.log("Clicked Panel"); | |
phantom.waitForSomeTime(); // for some reason waiting for the checkboxes doesn't wait long enough | |
page.evaluate(function() {document.querySelectorAll('.disarmed [role="checkbox"]')[1].click()}); //Silent Arming | |
page.evaluate(function() {document.querySelector('.disarmed .armed-stay').click()}); //Arm Stay | |
console.log("Arming...."); | |
phantom.waitFor(function() { | |
return page.evaluate(function() { | |
return document.querySelector('.card-info.armed-stay h4 span').textContent == "Armed Stay"; | |
}) | |
}) | |
console.log("Alarm is now armed for stay.") | |
phantom.exit(); | |
} else { | |
console.log('Error loading page.'); | |
phantom.exit(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alarm.com just changed their site again and phantomjs doesn't work because of unsupported ES6 promises. I rewrote this in chrome-headless https://gist.github.com/drewB/7330b172d00364e067a91a95cf927695