no-sleep-app
Version:
NoSleepApp is a lightweight JavaScript library that prevents devices from going to sleep during critical activities. It uses the Screen Wake Lock API or a fallback video playback method to keep the screen active, ensuring uninterrupted user experiences ac
135 lines (107 loc) • 4.53 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: no-sleep-element.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: no-sleep-element.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @fileoverview NoSleepElement is a utility class that creates and manages a hidden video element
* to prevent devices from entering sleep mode when the Wake Lock API is not available.
*/
import { webm, mp4 } from './files.js';
/**
* Class to manage a hidden video element that prevents devices from sleeping.
*/
export default class NoSleepElement {
constructor() {
/**
* @type {HTMLVideoElement} The hidden video element used to prevent sleep.
*/
this.noSleepVideo = document.createElement("video");
this.noSleepVideo.setAttribute("playsinline", ""); // Prevents video from using fullscreen by default.
this._addSourceToVideo(this.noSleepVideo, "webm", webm);
this._addSourceToVideo(this.noSleepVideo, "mp4", mp4);
}
/**
* Adds a source to the video element.
* @private
* @param {HTMLVideoElement} video The video element to which the source is added.
* @param {string} type The MIME type of the video source (e.g., "webm" or "mp4").
* @param {string} dataURI The data URI of the video source.
*/
_addSourceToVideo(video, type, dataURI) {
const source = document.createElement("source");
source.src = dataURI;
source.type = `video/${type}`;
video.appendChild(source);
}
/**
* Sets up a listener for the `loadedmetadata` event on the video element.
* This ensures the video behaves correctly based on its duration.
*/
setMetadataListener() {
this.noSleepVideo.addEventListener("loadedmetadata", this._onVideoLoadedMetadata.bind(this));
}
/**
* Handles the `loadedmetadata` event of the video.
* Configures the video to either loop or adjust playback position based on its duration.
* @private
*/
_onVideoLoadedMetadata() {
if (this.noSleepVideo.duration <= 1) {
this.noSleepVideo.setAttribute("loop", true);
} else {
this.noSleepVideo.addEventListener("timeupdate", this._onVideoTimeUpdate.bind(this));
}
}
/**
* Handles the `timeupdate` event of the video.
* Prevents the video from playing beyond a certain point by resetting the playback position.
* @private
*/
_onVideoTimeUpdate() {
if (this.noSleepVideo.currentTime > 0.5) {
this.noSleepVideo.currentTime = Math.random(); // Resets to a random time to maintain activity.
}
}
/**
* Plays the video to keep the device awake.
* @returns {Promise<void>} Resolves when playback successfully starts.
*/
play() {
return this.noSleepVideo.play();
}
/**
* Pauses the video, stopping its effect of preventing sleep.
*/
pause() {
this.noSleepVideo.pause();
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="module.exports.html">exports</a></li></ul><h3>Global</h3><ul><li><a href="global.html#disable">disable</a></li><li><a href="global.html#enable">enable</a></li><li><a href="global.html#handleFullscreenChange">handleFullscreenChange</a></li><li><a href="global.html#handleVisibilityChange">handleVisibilityChange</a></li><li><a href="global.html#isEnabled">isEnabled</a></li><li><a href="global.html#isNativeWakeLockSupported">isNativeWakeLockSupported</a></li><li><a href="global.html#isOldIOS">isOldIOS</a></li><li><a href="global.html#pause">pause</a></li><li><a href="global.html#play">play</a></li><li><a href="global.html#removeListeners">removeListeners</a></li><li><a href="global.html#setMetadataListener">setMetadataListener</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Fri Nov 22 2024 23:19:12 GMT+0000 (Greenwich Mean Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>