NoSleep.js icon indicating copy to clipboard operation
NoSleep.js copied to clipboard

Bind nosleep state to a cookie for persistency after browser reload?

Open neocamel opened this issue 5 years ago • 7 comments

  • Device Name: Pixel 2
  • Device Version: Android v 9
  • Browser Name: Chrome
  • Browser Version: 70.0.3626.121
  • NoSleep.js Version: v0.9.0

Feature Proposal

Is it (or could it be) possible to store the state of the nosleep in a cookie, so that the setting is persistent even if the user refreshes the browser window?

Feature Use Case

I have a web application where the user is monitoring an output of Wordpress posts, and I have a javascript snippet that is reloading the page every 60 seconds to retrieve any new posts. If the user enables the wake lock, but the page then reloads itself, it turns off the wake lock. I was thinking if the state of the wake lock could be bound to a cookie, then when the page reloads the page the script could check the status of the cookie and decide if the wake lock should be enabled or disabled upon page reload. Is this possible?

neocamel avatar Mar 10 '19 17:03 neocamel

I don’t think that is possible because a video can only be played inside a user interaction callback (eg. in addEventlistener). After a reload this is not possible.

ngerritsen avatar May 17 '19 15:05 ngerritsen

That makes sense. I solved this problem by reloading the content of my page periodically using AJAX, so the browser window doesn't need to be refreshed. That said, if a user does reload the browser window, the nosleep is reset to the 'off' state, it's not ideal, but it works for my purposes.

neocamel avatar May 17 '19 15:05 neocamel

Actually, if I could elaborate on my previous comment. I'm noticing something that is a bit of a bug. It could be due to a mistake in how I integrated nosleep.js, but I'm not sure:

If I load my page, and click my 'nosleep' button. It keeps the screen awake. If I refresh my browser window, but 'nosleep' button is again showing disabled, so I click it 'on' again, but it doesn't work this time, and the screen dims. I have to go 'on/off/on' for it to register.

Any ideas what areas I could look in to troubleshoot this?

neocamel avatar Jul 25 '19 18:07 neocamel

@neocamel need reset the state of your button, if using ajax to reload the entire Feed Object and the button and sleep out of the object, the button reset sucessfull on browser refresh or history change but not if the component refresh and not browser, you need remove event linstener on beforeDestroy the component on register event listener call

flourigh avatar Aug 12 '19 10:08 flourigh

@flourigh Actually the part of the content that I want to constantly refresh is inside an ajax container that refreshes every 30 seconds, the nosleep button is outside of that ajax container, because I don't want the state of the nosleep to change when the ajax container refreshes. This is working correctly for me.

The trouble I'm having is when a full page refresh is done. The nosleep button appears as a disabled status (as it should), but when I click it to enable it again, the button shows as enabled, but the screen turns off still. I have to turn it on > off again > on again in order for the nosleep to work.

neocamel avatar Aug 12 '19 17:08 neocamel

@flourigh Actually the part of the content that I want to constantly refresh is inside an ajax container that refreshes every 30 seconds, the nosleep button is outside of that ajax container, because I don't want the state of the nosleep to change when the ajax container refreshes. This is working correctly for me.

The trouble I'm having is when a full page refresh is done. The nosleep button appears as a disabled status (as it should), but when I click it to enable it again, the button shows as enabled, but the screen turns off still. I have to turn it on > off again > on again in order for the nosleep to work.

Did you solve this problem? I'm facing the same thing. NoSleep doesn't work after the page reloads.

renanxx1 avatar Mar 11 '21 18:03 renanxx1

No i haven't solved this issue yet, but i haven't been working on it lately.

What has worked for me is, after page reload, toggle nosleep on and off a few times, stopping on "on". Then it works. I have the js event bound to a button on my site, so i just tap it a few times and set it to "on" and it works.

Not a practical solution for a commercial application, but works for my personal use.

On Thu, Mar 11, 2021, 11:41 AM renanxx1 @.***> wrote:

@flourigh https://github.com/flourigh Actually the part of the content that I want to constantly refresh is inside an ajax container that refreshes every 30 seconds, the nosleep button is outside of that ajax container, because I don't want the state of the nosleep to change when the ajax container refreshes. This is working correctly for me.

The trouble I'm having is when a full page refresh is done. The nosleep button appears as a disabled status (as it should), but when I click it to enable it again, the button shows as enabled, but the screen turns off still. I have to turn it on > off again > on again in order for the nosleep to work.

Did you solve this problem? I'm facing the same thing. NoSleep doesn't work after the page reloads.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/richtr/NoSleep.js/issues/67#issuecomment-796957056, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABCZV2LE3DXPPEA3YV2A7EDTDEFF7ANCNFSM4G45TEJQ .

neocamel avatar Mar 11 '21 19:03 neocamel