forked from KTibow/fullscreen-card
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-control-card.js
More file actions
611 lines (571 loc) · 21.8 KB
/
browser-control-card.js
File metadata and controls
611 lines (571 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/**
* Browser Control Card for Home Assistant
* Provides buttons to control browser features such as full-screen mode, wake lock, zoom, and more.
* LICENSE: MIT
* https://github.com/mathoudebine/homeassistant-browser-control-card
*/
const CARD_VERSION = 'v0.0.0'; // Will be replaced by actual version during build
/* Wake Lock part from https://web.dev/wake-lock/ (sources: https://glitch.com/edit/#!/wake-lock-demo?path=script.js%3A1%3A0 ) */
/* Fullscreen part & card design from https://github.com/KTibow/fullscreen-card */
var wake_lock_supported;
if ("wakeLock" in navigator && "request" in navigator.wakeLock) {
wake_lock_supported = true;
} else {
wake_lock_supported = false;
console.warn(
"Browser Control Card: Wake Lock API not supported by this browser."
);
}
var css_zoom_supported;
try {
if (CSS.supports("zoom", "1")) {
css_zoom_supported = true;
} else {
css_zoom_supported = false;
console.warn(
"Browser Control Card: CSS Zoom not supported by this browser."
);
}
} catch (error) {
// When in doubt, display the zoom buttons
css_zoom_supported = true;
console.warn(
"Browser Control Card: CSS Zoom may not be supported by this browser."
);
}
let wakeLock = null;
const requestWakeLock = async () => {
try {
wakeLock = await navigator.wakeLock.request("screen");
} catch (e) {
wakeLock = null;
}
};
const cancelWakeLock = () => {
wakeLock.release();
wakeLock = null;
};
const handleVisibilityChangeOnWakeLock = () => {
if (wakeLock !== null && document.visibilityState === "visible") {
requestWakeLock();
}
};
const ha = document.querySelector("body > home-assistant");
const ha_main = ha.shadowRoot.querySelector("home-assistant-main");
const ha_panel_lovelace = ha_main.shadowRoot.querySelector("ha-panel-lovelace");
function hideNavbar(hideNavbar) {
try {
if (!ha_panel_lovelace) {
return;
}
let huiRoot = ha_panel_lovelace.shadowRoot.querySelector("hui-root");
if (!huiRoot) {
return;
}
huiRoot = huiRoot.shadowRoot;
const view = huiRoot.querySelector("#view");
let appToolbar = huiRoot.querySelector("app-toolbar");
if (!appToolbar) {
// Changed with 2023.04
appToolbar = huiRoot.querySelector("div.toolbar");
}
if (!appToolbar) {
// Changed with 2026.02
appToolbar = huiRoot.querySelector("div.header");
}
if (hideNavbar) {
appToolbar.style.setProperty("display", "none");
view.style.minHeight = "100vh";
view.style.marginTop = "0";
view.style.paddingTop = "0";
} else {
appToolbar.style.removeProperty("display");
view.style.removeProperty("min-height");
view.style.removeProperty("margin-top");
view.style.removeProperty("padding-top");
}
window.dispatchEvent(new Event("resize"));
} catch (e) {
console.warn(e);
}
}
function hideSidebar(hideSidebar) {
try {
if (ha_panel_lovelace) {
const ha_menu_button = ha_panel_lovelace.shadowRoot
.querySelector("hui-root")
.shadowRoot.querySelector("ha-menu-button");
if (ha_menu_button) {
if (hideSidebar) {
ha_menu_button.style.display = "none";
} else {
ha_menu_button.style.removeProperty("display");
}
}
}
} catch (e) {
console.warn(e);
}
try {
const sidebar = ha_main.shadowRoot
.querySelector("ha-drawer")
.shadowRoot.querySelector("aside");
if (sidebar) {
if (hideSidebar) {
sidebar.style.maxWidth = "0px";
ha_main.style.setProperty(
"--mdc-drawer-width",
"env(safe-area-inset-left)"
);
} else {
sidebar.style.maxWidth = "";
ha_main.style.removeProperty("--mdc-drawer-width");
}
window.dispatchEvent(new Event("resize"));
}
} catch (e) {
console.warn(e);
}
}
// Icons and CSS style for buttons
const fullscreen_icon = '<ha-icon icon="mdi:fullscreen"></ha-icon>';
const fullscreen_exit_icon = '<ha-icon icon="mdi:fullscreen-exit"></ha-icon>';
const wake_lock_icon = '<ha-icon icon="mdi:sleep-off"></ha-icon>';
const zoom_in_icon = '<ha-icon icon="mdi:magnify-plus"></ha-icon>';
const zoom_out_icon = '<ha-icon icon="mdi:magnify-minus"></ha-icon>';
const refresh_icon = '<ha-icon icon="mdi:refresh"></ha-icon>';
const hide_navbar_icon = '<ha-icon icon="mdi:table-row-remove"></ha-icon>';
const hide_sidebar_icon = '<ha-icon icon="mdi:playlist-remove"></ha-icon>';
const minimize_icon = '<ha-icon icon="mdi:eye-off"></ha-icon>';
const restore_icon = '<ha-icon icon="mdi:eye"></ha-icon>';
const hide_card_icon = '<ha-icon icon="mdi:close"></ha-icon>';
const button_common_style =
"border: none;" +
"padding: 0.5rem;" +
"display: inline-block;" +
"text-align: center;" +
"border-radius: var(--ha-card-border-radius, 4px);" +
"cursor: pointer;" +
"margin-right: 5px;" +
"margin-bottom: 2px;" +
"margin-top: 2px;";
const big_icon_style = "--mdc-icon-size: var(--ha-font-size-5xl);";
function getIconStyle(small_icon) {
if (small_icon) {
return "";
} else {
return big_icon_style;
}
}
function getButtonStyle(button_color, icon_color) {
let button_style = button_common_style;
if (button_color != null && button_color !== "") {
button_style += "background: " + button_color + ";";
}
else {
button_style += "background: var(--primary-color);";
}
if (icon_color != null && icon_color !== "") {
button_style += "color: " + icon_color + ";";
}
else {
button_style += "color: var(--text-primary-color);";
}
return button_style;
}
class BrowserControlCard extends HTMLElement {
set hass(hass) {
this._hass = hass;
}
setConfig(config) {
// Clone the configuration to edit it if necessary (e.g. for old config. keys)
this.config = structuredClone(config);
if (this.config) {
this.content = document.createElement("ha-card");
if (!this.config.no_padding) {
this.content.style.padding = "1rem";
}
if (this.config.no_background) {
this.content.style.background = "none";
this.content.style.border = "none";
}
this.content.style.display = "flex";
this.content.style.alignItems = "center";
if (!this.config.layout) {
this.config.layout = "center"
}
this.content.style.justifyContent = this.config.layout;
// Convert old configuration keys
if (!this.config.controls) {
this.config.controls = [];
if (!this.config.hide_fullscreen) {
this.config.controls.push("fullscreen")
}
if (!this.config.hide_screenlock) {
this.config.controls.push("wakelock")
}
if (!this.config.hide_zoom) {
this.config.controls.push("zoom")
}
if (!this.config.hide_refresh) {
this.config.controls.push("reload")
}
if (!this.config.hide_navbar) {
this.config.controls.push("navbar")
}
if (!this.config.hide_sidebar) {
this.config.controls.push("sidebar")
}
}
/********************************************************
Full-screen button
********************************************************/
if (this.config.controls.includes('fullscreen')) {
this.fullscreen = false;
this.fullscreenbtn = document.createElement("button");
this.fullscreenbtn.innerHTML = fullscreen_icon;
this.fullscreenbtn.style.cssText = getButtonStyle(this.config.button_color, this.config.icon_color);
this.fullscreenbtn.title = "Enter fullscreen";
this.fullscreenbtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.fullscreenbtn.onclick = function () {
if (this.fullscreen) {
document.exitFullscreen();
this.fullscreenbtn.innerHTML = fullscreen_icon;
this.fullscreenbtn.getElementsByTagName(
"ha-icon"
)[0].style.cssText = getIconStyle(this.config.small_buttons);
this.fullscreenbtn.title = "Enter fullscreen";
} else {
document.documentElement.requestFullscreen();
this.fullscreenbtn.innerHTML = fullscreen_exit_icon;
this.fullscreenbtn.getElementsByTagName(
"ha-icon"
)[0].style.cssText = getIconStyle(this.config.small_buttons);
this.fullscreenbtn.title = "Exit fullscreen";
}
this.fullscreen = !this.fullscreen;
}.bind(this);
this.content.appendChild(this.fullscreenbtn);
}
/********************************************************
Sleep lock button (if supported)
********************************************************/
if (this.config.controls.includes('wakelock') && wake_lock_supported) {
this.wake_lock = false;
this.nowakebtn = document.createElement("button");
this.nowakebtn.innerHTML = wake_lock_icon;
this.nowakebtn.style.cssText = getButtonStyle(this.config.button_color, this.config.icon_color);
this.nowakebtn.style.background = "var(--disabled-color)";
this.nowakebtn.title = "Keep screen awake";
this.nowakebtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.nowakebtn.onclick = function () {
if (this.wake_lock) {
document.removeEventListener(
"visibilitychange",
handleVisibilityChangeOnWakeLock
);
document.removeEventListener(
"fullscreenchange",
handleVisibilityChangeOnWakeLock
);
cancelWakeLock();
this.nowakebtn.title = "Keep screen awake";
this.nowakebtn.style.background = "var(--disabled-color)";
} else {
requestWakeLock();
document.addEventListener(
"visibilitychange",
handleVisibilityChangeOnWakeLock
);
document.addEventListener(
"fullscreenchange",
handleVisibilityChangeOnWakeLock
);
this.nowakebtn.title = "Stop keeping screen awake";
this.nowakebtn.style.background = "var(--primary-color)";
}
this.wake_lock = !this.wake_lock;
}.bind(this);
this.content.appendChild(this.nowakebtn);
}
/********************************************************
Zoom buttons
********************************************************/
if (this.config.controls.includes('zoom') && css_zoom_supported) {
this.zoom_level = 1.0;
this.zoominbtn = document.createElement("button");
this.zoominbtn.innerHTML = zoom_in_icon;
this.zoominbtn.style.cssText = getButtonStyle(this.config.button_color, this.config.icon_color);
this.zoominbtn.title = "Zoom in";
this.zoominbtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.zoominbtn.onclick = function () {
this.zoom_level = this.zoom_level + 0.1;
document.body.style.zoom = this.zoom_level;
}.bind(this);
this.content.appendChild(this.zoominbtn);
this.zoomoutbtn = document.createElement("button");
this.zoomoutbtn.innerHTML = zoom_out_icon;
this.zoomoutbtn.style.cssText = getButtonStyle(this.config.button_color, this.config.icon_color);
this.zoomoutbtn.title = "Zoom out";
this.zoomoutbtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.zoomoutbtn.onclick = function () {
this.zoom_level = this.zoom_level - 0.1;
if (this.zoom_level < 0.0) {
this.zoom_level = 0.0;
} else {
document.body.style.zoom = this.zoom_level;
}
}.bind(this);
this.content.appendChild(this.zoomoutbtn);
}
/********************************************************
Refresh button
********************************************************/
if (this.config.controls.includes('reload')) {
this.refreshbtn = document.createElement("button");
this.refreshbtn.innerHTML = refresh_icon;
this.refreshbtn.style.cssText = getButtonStyle(this.config.button_color, this.config.icon_color);
this.refreshbtn.title = "Refresh page";
this.refreshbtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.refreshbtn.onclick = function () {
document.location.reload();
}.bind(this);
this.content.appendChild(this.refreshbtn);
}
/********************************************************
Hide Navigation Bar
********************************************************/
if (this.config.controls.includes('navbar')) {
this.hidden_navbar = false;
this.navbarbtn = document.createElement("button");
this.navbarbtn.innerHTML = hide_navbar_icon;
this.navbarbtn.style.cssText = getButtonStyle(this.config.button_color, this.config.icon_color);
this.navbarbtn.style.background = "var(--disabled-color)";
this.navbarbtn.title = "Hide navigation bar";
this.navbarbtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.navbarbtn.onclick = function () {
if (this.hidden_navbar) {
hideNavbar(false);
this.navbarbtn.title = "Hide navigation bar";
this.navbarbtn.style.background = "var(--disabled-color)";
} else {
hideNavbar(true);
this.navbarbtn.title = "Show navigation bar";
this.navbarbtn.style.background = "var(--primary-color)";
}
this.hidden_navbar = !this.hidden_navbar;
}.bind(this);
this.content.appendChild(this.navbarbtn);
}
/********************************************************
Hide Sidebar
********************************************************/
if (this.config.controls.includes('sidebar')) {
this.hidden_sidebar = false;
this.sidebarbtn = document.createElement("button");
this.sidebarbtn.innerHTML = hide_sidebar_icon;
this.sidebarbtn.style.cssText = getButtonStyle(this.config.button_color, this.config.icon_color);
this.sidebarbtn.style.background = "var(--disabled-color)";
this.sidebarbtn.title = "Hide sidebar";
this.sidebarbtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.sidebarbtn.onclick = function () {
if (this.hidden_sidebar) {
hideSidebar(false);
this.sidebarbtn.style.background = "var(--disabled-color)";
this.sidebarbtn.title = "Hide sidebar";
} else {
hideSidebar(true);
this.sidebarbtn.style.background = "var(--primary-color)";
this.sidebarbtn.title = "Show sidebar";
}
this.hidden_sidebar = !this.hidden_sidebar;
}.bind(this);
this.content.appendChild(this.sidebarbtn);
}
/********************************************************
Minimize card (show/hide controls)
********************************************************/
if (this.config.controls.includes('minimize')) {
this.card_hidden = false;
this.minimizebtn = document.createElement("button");
this.minimizebtn.innerHTML = minimize_icon;
this.minimizebtn.style.cssText = getButtonStyle(this.config.button_color, this.config.icon_color);
this.minimizebtn.title = "Minimize card";
this.minimizebtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.minimizebtn.onclick = function () {
if (this.card_hidden) {
// Show: restore the card content, show button with eye-off icon
this.content.childNodes.forEach(function (child) {
if (child !== this.minimizebtn) {
child.style.display = "";
}
}.bind(this));
this.minimizebtn.innerHTML = minimize_icon;
this.minimizebtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.minimizebtn.title = "Minimize card";
} else {
// Hide: make all other buttons invisible, keep only this button
this.content.childNodes.forEach(function (child) {
if (child !== this.minimizebtn) {
child.style.display = "none";
}
}.bind(this));
this.minimizebtn.innerHTML = restore_icon;
this.minimizebtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.minimizebtn.title = "Show controls";
}
this.card_hidden = !this.card_hidden;
}.bind(this);
this.content.appendChild(this.minimizebtn);
}
/********************************************************
Hide card (will come back on page reload)
********************************************************/
if (this.config.controls.includes('hide')) {
this.hidecardbtn = document.createElement("button");
this.hidecardbtn.innerHTML = hide_card_icon;
this.hidecardbtn.style.cssText = getButtonStyle(this.config.button_color, this.config.icon_color);
this.hidecardbtn.title = "Hide card (reload page to restore)";
this.hidecardbtn.getElementsByTagName("ha-icon")[0].style.cssText =
getIconStyle(this.config.small_buttons);
this.hidecardbtn.onclick = function () {
this.style.display = "none";
}.bind(this);
this.content.appendChild(this.hidecardbtn);
}
while (this.firstChild) {
this.removeChild(this.firstChild);
}
this.appendChild(this.content);
}
}
static getStubConfig() {
return {
controls: [
"fullscreen",
"wakelock",
"zoom",
"reload",
"navbar",
"sidebar",
"minimize",
"hide"
],
no_padding: false,
small_buttons: true,
no_background: false,
layout: "center",
};
}
getCardSize() {
return 2;
}
static getConfigForm() {
return {
schema: [
{
type: "expandable",
name: "",
title: "Features",
flatten: true,
schema: [
{
name: "controls",
required: true,
selector: {
select: {
options: [
{ label: "Full screen", value: "fullscreen" },
{ label: "Keep screen awake", value: "wakelock" },
{ label: "Zoom in/out", value: "zoom" },
{ label: "Reload page", value: "reload" },
{ label: "Toggle navigation bar", value: "navbar" },
{ label: "Toggle sidebar", value: "sidebar" },
{ label: "Minimize card (hide controls)", value: "minimize" },
{ label: "Hide card (reload page to restore)", value: "hide" },
],
multiple: true,
},
},
},
],
},
{
type: "expandable",
name: "",
title: "Style",
flatten: true,
schema: [
{
type: "grid",
name: "",
schema: [
{ name: "no_padding", selector: { boolean: {} } },
{ name: "no_background", selector: { boolean: {} } },
{ name: "small_buttons", selector: { boolean: {} } },
{
name: "layout",
selector: {
select: { options: ["center", "space-around", "left", "right"], multiple: false, mode: "dropdown" },
},
},
{ name: "button_color", selector: { text: {} } },
{ name: "icon_color", selector: { text: {} } },
],
},
],
},
],
computeLabel: (schema) => {
switch (schema.name) {
case "controls":
return "Available controls";
case "layout":
return "Horizontal layout";
}
return undefined;
},
computeHelper: (schema) => {
switch (schema.name) {
case "no_padding":
return "Remove white space (padding) between buttons and card borders";
case "controls":
return "Select the controls to display. Some controls may be hidden if your current browser does not support the feature.";
case "layout":
return "Configure controls alignment and spacing inside the card";
case "no_background":
return "Remove card background and borders (transparent card)";
case "button_color":
return "CSS color for buttons. Leave empty to use theme primary-color.";
case "icon_color":
return "CSS color for icons. Leave empty to use theme text-primary-color.";
}
return undefined;
},
};
}
}
customElements.define("browser-control-card", BrowserControlCard);
window.customCards = window.customCards || [];
window.customCards.push({
type: "browser-control-card",
name: "Browser Control Card",
preview: true,
description:
"Card to control browser settings: full-screen, wake lock, zoom...",
});
console.info(
`%c BROWSER-CONTROL-CARD %c ${CARD_VERSION} `,
'color: white; background: #009ac7; font-weight: bold;',
'color: #009ac7; background: white; font-weight: bold;'
);