Settings

Theme

Show HN: Prevent your computer sleeping with just a webpage

nosleep.page

252 points by bradleyjkemp 4 years ago · 136 comments (134 loaded) · 1 min read

Reader

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

singlow 4 years ago

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.

  • rsoto 4 years ago

    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.

    • monkeybutton 4 years ago

      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!

      • shbooms 4 years ago

        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

        • monkeybutton 4 years ago

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

          • sameerds 4 years ago

            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.

          • em3rgent0rdr 4 years ago

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

          • mike_hock 4 years ago

            Mine still does that.

        • lupire 4 years ago

          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.

    • Firmwarrior 4 years ago

      I think on Windows you can run powercfg /requests

      I'm not sure about linux or Mac though

      • ksherlock 4 years ago

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

      • Too 4 years ago

        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.

  • iforgotpassword 4 years ago

    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.

  • codetrotter 4 years ago

    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.

  • bryans 4 years ago

    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.

  • zinekeller 4 years ago

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

    • SV_BubbleTime 4 years ago

      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.

  • mike_hock 4 years ago

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

  • heyoni 4 years ago

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

serf 4 years ago

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 4 years ago

    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 4 years ago

    Indeed, how awful.

Hamcha 4 years ago

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 4 years ago

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

  • huseyinkeles 4 years ago

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

  • vulcan01 4 years ago

    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 4 years ago

      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)

      • windexh8er 4 years ago

        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.

      • ecf 4 years ago

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

        • freediver 4 years ago

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

          • adamomada 4 years ago

            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!

  • buzzert 4 years ago

    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.

  • simonjgreen 4 years ago

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

  • anamexis 4 years ago

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

  • StrangeSound 4 years ago

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

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

  • jedberg 4 years ago

    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!

  • Destiner 4 years ago

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

  • bobbylarrybobby 4 years ago

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

  • lupire 4 years ago

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

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

  • 323 4 years ago

    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.

WalterBright 4 years ago

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

  • motohagiography 4 years ago

    I fully expect mine to f' everything.

    • labrador 4 years ago

      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

velcrovan 4 years ago

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.

  • SteveDR 4 years ago

    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.

    • jcynix 4 years ago

      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.

    • latexr 4 years ago

      > 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 4 years ago

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

        • bajsejohannes 4 years ago

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

          • lambdaba 4 years ago

            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

  • reidjs 4 years ago

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

    • tomstuart 4 years ago

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

      • jen729w 4 years ago

        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.

    • Tao331 4 years ago

      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.

ognarb 4 years ago

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

  • mxuribe 4 years ago

    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?

    • seba_dos1 4 years ago

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

      • sundarurfriend 4 years ago

        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.

        • seba_dos1 4 years ago

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

      • mxuribe 4 years ago

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

  • meibo 4 years ago

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

Minor49er 4 years ago

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

taftster 4 years ago

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

  • toastal 4 years ago

    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.

  • ghotli 4 years ago

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

    • taftster 4 years ago

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

  • JadeNB 4 years ago

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

    Can't you have a separate, tiny window?

    • throwanem 4 years ago

      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.

justusthane 4 years ago

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?

  • bradleyjkempOP 4 years ago

    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 4 years ago

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

  • remram 4 years ago

    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.

  • robbedpeter 4 years ago

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

    • skykooler 4 years ago

      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.

    • Too 4 years ago

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

    • TobyTheDog123 4 years ago

      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 4 years ago

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

hk__2 4 years ago

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

  • capableweb 4 years ago

    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.

gabrielsroka 4 years ago

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.

  • Too 4 years ago

    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.

  • bradleyjkempOP 4 years ago

    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

karussell 4 years ago

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 4 years ago

    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).
achairapart 4 years ago

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.

addsubtract 4 years ago

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.

flakiness 4 years ago

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

tnzk 4 years ago

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.
causality0 4 years ago

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.

robbedpeter 4 years ago

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 4 years ago

    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.

  • Tao331 4 years ago

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

    • robbedpeter 4 years ago

      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.

devit 4 years ago

_Your_ computer, maybe.

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

viggity 4 years ago

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

mrep 4 years ago

Why not just use keep awake?

https://chrome.google.com/webstore/detail/keep-awake/bijihla...

  • tailspin2019 4 years ago

    Why not just use nosleep.page?

    https://nosleep.page

  • 40four 4 years ago

    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 4 years ago

      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

      • 40four 4 years ago

        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!

alloai 4 years ago

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

`

Mikeb85 4 years ago

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

farzher 4 years ago

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

emptysea 4 years ago

Doesn’t seem to work on iOS 15.4.1

boxingrock 4 years ago

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

boo-ga-ga 4 years ago

Very cool, and looks slick!

catsarebetter 4 years ago

I just turn zoom on

dolmen 4 years ago

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

That seems huge for the task.

  • GlitchMr 4 years ago

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

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection