Advertisement
StavenCross

Untitled

Jan 6th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var interval = 15000;
  2. $(document).ready(function()
  3. {    
  4.     var now = Date.now(); //get current miliseconds since jan 1970
  5.     window.localStorage.setItem('LastKeepAlive',now); //store it in localstorage so it persists across reloads
  6.     setInterval(logToSystem, interval);
  7. });
  8.  
  9. function browserCheckForConnectionToAHK() {
  10.     var lastCheck = window.localStorage.getItem('LastKeepAlive'); //bring in the var from localStorage
  11.     var now = Date.now(); //get time in MS
  12. if(now - lastCheck > (interval*2)) { //if the last time we updated was more than 2x the interval, we assume we're disconnected
  13.         alert('Disconnected ' + ((now-lastCheck)/1000) + ' seconds ago from page. Please reload the app');
  14.     } else {
  15. //might want to log this to the system some how so we can identify what they were doing to get disconnected.
  16.     }
  17.     AHK('confirmBrowserConnection'); //Now we'll reach out to AHK and try to confirm we're still connected
  18. }
  19.  
  20. function ahkStillConnected() { //called by AHK
  21.     var now = Date.now(); //get current time
  22.     window.localStorage.setItem('LastKeepAlive',now); //update our localStorage with this new time, so browserCheckForConnectionToAHK has updated time to compare
  23.     var response = "success"; //we need to return a value to AHK to prevent a sleep loop.
  24.     return response
  25. }
  26.  
  27.  
  28. /*********************** AHK **********************************/
  29. confirmBrowserConnection() {   
  30.     pageInst:=reconnectToPage() ;connect to the existing instance of chrome page
  31.     response:=pageInst.Evaluate("ahkStillConnected()").value ;eval the func, grab the return value.
  32.     wb.Disconnect() ;disconnect from page - if you don't do this, the script will die.
  33. }
  34.  
  35. reconnectToPage() {
  36.     pageInst:=Chrome.GetPage(,,,,"event_callBack") ;connect to the first page in Chrome index. As long as yuou have a single page app this should work.
  37.     return pageInst
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement