Никита Дубко, Веб-стандарты
Никита Дубко, Веб-стандарты
if ('contacts' in navigator) {
import('./contacts.mjs');
}
const copy = async (blob) => {
try {
await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob,
}),
]);
} catch (err) {
console.error(err.name, err.message);
}
};
navigator.bluetooth.requestDevice({
filters: [{
services: [
0x1234,
0x12345678,
'99999999-0000-1000-8000-00805f9b34fb'
]
}]
})
.then(device => { /* … */ })
.catch(error => { console.error(error); });
const unreadCount = 24;
// Set the badge
navigator.setAppBadge(unreadCount).catch((error) => {
// Do something with the error.
});
// Clear the badge
navigator.clearAppBadge().catch((error) => {
// Do something with the error.
});
const ac = new AbortController();
navigator.credentials.get({
otp: { transport: ['sms'] },
signal: ac.signal
}).then(otp => {
input.value = otp.code;
if (form) form.submit();
}).catch(err => {
console.log(err);
});
const modelConstraint = {
languages: ['en']
};
try {
const recognizer = await navigator
.createHandwritingRecognizer(modelConstraint);
// Do something with recognizer
} catch (err) {
// Ooops
}
const requestWakeLock = async () => {
try {
wakeLock = await navigator.wakeLock.request();
wakeLock.addEventListener('release', () => {
console.log('Screen Wake Lock released:', wakeLock.released);
});
} catch (err) {
// Oops
}
};
await requestWakeLock();
window.setTimeout(() => {
wakeLock.release();
wakeLock = null;
}, 5000);
const eyeDropper = new EyeDropper();
try {
const result = await eyeDropper.open();
// The user selected a pixel, here is its color:
const colorHexValue = result.sRGBHex;
} catch (err) {
// The user escaped the eyedropper mode.
}
const sensor = new AmbientLightSensor();
sensor.addEventListener('reading', (event) => {
console.log('Current light level:', sensor.illuminance);
});
sensor.addEventListener('error', (event) => {
console.log(event.error.name, event.error.message);
});
sensor.start();
const lock = await navigator.wakeLock.request({
increaseBrightness: true
});
// Or
try {
const sentinel = await screen.requestBrightnessIncrease();
window.setTimeout(() => {
await sentinel.release();
}, 5000);
} catch (e) {
// Oops
}
// app.manifest
"lock_screen": {
"start_url": "/some/url"
}
// app.js
const data = await window.getLockScreenData();
await data.setData("my-key", "my-content");
const controller = new AbortController();
const signal = controller.signal;
const idleDetector = new IdleDetector();
idleDetector.addEventListener('change', () => {
const userState = idleDetector.userState;
const screenState = idleDetector.screenState;
console.log(`Idle change: ${userState}, ${screenState}.`);
});
await idleDetector.start({
threshold: 60000,
signal,
});
try {
const screenDetails = await window.getScreenDetails();
const primaryScreen = screenDetails
.screens
.filter((screen) => screen.isPrimary)[0];
await document.body.requestFullscreen({
screen: primaryScreen
});
} catch (err) {
// Oops
}
function callback(update) {
if (update.cpuSpeed > 0.5) {
// The CPU is running at faster than base speed.
} else {
// The CPU is running at normal or reduced speed.
}
if (update.cpuUtilization >= 0.9) {
// CPU utilization is over 90%.
} else if (update.cpuUtilization >= 0.5) {
// CPU utilization is over 50%.
} else {
// CPU utilization is under 50%.
}
}
const observer = new ComputePressureObserver(callback, {
cpuUtilizationThresholds: [0.5, 0.9],
cpuSpeedThresholds: [0.5],
});
observer.observe();
// Later
observer.unobserve();