Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Prevent your computer sleeping with just a webpage (nosleep.page)
252 points by bradleyjkemp on April 22, 2022 | hide | past | favorite | 130 comments
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...




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.


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.


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!


it's gotten to the point now where laptop manufacturers tell you not to put sleeping/hibernating laptop into a bag at all due to amount of heat they still generate even while sleeping [0]. Only those that are fully shut down should be placed in a bag (according to them).

[0] https://www.dell.com/community/XPS/FAQ-Modern-Standby/td-p/7...

Previous HN discussion: https://news.ycombinator.com/item?id=28639952


I miss hibernation. Real, write the contents of RAM to disk and shut-off completely type of hibernation.


I am confused. It's still around, right? It might be disabled by default on Windows, but you can always modify the power setting to enable hybernation? I set mine up and replaced "sleep" with "hibernate" in every setting I could find, like power button, lid close, idle timeout.


As I understand on current windows, the normal power off is actually hibernate.


Mine still does that.


The problem is with sleep (and the "active" sleep "S0 Low Power" that will check email) and Modern Standby, which uses software to choose when to Hibernate, so might not.

Hibernate S4 itself is fine, except the risk of physically triggering wakeup.


I think on Windows you can run powercfg /requests

I'm not sure about linux or Mac though


On MacOS, Activity Monitor has a "Preventing Sleep" column. From the command line, "pmset -g" or "pmset -g assertions" has useful information.


Now we need a menu appplet, cute icon, and name like Lullaby.


Another super useful powercfg flag is /lastwake to tell you why the computer woke up from sleep.

Has helped me identify flaky input peripherals or timers many times. Worst was an HP laptop with a wonky touch bar for media controls, that would frequently ghost press and wake the computer, even when in hibernate and with the lid closed.


I think at least if it's not the currently active tab it will still go to sleep, so it's not like a random Autoplay video in a background tab will ruin your day. At least that's what it seems like with Firefox on Linux.


I have my computers set up to lock with Meta + L on my Linux machine and Cmd + L on my MacBook Pro M1. And then I quickly and simply lock them with that whenever I get up from my seat.


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.


Firefox (at least in Windows) does allow sleep when there's a no-audio video.


This is true now. But for awhile did not and any video would keep the PC awake.

Ended up being a security risk at our company because someone in marketing didn’t know this and made a full screen video. Well, cleaning staff came in and almost all our PCs were on because lots of people use the company website.

Fixed that nonsense right away.


Cut the browser off from anything but the xorg and pulseaudio sockets (no dbus!). Then it can't place any inhibitors.


It used to be possible with mint.com. I actually reported that to them and they fixed it!


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'.


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.


Indeed, how awful.


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


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".


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?”


"This application prevented shutdown..."


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


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)


I used Caffeine for years and never really thought about it much, it's always been useful. But I installed Amphetamine lately on a new MBP and I've used the schedule and countdown modes multiple times now. It's a great tool and the added functionality is useful.


> (My first HN comment, via Orion Browser)

Did you manually type that out or is this some “Sent from my iPhone”-esque growth-hacking strategy by the Orion team?


Not from Orion, would be pretty invasive if it was (Orion dev here).


Thanks for clearing that up. I happened to get your beta invite the same day and was a bit over excited about it, I guess

Thanks for working on Orion!


And it does specifically need to be a third party application like Word. Built in apps like TextEdit support state restoration, so it will probably still reboot if you have unsaved content in TextEdit.


The windows equivalent to this is an unsaved text file in notepad.


PowerToys[0] has a utility for that. It stays in the corner of the taskbar.

[0] https://github.com/microsoft/PowerToys


This is good for users who don't have admin rights but want to keep online for employee monitoring purposes.


This burns me at least twice a week. I'll go to bed and 15 mins later I'll notice my computer is still on... it's always notepad.


An open iTerm terminal or Emacs window also fulfill this function for me (usually unintentionally)


You can also do this in the browser, using the beforeunload event

https://developer.mozilla.org/en-US/docs/Web/API/Window/befo...


I have the opposite problem. I want it to restart overnight and come back to find it blocked by my terminal being open.

Incidentally iTerm will block a restart too, and is useful to have open all the time anyway!


I’ve been using 10 hour videos of black screen with success.


Open a vim session in Terminal. Active processes prevent Terminal from quitting which prevents shutdown from proceeding


Why would Word do all that craziness instead of just saving a copy of the document like it did 20 years ago?


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.


Only if the web page is visible - page is active tab and browser is visible.

It's a useful feature, I use it to prevent a tablet displaying a dashboard turning off the screen due to lack of user action.


I have higher standards for my computer than sleeping with just a webpage.


I fully expect mine to f' everything.


I wouldn't let my computer sleep with this because it could get a virus and then before I know it I'm getting asked for money and if I don't pay up my nudes are all over my social media


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.


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.


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.


> 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.


The more unixy way is probably `timeout <seconds> caffeinate` :)


Or `caffeinate sleep <seconds>` which has the (only) added benefit that it comes pre-installed with MacOS


Yes but this got me thinking is it makes more sense to sleep after timing out the caffeination instead of keeping the sleep command running ;) that's enough Unix for the day haha


For some reason caffeinate doesn't keep my computer awake (newish macbook pro)


You might need `caffeinate -d` to prevent the display from sleeping.


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.


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.


> Worked like a charm when I needed to discharge a swelling battery.

I think you buried the lede there...


It just happens, apparently. Here's a decent rundown:

https://www.ifixit.com/Wiki/What_to_do_with_a_swollen_batter...


This is a standard functionality in kde plasma in the system tray.


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?


There's a checkbox "Manually block sleep and screen locking" and you just check that.


Where in the system tray is it? Do we need to click the battery icon or something? Maybe because I'm on a pretty old version (5.18.6), I don't see that checkbox anywhere I looked.


Yes, it's in the battery applet. I'm pretty sure it's been there for a long time, but I think it was named differently before ("disable power management" maybe?)


Ah, yes, there's an "Enable Power management" checkbox, by default checked. Thanks.


Ah yes, of course! How did i ever miss that? I see it now; thanks very much!


In the XFCE power menu, this is called "presentation mode", works just as well


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


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).


Using #000 would save the maximum power for any device with an OLED panels (like my laptop and my smart phone). Given that the idea is to keep a device on and some screens not being smart enough to understand this is an okay time to power down, #000 would be the only ethical color scheme to use. If you added a jitter to the placement of text over time, you could prevent possible burn in as well.


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 :)


"Show HN" is somewhat asking for feedback, is it not? Or am I mistaken in the format?

Yes, I can probably self-host something like this, change the color schema in a myriad of ways. But I thought the point of Show HN was to showcase and somewhat solicit feedback from the HN community.


> I can't minimize the browser (firefox) because then it apparently doesn't work.

Can't you have a separate, tiny window?


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.


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?


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...)


This is awesome my work computer has a stupid 5 minute setting until it sleeps that is locked.


If it's really locked down, you can find USB devices for a few bucks that emulate a mouse and move by a few pixels every minute. Foolproof for those companies with nonsensical settings or nonsensical spying.


I use AutoIt to move the cursor and it's free.


Deliberately bypassing that puts your employment at risk. There's a reason you're being forced to log in.


Maybe if you're slacking off, but if you're working on a project with 10-minute compile times, you can't really use the computer for anything else while it's doing that, and it's quite annoying if it decides to go to sleep in the middle of that because you haven't nudged the mouse recently enough.


Auto sleep and auto screen lock are two very different things.


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 :)


playing something in windows media player also keep it open, or running a powershell script that presses F13 or scroll lock also works.


> they're often overkill

Can you explain why? I never wants my Mac to sleep except when I close the lid. Amphetamine does the job; it starts at boot and I don’t have to care about it at all.


I'm not saying web applications are always better (of course they aren't), but they are better for when you either can't/don't want to install any application that gives access to everything on your computer. If a simple navigation to the URL can give you the same thing as installing a application (implicitly giving them access to all your files and more), wouldn't you normally prefer the first?

Again, web applications are not better for everything, but they are more safe for the average computer user, as they run in a restricted environment, compared to native applications.


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.


While the use case for recipes stying alive is very useful. Should this responsibility really be delegated to each website?

I didn’t even know this was a feature of my browser. Say for example I write a shopping list based on the recipe web page, then go to the supermarket to buy the ingredients, leaving the tab open because I will need the instructions tomorrow for the cooking. Now my computer will be forced on during the rest of day based on the choice of the website owner?! Wtf. What if I leave the tablet open after finishing cooking, Is there any timeout on this thing or will that differ depending on each website?

Honestly this feature is really badly thought through. It should be a feature of the OS and not up to random web sites without even asking for permission.

Putting my most cynical hat on you could imagine advertisers enabling this on every website so you are more likely to be exposed to their ads.


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


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/


Ah neat, yeah that's exactly what I need, thanks!


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>


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 :)


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).


I obviously missed the NoSleep.js at the end ... but then I wonder: what is then so special about this website?


It prevents your computer from sleeping with just a webpage. Why does it need to be more special than that?


I think karrussell is talking about the live demo linked from NoSleep.js' project page: https://richtr.github.io/NoSleep.js/example/


I meant that nosleep.page is basically the example page from NoSleep.js hosted on the internet...


The idea. A lot of company computers have autolock on, just an example.


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.


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.


FYI: There is also a Chrome extension doing similar thing: https://chrome.google.com/webstore/detail/keep-awake/bijihla...


On Mac I have aliases below:

  alias decafe='killall caffeinate'
  alias cafe='caffeinate -id'
which allows me to just '$ cafe' to keep it awake and '$ decafe' to stop it.


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.


Hey - developer of PowerToys Awake here. Would be happy to investigate. Feel free to open an issue in the GitHub repo (https://github.com/microsoft/PowerToys/issues) and I will look into it.


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.


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.


> let's make it harder for the people who have to clean up after security incidents

Uh oh, sounds like you might have to actually do your job too!


I'm so glad I left desktop support, but I still empathize. Running a network of hundreds of endpoints means you have to sacrifice laziness. That means you have to log in again after leaving your computer for five minutes. Because if you don't, the jackass who doesn't like you will wait to surf your computer to porn sites, or your coworkers will replace your wallpaper, or someone actually malicious will delete your project files and backups, or run a zero-day.

All sorts of shit like that happens. People are fundamentally lazy and stupid, which is why we can't have nice things, line workplaces where you don't have to worry about leaving your computer unlocked.

Stuff like this site that makes it easier to embrace the laziness and stupidity just make life worse.

How about no browsers? How about limiting network access to a strict whitelist approved by the boss and nothing else?

That's the type of shit that happens when you create tools like this. I 100% guarantee this site will be misused and the resulting corporate overcorrection will make hundreds or thousands of people's lives objectively worse.


_Your_ computer, maybe.

My workstation will certainly not be prevented from entering sleep by anything a webpage can do.


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



Why not just use nosleep.page?

https://nosleep.page


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!


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


Hey fair enough. Good to know. But you’re the one who asked ‘why’? I guess you thought that was rhetorical? :)

I was just going off the docs which specifically mention Chrome OS and Chrome-books. Personally, I’ve never really had a use case for something like this.

My only point is, I still applaud the author’s effort here, and it taught me something new about Web APIs. It’s interesting that you can do that at all with a web page!


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

`


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


Also would recommend this: https://docs.microsoft.com/en-us/windows/powertoys/awake

Disclaimer: I built the tool.


Dunno, been using Caffeine on GNOME for years, works great.


my windows computer has trouble sleeping. it always randomly wakes up :( quite annoying waste of power


Doesn’t seem to work on iOS 15.4.1


i might be pessimistic but this would be a great bait and switch for some cryptomining, lol.


Very cool, and looks slick!


I just turn zoom on


Does it really need to be 12 Kb of obfuscated JS+CSS?

That seems huge for the task.


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).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: