Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the option to specify a title when the enable function is called #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions dist/NoSleep.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ var NoSleep = function () {

_classCallCheck(this, NoSleep);

this.title = "No sleep";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use capital S here to keep the default value as-is (i.e. No Sleep)

this.enabled = false;
if (nativeWakeLock()) {
this._wakeLock = null;
Expand All @@ -144,7 +145,7 @@ var NoSleep = function () {
// Set up no sleep video element
this.noSleepVideo = document.createElement("video");

this.noSleepVideo.setAttribute("title", "No Sleep");
this.noSleepVideo.setAttribute("title", this.title);
this.noSleepVideo.setAttribute("playsinline", "");

this._addSourceToVideo(this.noSleepVideo, "webm", webm);
Expand Down Expand Up @@ -179,11 +180,14 @@ var NoSleep = function () {
value: function enable() {
var _this2 = this;

var title = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.title;

if (nativeWakeLock()) {
return navigator.wakeLock.request("screen").then(function (wakeLock) {
_this2._wakeLock = wakeLock;
_this2.enabled = true;
console.log("Wake Lock active.");
_this2.title = title;
console.log("Wake Lock active.", _this2.title);
_this2._wakeLock.addEventListener("release", function () {
// ToDo: Potentially emit an event for the page to observe since
// Wake Lock releases happen when page visibility changes.
Expand Down
2 changes: 1 addition & 1 deletion dist/NoSleep.min.js

Large diffs are not rendered by default.

58 changes: 29 additions & 29 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<title>NoSleep.js - Simple Test Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>NoSleep Test Page</h1>
<script src="../dist/NoSleep.min.js"></script>
<head>
<title>NoSleep.js - Simple Test Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>NoSleep Test Page</h1>
<script src="../dist/NoSleep.min.js"></script>
Comment on lines -3 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diff seems to originate due to the use of different format settings from the repo's owner. I suggest reverting those changes because the owner may not like them


<input type="button" id="toggle" value="Wake Lock is disabled" />
<input type="button" id="toggle" value="Wake Lock is disabled" />

<script>
var noSleep = new NoSleep();
<script>
var noSleep = new NoSleep();

var wakeLockEnabled = false;
var toggleEl = document.querySelector("#toggle");
toggleEl.addEventListener('click', function() {
if (!wakeLockEnabled) {
noSleep.enable(); // keep the screen on!
wakeLockEnabled = true;
toggleEl.value = "Wake Lock is enabled";
document.body.style.backgroundColor = "green";
} else {
noSleep.disable(); // let the screen turn off.
wakeLockEnabled = false;
toggleEl.value = "Wake Lock is disabled";
document.body.style.backgroundColor = "";
}
}, false);
</script>
var wakeLockEnabled = false;
var toggleEl = document.querySelector("#toggle");
toggleEl.addEventListener('click', function() {
if (!wakeLockEnabled) {
noSleep.enable(); // keep the screen on! If specified set the title of the video
wakeLockEnabled = true;
toggleEl.value = "Wake Lock is enabled";
document.body.style.backgroundColor = "green";
} else {
noSleep.disable(); // let the screen turn off.
wakeLockEnabled = false;
toggleEl.value = "Wake Lock is disabled";
document.body.style.backgroundColor = "";
}
}, false);
</script>

<!-- Github Ribbon -->
<a href="https://github.com/richtr/NoSleep.js"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
</body>
<!-- Github Ribbon -->
<a href="https://github.com/richtr/NoSleep.js"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
</body>
</html>
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const nativeWakeLock = () => "wakeLock" in navigator;

class NoSleep {
constructor() {
this.title = "No sleep";
this.enabled = false;
if (nativeWakeLock()) {
this._wakeLock = null;
Expand All @@ -37,7 +38,7 @@ class NoSleep {
// Set up no sleep video element
this.noSleepVideo = document.createElement("video");

this.noSleepVideo.setAttribute("title", "No Sleep");
this.noSleepVideo.setAttribute("title", this.title);
this.noSleepVideo.setAttribute("playsinline", "");

this._addSourceToVideo(this.noSleepVideo, "webm", webm);
Expand Down Expand Up @@ -70,13 +71,14 @@ class NoSleep {
return this.enabled;
}

enable() {
enable(title = this.title) {
if (nativeWakeLock()) {
return navigator.wakeLock
.request("screen")
.then((wakeLock) => {
this._wakeLock = wakeLock;
this.enabled = true;
this.title = title;
console.log("Wake Lock active.");
this._wakeLock.addEventListener("release", () => {
// ToDo: Potentially emit an event for the page to observe since
Expand Down