[HN Gopher] Show HN: Prevent your computer sleeping with just a ... ___________________________________________________________________ Show HN: Prevent your computer sleeping with just a webpage There's often times I want to prevent a computer/laptop/VM from sleeping and while, yes, there's various Caffeine/Amphetamine apps they're often overkill. Instead, this small (12Kb) page does the job and only needs a web browser. It's just a very simple usage of a web api normally used for things like video players: https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake... Author : bradleyjkemp Score : 131 points Date : 2022-04-22 16:12 UTC (6 hours ago) (HTM) web link (nosleep.page) (TXT) w3m dump (nosleep.page) | devit wrote: | _Your_ computer, maybe. | | My workstation will certainly not be prevented from entering | sleep by anything a webpage can do. | Hamcha wrote: | That's pretty handy! I remember a coworker asking me for | something similar years ago and me just making something on the | fly in C++ (a glorified wrapper over "SetThreadExecutionState"): | https://gist.github.com/Hamcha/3a7b3e7518e13d1fba7e | | I wonder if that page would even work, his work PC at the time | (late 2015) was a dual-socket Pentium III which ran surprisingly | OK given the already 15+ years of age, but lacked in software | support as apps like Skype and Chrome at one point started | shipping binaries that required.. SSE2 | eigenvalue wrote: | If you absolutely want to prevent a Mac from not only going to | sleep, but restarting for any reason at all (say, to install | important security updates, which are usually forced on the user | with no choice), there aren't many good ways. The only thing I've | found that really works reliably is to open MS Word, create a new | document, add some random characters to it, and then leave | without ever saving. Word will prevent the OS from doing anything | that would cause you to lose that unsaved document. I'm sure the | MS engineers dug deep into the kernel to find something | foolproof. It's a bit annoying that Apple doesn't give you a | little switch like that which you could toggle to say "don't | restart for any reason at all". | vulcan01 wrote: | Amphetamine[0] works pretty well for me. They also have an | extension (a kext, I think) that gives it more reliability. I | don't know how reliable it is without that extension though. | | [0]: | https://apps.apple.com/us/app/amphetamine/id937984704?mt=12 | adamomada wrote: | I've been using the more bare-bones KeepingYouAwake[0] just | because I came across Caffeine first and this was suggested | as the continuation of that simple utility. The icon makes it | obvious whether it is enabled or disabled | | Edit: I see now that Amphetamine has the option of a similar- | looking icon to make it more obvious whether it is enabled or | not | | [0] https://github.com/newmarcel/KeepingYouAwake | | (My first HN comment, via Orion Browser) | Destiner wrote: | I've been using 10 hour videos of black screen with success. | StrangeSound wrote: | You can also do this in the browser, using the beforeunload | event | | https://developer.mozilla.org/en-US/docs/Web/API/Window/befo... | huseyinkeles wrote: | Same thing happens with JetBrains IDEs. I usually set my Mac to | update after midnight but usually find it got stuck with the | IDE asking "are you sure you want to quit?" | keyle wrote: | "This application prevented shutdown..." | boo-ga-ga wrote: | Very cool, and looks slick! | taftster wrote: | So I like this and I'm trying it out, it seemingly works OK. | | However, the GREEN background is very annoying. I don't need the | whole webpage to be green for me to know it's on and working. I | can't minimize the browser (firefox) because then it apparently | doesn't work. So I'm left with a very big green screen sitting in | the corner of my eye. Wondering too the effect of that in terms | of screen color burnout (if that's even a thing anymore). | | I would definitely recommend to tone down the color scheme. Stay | away from green. Just go with grey and darker grey or black for | your on/off schemes, including inside of the slider button. | | My use case is that I have the "nosleep" computer on the side of | my desk. It catches IM/chat messages (from Teams, etc.) and email | (from Outlook). I don't like my primary development machine | bothered by these notifications, but I do like having that | machine "awake" and alert to chat/email notifications (and I | can't (or shouldn't) change the default policies on that | machine). | JadeNB wrote: | > I can't minimize the browser (firefox) because then it | apparently doesn't work. | | Can't you have a separate, tiny window? | throwanem wrote: | MDN (linked from the page under discussion) says Firefox | doesn't expose this API, so I feel like window configuration | may be a side issue here. | ghotli wrote: | I'm confused, kinda. If you want it to be a different color | just alter the color? | | Lots of ways to attack that, host it yourself, change it with | chrome dev tools, a bookmarklet that alters the css, user | scripts for css overrides like greasemonkey popularized back in | the early days of the web. Hope this helps you get the color | you'd like :) | achairapart wrote: | Simple and potentially useful but I think that nosleep.js itself | uses the Screen Wake Lock API when available, so maybe it's still | overkill using it just as a fallback and writing other code on | top of it. | | Besides, it's a shame that iOS still doesn't support this API. | The video-hack has too many drawbacks and this feature would be | very useful for a certain kind of PWAs. | justusthane wrote: | This looks cool! It would be nice if there was a little more info | on the page about how it works and any potential limitations. For | example, does the page have to remain in the foreground? And if | it does work in the background, will it still work if the browser | unloads the page from memory? | bradleyjkemp wrote: | Oh, 100% I need some more docs on the page: it's definitely not | foolproof. | | From my testing, it works even if the window is in the | background somewhere but generally it stops working if you | switch to a different tab within the same window. | | You should get a popup though if you do something that causes | the page to lose its Wake Lock (which works by listening to the | release event: https://developer.mozilla.org/en- | US/docs/Web/API/Screen_Wake...) | bogota wrote: | This is awesome my work computer has a stupid 5 minute setting | until it sleeps that is locked. | robbedpeter wrote: | Deliberately bypassing that puts your employment at risk. | There's a reason you're being forced to log in. | TobyTheDog123 wrote: | If an employer is going to fire you for running caffeinate | while presumably working from home (if you're leaving your | laptop alone and unlocked in a public space God help you), | maybe it's time to look for a new job :) | oneweekwonder wrote: | playing something in windows media player also keep it open, or | running a powershell script that presses F13 or scroll lock | also works. | addsubtract wrote: | My work resets my Mac sleep timer settings on reboot or update. | This is very annoying for maintaining persistent connections to | servers, such as my development machine. I wrote a simple Python | script to move my mouse pointer by 1-5 pixels at random | intervals. Works great, and it always looks like I'm online for | prying eyes in Slack. | dolmen wrote: | Does it really need to be 12 Kb of obfuscated JS+CSS? | | That seems huge for the task. | GlitchMr wrote: | Most of the JavaScript is two empty videos encoded in WebM or | MP4, encoded using Base64 to be stored in plain text, and | picked depending on what the web browser supports. Considering | I doubt that video formats are designed with empty videos in | mind, this is probably not too bad. As for why videos are here, | they are a fallback in case web browser doesn't provide Screen | Wake Lock API (in particular, Safari and Firefox don't). | karussell wrote: | As Firefox (& I think Safari too) does not support the | ScreenWakeLock API are you using NoSleep.js? | https://github.com/richtr/NoSleep.js | | This library implements a hack and plays a very tiny video in a | loop too. This should even work on mobile devices and (according | to my personal&tiny tests) is not that inefficient as it sounds | :) | pdxandi wrote: | Are we seeing different sites when you click on the link? | Because that is literally all the landing page says. | This tiny site uses the Screen Wake Lock API to prevent your | device from sleeping. Where this API isn't | supported, an empty silent video is played to emulate this | behaviour (using NoSleep.js). | karussell wrote: | I obviously missed the NoSleep.js at the end ... but then I | wonder: what is then so special about this website? | _trampeltier wrote: | The idea. A lot of company computers have autolock on, just | an example. | spiderice wrote: | It prevents your computer from sleeping with just a | webpage. Why does it need to be more special than that? | rzzzt wrote: | I think karrussell is talking about the live demo linked | from NoSleep.js' project page: | https://richtr.github.io/NoSleep.js/example/ | karussell wrote: | I meant that nosleep.page is basically the example page | from NoSleep.js hosted on the internet... | [deleted] | mrep wrote: | Why not just use keep awake? | | https://chrome.google.com/webstore/detail/keep-awake/bijihla... | tailspin2019 wrote: | Why not just use nosleep.page? | | https://nosleep.page | 40four wrote: | I can think of a few reasons. Looking at that page, the | extension seems to only be compatible with Crome OS. A pretty | limited audience can use that. | | Also, I assume this is a pet project of the author. They had a | need, and found a way to solve it. They probably had fun, and | learned some things along the way. Often, that's kind of the | whole point of a weekend project. | | Even if there is another established solution, 'Why not just | ... some other thing' isn't great feedback. Discovering | people's ideas and projects is one of the beautiful things | about HN. | | This is a perfect example. I didn't even know the Screen Wake | Lock API existed, so I learned something new! | mrep wrote: | It has worked on every OS that has chrome that I have used | and takes like 30 seconds to install. It is easier to click a | chrome extension to stay awake than go to a webpage in my | opinion. | | You do you though | causality0 wrote: | Interesting. The various customizations Asus makes with its | gaming software on my laptop somehow stops the PowerToys keep- | awake utility from functioning as intended. I wonder if this page | will do the trick. | flakiness wrote: | FYI: There is also a Chrome extension doing similar thing: | https://chrome.google.com/webstore/detail/keep-awake/bijihla... | singlow wrote: | So i get annoyed that my computer doesn't go to sleep sometimes | because some website is playing a video. Is there a simple way to | tell Linux/Gnome/Chrome to ignore videos and go to sleep anyway? | I don't watch long videos on this computer so I hate to come back | into my office and find my screen never locked because some | website played a video i wasn't even watching. | bryans wrote: | I think the best option would be an extension that disables | autoplay across the board. There are many out there, though all | flawed in their own way. The least worst for Chrome seems to be | AutoplayStopper. | rsoto wrote: | Additionally, something to tell me whether my PC will go to | sleep or not if left alone. OP's problem happens to me every so | often that I need to make sure the PC is indeed going to sleep | before heading out. | Firmwarrior wrote: | I think on Windows you can run powercfg /requests | | I'm not sure about linux or Mac though | monkeybutton wrote: | Nothing like packing up your laptop at the end of the day, | clicking "sleep" and wondering if it actually will cooperate | or if you have open it back up and close random tabs and | programs till it does,then waiting for the fan to audibly | stop before putting it into a bag! | zinekeller wrote: | Firefox (at least in Windows) does allow sleep when there's a | no-audio video. | ognarb wrote: | This is a standard functionality in kde plasma in the system | tray. | mxuribe wrote: | Sorry, i'm a kde user but not sure how this would be done...? | Would you kindly provide details, or a link to a how to? | meibo wrote: | In the XFCE power menu, this is called "presentation mode", | works just as well | Minor49er wrote: | Using this on Firefox for Android causes the application icon and | header bar notifications to blink as long as it's enabled. I | don't think there's anything the developers could do about that | though | serf wrote: | nosleep.js brings firefox from ~3% cpu usage to ~25-30% cpu | usage. (it's playing an invisible video) | | that's a pretty steep cost to pay to simply keep a computer awake | because the browser it's using doesn't support a 'stay-awake | api'. | pessimizer wrote: | Or rather because the OS you're using doesn't think you're | responsible enough to have that kind of control over your own | computer. | | edit: Ah, I misunderstood. This is for your highly locked down | work computer. | yyahh wrote: | Indeed, how awful. | gabrielsroka wrote: | Thanks for sharing this. | | I've seen it on some recipe websites recently and was wondering | how it worked. | | I would recommend making the links (eg to the Mozilla page) use a | different color or underlined. | bradleyjkemp wrote: | Yup, that's my bad CSS I'm afraid. https://bulma.io explicitly | resets the color of <a> tags inside a hero, so I need to figure | out how to stop/override that | pkage wrote: | The "bulma" way would be to add the `has-text-link` class to | the <a> tag [0], which has the benefit of matching your | styles as well. | | [0] https://bulma.io/documentation/helpers/color-helpers/ | bradleyjkemp wrote: | Ah neat, yeah that's exactly what I need, thanks! | gabrielsroka wrote: | One or the other or both of these <a | href="https://developer.mozilla.org/en- | US/docs/Web/API/Screen_Wake_Lock_API" style="text-decoration: | underline; color: blue;">Screen Wake Lock API</a> | catsarebetter wrote: | I just turn zoom on | hoistbypetard wrote: | Can this web page keep my computer awake without me explicitly | giving it permission? I don't really like that, and would like to | tell my browser not to allow it. | velcrovan wrote: | On macOS just run `caffeinate` from the terminal (or `man | caffeinate` for more info on the options). | | I know OP copped to "various Caffeine/Amphetamine apps" but said | they're often "overkill". In this case I think a system-provided | command is less overkill than a web page that either uses a | browser API or plays an empty video. | [deleted] | reidjs wrote: | For some reason caffeinate doesn't keep my computer awake | (newish macbook pro) | Tao331 wrote: | I've been using a Zoom meeting of just myself, locked, audio | and camera off. Worked like a charm when I needed to | discharge a swelling battery. | tomstuart wrote: | You might need `caffeinate -d` to prevent the display from | sleeping. | jen729w wrote: | While this does work, I agree with OP here. The man page | for caffeinate says that it prevents system sleep and, as | far as I can tell, it does not do that on my M1 MBP using | Monterey. | | I do a `caffeinate -d` and then throw my mouse to a hot | corner to activate the screen saver. | SteveDR wrote: | Does this stay on indefinitely? I don't like using the command | line to enable settings that I only need for an hour or so. I | often forget to turn them off. | | I like that OP's site turns keeps my machine awake while it's | open, and I can just close the tab when I want my machine to | behave normally. | latexr wrote: | > Does this stay on indefinitely? | | If you want. If you don't, use the `-t` flag for it to turn | off after a set number of seconds or `-w` to do so after a | specific process ends. | lambdaba wrote: | The more unixy way is probably `timeout <seconds> | caffeinate` :) | bajsejohannes wrote: | Or `caffeinate sleep <seconds>` which has the (only) | added benefit that it comes pre-installed with MacOS | jcynix wrote: | I regularly use "caffeinate rsync ..." to make sure that | syncing finishes before the Macbook is allowed to sleep. So | caffeinate just prevents sleeping until rsync terminates. | | You can tell caffeinate to watch for other processes too, to | make sure a browser thread doing a download will finished | uninterrupted before sleeping. | alloai wrote: | with text file saved as .bat file ` @echo off | | powercfg.exe -x -monitor-timeout-ac 5 powercfg.exe -x -disk- | timeout-ac 180 powercfg.exe -x -standby-timeout-ac 180 | powercfg.exe -x -hibernate-timeout-ac 999 | | ` | gabrielsroka wrote: | https://news.ycombinator.com/formatdoc @echo | off powercfg.exe -x -monitor-timeout-ac 5 | powercfg.exe -x -disk-timeout-ac 180 powercfg.exe -x | -standby-timeout-ac 180 powercfg.exe -x -hibernate- | timeout-ac 999 | robbedpeter wrote: | Yeah, let's make it harder for the people who have to clean up | after security incidents. The automatic logout policies are there | for a reason. Bypassing that means one day, somebody's going to | forget and leave an important workstation open for a passersby or | malicious actor. | | If it's _that_ inconvenient, talk with your IT people and make a | case for changing it. If you use stuff like this, I hope you get | shitcanned. | A4ET8a8uTh0 wrote: | Eh, I hear you and I understand the concern, but.. some very | corporate jobs have very corporate ITs with their hands tied by | policy that makes exceptions truly exceptional. | | I was going to provide a more recent anecdote, but decided | against since it was a little too specific. | viggity wrote: | this is baller. I'd love an option that was like "keep awake for | [hours] [minutes]"... or "keep awake until [time]" so that it | could look like i was online until say 6:18pm ___________________________________________________________________ (page generated 2022-04-22 23:00 UTC)