K
K
kazantsev_alex2021-10-29 09:32:27
Notifications
kazantsev_alex, 2021-10-29 09:32:27

How to make a sound notification for a new post on the site?

there is code:

{
async function sha256(message) {
const script = document.currentScript;
const msgBuffer = new TextEncoder('utf-8').encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
return hashHex;
}
sha256(document.body.innerText).then(hash => {
const oh = localStorage.getItem('tab-reloader-hash');
if (oh && oh !== hash) {
script.dispatchEvent(new CustomEvent( 'play-sound', {
detail: ' https://www.w3schools.com/html/horse.
}
localStorage.setItem('tab-reloader-hash', hash);
});
}

We can convert the content of the page to a hash code and use it to evaluate whether or not there is a new content update. The HASH string is small, so we can just store it for later use. The following code calculates a SHA256 hash code from all page content (you can even restrict the code to use the content of a specific HTML element) and stores it in a constant variable. This hash code is calculated after each page refresh, and if the value changes, the user gets a sample sound alert:

No sound alert, in my Mozilla. There is another code in which it plows, because it is called in a different way:
{
const script = document.currentScript;
window.setTimeout(() => {
script.dispatchEvent(new Event('activate-tab'));
}, 500);
}
help to make it work please

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question