Detect wake from hibernation - javascript

Is there a way to detect a wake from hibernation? I am developing an extension that displays information when the user turns on the computer and logs in to the system. I can do that easily by setting the extension to run when the system starts and execute the codes instantly.
background.js
function displayInfo(){ /*...*/ }
displayInfo();
However, some users might not turn off their computer, rather, they would hibernate for faster wake time. Hibernate should still be considered that the user is "turning on" the computer, but currently I have no way of detecting that.
Is there anyway I can achieve that?

No "perfect/foolproof" method, but some an idea:
Record the system time every 5 minutes or so. If the current time is much larger than the expected time, assume it slept/hibernated.

Related

Log user activity on various platforms

I have an Electron app and I need to log system-wide user activity (e.g. mouse-clicks, keystrokes). Basically, i need to detect if user is AFK if he did nothing in a 5-minute interval, so i can stop the timer inside the app. I'm pretty sure I need to do it with external scripts for each platform (osx, linux, ms). Can you guys suggest me any finished tools or os-level APIs to do it (doesn't matter for which plat) if you know some, or just tell me how you would do it / did it some time.
For all platform monitoring and analysis use GrayLog
https://www.graylog.org/

How to run script in Firefox when a tab was closed?

At our company we are using a web application with shared licenses. Unfortunately if someone simply closes the tab the application is running in it wont release the license lock. I am wondering whether it is possible to run/trigger a scipt when a Firefox tab is closed, so I could automatically release the licenses? I think greasemonkey might be able to do this, but I haven't found a solution yet.
There is both window.onbeforeunload and window.onunload, which are used differently depending on the browser. You can assing them either by setting the window properties to functions, or using the .addEventListener:
window.onbeforeunload = function(){
// Do something
}
// OR
window.addEventListener("beforeunload", function(e){
// Do something
}, false);
Usually, onbeforeunload is used if you need to stop the user from leaving the page (ex. the user is working on some unsaved data, so he/she should save before leaving).
You can try to release locks in unload events, as Bcfm suggested in his answer, but what if browser or computer simply crashes? Or script takes too long to execute and gets killed by browser anyway?
Another approach would be to make the site constantly ping license server (i.e. every 10 seconds) so that lock is hold until there is no ping for proportional amount of time (i.e. 30 seconds). This way the license lock is freed in all cases.
Of course this may be not relevant to your scenario, it's just a suggestion.

Detect if monitor is powered on from IE Kiosk mode

I developed a web app to display a slideshow, and want to display it on my secondary monitor (Connected via HDMI) with IE's Kiosk mode on Windows 10. Because of CPU and other resources on the shared server, I want to pause the slideshow when the monitor is powered off. (And therefore nobody is seeing it)
Is there a way to detect connected displays from Internet Explorer? Since this is a one-pc kiosk setup, add-ons, etc. are accepted. Triggering javascript/jquery events would be ideal. Thank you!
No, there is no reliable way to detect if a second monitor is physically switched off but still connected via the cable.
I have to ask though: why do you need to physically switch the second monitor off?
As an alternative could you not:
Have the slideshow stop after a timed duration unless it receives an input?
Have the slideshow only on display at certain times of the day?
Accept events from, say, a node server to control when to and not show the slideshow?
Having said that these threads could provided you, albeit unreliably apparently, what you need:
Is there any way to detect the monitor state in Windows (on or off)?
Monitoring a displays state in python?
You can't do in javascript. Why not try some asp component.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd162617%28v=vs.85%29.aspx
You could potentially write a command line program that sits on a particular port, continuously checks for that locally and then use HTML5 WebSockets in IE to communicate with it?
i.e. C# PowerModeChangedEvent
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(
SystemEvents_PowerModeChanged
);
I don't think so....
CPU cycles are paused when the client computer is put into Sleep mode. (win+L)
Start>Control Panel>Power
configures how the monitor(s) behave when the client is powered down or put to sleep mode.
the screen object in js returns the metic values (height/width) of the screen object but not its powered state.
the impact of wasted CPU cycles on a powered down secondary monitor should be un-noticable....
probably you have not selected the option to "Use software rendering instead of GPU rendering" on the Advance tab of internet options....
You will notice that your CPU on your desktop will throttle up and the cooling fan will race if you haven't set the above setting when running graphic intensive web pages or canvas scripts.

how to get the real time in windows

I was build a Web system which is running on Windows and Linux too. It's running through Xampp. In this situation who using this System changing the time simply and add missed data into the system. So i want to track those. For that i was plan to get the real time using a time server. But if that PC doesn't have Internet that will gonna be a problem to get real time. So i need something execute behind of the System which doesn't interact with Windows Time. So i can get the real time. IS there anyway to do it using VB OR Any scripting language ?
If the system in question doesn't have an internet connection, I don't believe there's any way to get the "real" time, just the system time.
So... you want to get an accurate count of 'real time', not system time, without being connected to the internet?
The only thing I can think of is a radio frequency controlled device to correct the time - e.g. http://www.galsys.co.uk/time-receivers/
Normally, if it was connected to the internet, you could get it to read the time off of the internet - e.g. from here http://tycho.usno.navy.mil/timer.html - that could be done on a Linux system by using a bash script, curl or wget, and the date command.

Implement locking & unlocking a document while editing

Here's the situation:
I have a web-based ticket application, multiple users.
One problem that might occur (and does happen in the old version I'm replacing) is that user1 opens a ticket, edits it, and saves it. But while he was editing it, user2 also opened and saved the ticked. The changes user2 made will be lost/overwritten by user1.
To prevent this I implemented a locking mechanism, it's fairly simply:
On opening a ticket the PHP script checks for existing locks.
If it doesn't find any, it locks & opens the document.
In JS, setTimeout() and an XmlHttpRequest call to unlocks the ticket after 10 minutes (works w/o problems).
I also set an unload event to unlock the ticket when closing/moving away from the window/tab
The problem sits in step 4: The unload event (& it's friend beforeunload) just doesn't work well enough to implement this reliably (for this feature to have any serious meaning, it needs to be reliable), many browsers don't always fire it when I would like it to be fired (Like pressing back button, hitting F5, closing tab, etc. This varies per browser)
The only alternative I can come up with is using a setTimeout() and XmlHttpRequest() call to a php script to tell it the page is still open. If this "heartbeat" monitor fails we assume the user moved away from the ticket and unlock the document.
This seems horribly inefficient to me and quickly leads to many requests to the server with even a few users.
Anyone got a better idea on how to handle this?
It needs to work in IE8+ and other modern browsers (ideally, Firefox, Webkit, Opera). I don't care about IE6/IE7, our organization doesn't use those).
Using heartbeat pings via XHR is the way to go. Depending on the use case you might also want to send them after the user stopped typing in a field instead of every x seconds - this would ensure the page being kept open but inactive would not keep it locked.
If you send those XHRs after the user stopped typing, use one of the keydown/up/press events and a debounce / throttle script to send the request only when the user stops typing for e.g. 5 seconds and one every x seconds (in case it's likely enough the user will be typing for a long time).
Maybe it's not the best solution, but it's worth looking into it : websockets.
You could establish a connection with the server at page load and when the connection fails (ie the client does not respond to the ping), you can unlock the ticket.
Using something like socket.io ensures you that this procedure will work even on ie8.
The main advantage is that you do not send a request every n seconds, but the server sends you a ping every n seconds and you don't have to care about unload/beforeunload events. If the client doesn't respond to the ping, unlock the ticket.
The main disadvantage is that you need a server to handle all your websocket connections, which can be done in almost any server-side language, but it can be a bit harder than a simple web-service (in case of xhr polling)
Implementing ajax heartbeats or unload handlers to unlock the document automatically is tricky.
You problem is that even if you have support for beforeunload in all browsers that you target, it still might not be called if the browser crashes or the user falls asleep.
Look at how webdav works. You explicitly aquire a lock before you start edit, then you save and release the lock explicitly.
Other users can see who has acquired a lock and admins can release locks that has been left behind by accident.

Categories

Resources