16

I am completely new to javascript programming and I have a question that I didn't manage to find an answer for anywhere.

I have recently put together a simple slideshow to view the photos remotely that I host on my home computer. This by itself works fine. The problem I run into is that when I'm viewing photos I don't interact with the hardware, which after some time causes the monitor to switch off. This is particularly annoying when watching photos on my mobile phone.

My question is: is there a way to prevent this from happening? I am thinking in the direction of faking a mouse or other event every time I refresh the photo, but I have no clue how to do that and if it is possible.

Any help is greatly appreciated!

gertjan
  • 843
  • 1
  • 8
  • 16
  • 5
    The question is interesting, but along with many limitations of JavaScript, it can't be done. Why? Security. As there are good people that want to use this code wisely, there are opposites that will harm. – JCOC611 Dec 21 '10 at 21:58
  • It's in the JavaScript manual next to the functions that give the user an electric shock if they try turning the computer off... – Pete Kirkham Dec 21 '10 at 22:42
  • Hm, not possible then, that's too bad. I suppose that only leaves John's solution and switch off power saving manually. :\ Well thanks everybody for answering so rapidly! – gertjan Dec 23 '10 at 08:22
  • 1
    This is not as unreasonable as you might expect; for example, there are already APIs for controlling screen brightness and orientation: https://developer.mozilla.org/en-US/docs/Web/API/Screen – Cauterite Dec 23 '16 at 09:52
  • Possible duplicate of https://stackoverflow.com/questions/11529247/in-html5-how-can-i-keep-an-android-device-s-screen-on – MadeInLagny Jan 02 '20 at 14:01

5 Answers5

12

Over the decade since this question was originally asked JavaScript has grown to provide much of the OS functionality (usually in a secure manner). The "wake lock" functionality is slowly being implemented. Currently there is a draft for the navigator.getWakeLock interface: https://www.w3.org/TR/wake-lock/#conformance

Chrome (https://developers.google.com/web/updates/2018/12/wakelock) and Mozilla (https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API) are considering it in various manners.

Krastanov
  • 6,479
  • 3
  • 29
  • 42
9

No. JavaScript on the browser cannot interact with the underlying system. Simulating keystrokes in the browser will not stop the screen saver from turning on. This is for security reasons, so that malicious code can't harm the system when you visit a web page.

Link on JavaScript Security

The modern JavaScript security model is based upon Java. In theory, downloaded scripts are run by default in a restricted “sandbox” environment that isolates them from the rest of the operating system. Scripts are permitted access only to data in the current document or closely related documents (generally those from the same site as the current document). No access is granted to the local file system, the memory space of other running programs, or the operating system’s networking layer. Containment of this kind is designed to prevent malfunctioning or malicious scripts from wreaking havoc in the user’s environment. The reality of the situation, however, is that often scripts are not contained as neatly as one would hope. There are numerous ways that a script can exercise power beyond what you might expect, both by design and by accident.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
4

No, JavaScript cannot affect hardware or operating system. Just turn off monitor power saving settings until you're done with the slideshow.

John Giotta
  • 16,432
  • 7
  • 52
  • 82
1

Yes, It's possible now :)

Just use NoSleep.js library: https://github.com/richtr/NoSleep.js

It's working form me with Reveal.js slides on my Android Tablet

Pedro Blaszczak
  • 336
  • 3
  • 6
  • 1
    Just a warning, this library makes Chrome use a huge amount of CPU. My fans where going crazy when I tried it: https://github.com/richtr/NoSleep.js/issues/66 – Evanss Apr 30 '20 at 15:42
  • For me NoSleep isn't working in desktop browsers. Did anybody get that to work ? – Gene Vincent Oct 05 '20 at 15:58
  • I use it on my android tablet with chrome browser, and yes it will hang the browser after few hours, so I reload the page from time to time. But so far didn't try to find better solution – Pedro Blaszczak Oct 06 '20 at 19:05
-1

You could do it with a console application written in c# that interacted with the os

since js is a client side browser language it can only interact with the browser/bowser

Kevin
  • 9