richtr / NoSleep.js

Prevent display sleep and enable wake lock in any Android or iOS web browser.
MIT License
2.23k stars 380 forks source link

[suggestion] Adding Support for the new Wake Lock API (available on Firefox OS and newer Chrome with web-experiments allowed) #70

Closed ghost closed 4 years ago

ghost commented 5 years ago

https://developer.mozilla.org/en-US/docs/Archive/B2G_OS/API/Wake_Lock_API

This is a non-standard feature but it is available on some platforms, essentially it will allow a web-app that uses NoSleep.js, to also switch off the PC screensaver (currently, screensaver and power-saving are not turned off).

Also, it does not require user-interaction.

I've combine it with the normal in a little try/catch...

var screen_lock; //should be available globally for the app.

//....

function toggle_fullscreen(){
  if(false === is_fullscreen_on()){
    console.log("is not in fullscreen, turning on.");
    nosleep.enable();
    if("undefined" !== typeof window.navigator.requestWakeLock){ try{ screen_lock = window.navigator.requestWakeLock("screen");}catch(err){} }    //trying to keep screen-ON using: https://developer.mozilla.org/en-US/docs/Archive/B2G_OS/API/Wake_Lock_API
    fullscreen_on(undefined);
  }
  else{
    console.log("is in fullscreen, turning off.");
    nosleep.disable();
    if("undefined" !== typeof window.navigator.requestWakeLock){ try{ screen_lock.unlock(); }catch(err){} }
    fullscreen_off();
  }
}

//.........

  self.document.documentElement.ondblclick  = function(ev){  //double-click on the document.
                                                toggle_fullscreen();
                                              };
richtr commented 4 years ago

This has been added in https://github.com/richtr/NoSleep.js/pull/95 and is now released as part of v0.11.0.