å®å ¨ãªã³ã³ããã¹ãç¨: ãã®æ©è½ã¯ä¸é¨ã¾ãã¯ãã¹ã¦ã®å¯¾å¿ãã¦ãããã©ã¦ã¶ã¼ã«ããã¦ãå®å ¨ãªã³ã³ããã¹ã (HTTPS) ã§ã®ã¿å©ç¨ã§ãã¾ãã
ã¡ã¢: ãã®æ©è½ã¯ã¦ã§ãã¯ã¼ã«ã¼å ã§å©ç¨å¯è½ã§ãã
éç¥ API ã¯ãã¦ã§ããã¼ã¸ãã¢ããªããã·ã¹ãã ã¬ãã«ã§ãã¼ã¸å¤é¨ã«è¡¨ç¤ºãããéç¥ãéããã¨ãå¯è½ã«ãã¾ããããã«ãããã¢ããªã±ã¼ã·ã§ã³ãã¢ã¤ãã«ãããã¯ã°ã©ã¦ã³ãã®ç¶æ ã§ãã£ã¦ããã¢ããªããã¦ã¼ã¶ã¼ã«æ å ±ãéä¿¡ãããã¨ãã§ãã¾ãããã®è¨äºã§ã¯ãã¢ããªã§ API ã使ç¨ããæ¹æ³ã®åºæ¬ãè¦ã¦ããã¾ãã
ä¸è¬çã«ã·ã¹ãã éç¥ã¨ã¯ããªãã¬ã¼ãã£ã³ã°ã·ã¹ãã ã®æ¨æºçãªéç¥ã¡ã«ããºã ãæãã¾ããä¸è¬çãªãã¹ã¯ãããã·ã¹ãã ãã¢ãã¤ã«ç«¯æ«ããéç¥ãè¡ãæ¹æ³ã®å®ä¾ãæãåºãã¾ãããã
å½ç¶ãªããã·ã¹ãã éç¥ã®ä»çµã¿ã¯ãã©ãããã©ã¼ã ããã©ã¦ã¶ã¼ã«ãã£ã¦ç°ãªãã¾ããåé¡ã¯ãªããéç¥ API ã¯ã»ã¨ãã©ã®ã·ã¹ãã éç¥ã«å¯¾ãã¦ååãªäºææ§ãæã¤ããã«ãªã£ã¦ãã¾ãã
ä¾ã¦ã§ãéç¥ã®æãé¡èãªç¨éã®ä¸ã¤ããã¦ã§ããã¼ã¹ã®ã¡ã¼ã«ã IRC ã¢ããªã±ã¼ã·ã§ã³ã«ããã¦ãæ°ããã¡ãã»ã¼ã¸ãåä¿¡ããã¨ãã«ãã¦ã¼ã¶ã¼ãã»ãã®ã¢ããªã±ã¼ã·ã§ã³ã§ä½ãããã¦ãã¦ãéç¥ãããå¿ è¦ãããå ´åã§ããããã«ã¯æ°å¤ãã®äºä¾ãåå¨ããä¾ãã° Slack ãªã©ãããã¾ãã
ã¦ã§ãéç¥ãã©ã®ããã«å©ç¨ã§ããã®ãã®èããæ·±ããããã«ãå®ä¸çã®ä¾ã â To-do ãªã¹ãã¢ã㪠â ãæ¸ãã¦ã¿ã¾ããããã㯠IndexedDB ã使ç¨ãã¦ãã¼ã«ã«ã«ãã¼ã¿ãæ ¼ç´ããã¿ã¹ã¯ã®æéãæ¥ãã¨ãã«ã·ã¹ãã éç¥ã使ç¨ãã¦ã¦ã¼ã¶ã¼ã«éç¥ãã¾ãã To-do ãªã¹ãã®ã³ã¼ãããã¦ã³ãã¼ãããããã©ã¤ãã§å®è¡ããã¢ããªãè¦ãããã¦ã¿ã¾ãããã
許å¯ã®è¦æ±ã¢ããªãéç¥ãéä¿¡ã§ããããã«ãªãåã«ãã¦ã¼ã¶ã¼ã¯ã¢ããªããããè¡ã権éãèªããªããã°ãªãã¾ããããã㯠API ãã¦ã§ããã¼ã¸ã®å¤é¨ã«ãããã®ã¨å¯¾è©±ãããã¨ããéã®ä¸è¬çãªè¦ä»¶ã§ããã¦ã¼ã¶ã¼ã¯å°ãªãã¨ã 1 åã¯ã¢ããªã±ã¼ã·ã§ã³ãéç¥ãéããã¨ãèªããªããã°ãªãããããã«ããã©ã®ã¢ããªããµã¤ããéç¥ã表示ãã¦ããããã¦ã¼ã¶ã¼ãå¶å¾¡ãããã¨ãã§ãã¾ãã
éå»ã«ããã·ã¥éç¥ãæªç¨ããããã¨ããã£ããããã¦ã§ããã©ã¦ã¶ã¼ãéçºè ã¯ãã®åé¡ãç·©åããããã®å¯¾çãå®è£ ãå§ãã¦ãã¾ããéç¥ã表示ãããã¨ã®è¨±å¯ãæ±ããã®ã¯ãã¦ã¼ã¶ã¼ã®æä½ (ãã¿ã³ãã¯ãªãã¯ãããªã©) ã®åå¿ã¨ãã¦è¡ãã¹ãã§ããã¦ã¼ã¶ã¼ãåæãã¦ããªãéç¥ã§ã¦ã¼ã¶ã¼ã«è¿·æããããã¹ãã§ã¯ãªãã®ã§ãããã¯ãã¹ããã©ã¯ãã£ã¹ã§ããã ãã§ãªããä»å¾ãã©ã¦ã¶ã¼ã¯ã¦ã¼ã¶ã¼ã®æä½ã«ãã£ã¦èµ·åããããã®ã§ã¯ãªãéç¥ã®è¨±å¯ã®è¦æ±ãæç¤ºçã«æå¦ããããã«ãªãã§ããããä¾ãã°ã Firefox ã¯ãã¼ã¸ã§ã³72ãããã§ã«ãããè¡ã£ã¦ããã Safari ããã°ããåãããããè¡ã£ã¦ãã¾ãã
å ãã¦ã Chrome 㨠Firefox ã§ã¯ããµã¤ããä¿è·ãããã³ã³ããã¹ã (ããªãã¡ HTTPS) ã§ã¯ãªãéãéç¥ãè¦æ±ãããã¨ãã§ãããã¾ãå¥ãªãªã¸ã³ã® <iframe>
ããè¦æ±ãããéç¥ã許å¯ãããã¨ãã§ããªããªãã¾ããã
èªã¿åãå°ç¨ã® Notification.permission
ããããã£ã®å¤ã調ã¹ãã¨ããã§ã«è¨±å¯ãå¾ã¦ãããã確èªã§ãã¾ãããã®ããããã£ã¯ã3 種é¡ã®ããããã®å¤ãåãã¾ãã
default
ã¦ã¼ã¶ã¼ã¯ã¾ã 許å¯ãæ±ãããããã¨ããªãããããã£ã¦éç¥ã¯è¡¨ç¤ºãããªãã
granted
ã¦ã¼ã¶ã¼ã¯ä»¥åã«éç¥è¡¨ç¤ºã®è¨±å¯ãæ±ãããã¦ããã許å¯ããã
denied
ã¦ã¼ã¶ã¼ã¯ãéç¥ã表示ãããã¨ãæç¤ºçã«æå¦ããã
éç¥ã表示ãã許å¯ãã¾ã å¾ã¦ããªãå ´åã¯ãã¢ããªã±ã¼ã·ã§ã³ã¯ Notification.requestPermission()
ã¡ã½ããã使ç¨ãã¦ã¦ã¼ã¶ã¼ã«è¦æ±ããå¿
è¦ãããã¾ãããã£ã¨ãç°¡åãªå½¢ã§ã¯ã次ã®ãããªãã®ãããã¾ãã
Notification.requestPermission().then((result) => {
console.log(result);
});
ããã¯ã¡ã½ããã®ãããã¹ãã¼ã¹ã®çã使ç¨ãã¦ãã¾ããå¤ããã¼ã¸ã§ã³ã«å¯¾å¿ãããå ´åã¯ã次ã®ããã«å¤ãã³ã¼ã«ããã¯çã使ç¨ããå¿ è¦ãããã¾ãã
Notification.requestPermission((result) => {
console.log(result);
});
ã³ã¼ã«ããã¯çã¯ãªãã·ã§ã³ã§ãã¦ã¼ã¶ã¼ã表示ã許å¯ããè¦æ±ã«çããæã«å¼ã³åºãããã³ã¼ã«ããã¯é¢æ°ãåãå ¥ãã¾ãã
ã¡ã¢: Notification.requestPermission
ããããã¹ãã¼ã¹çã«å¯¾å¿ãã¦ãããã©ãããã確å®ã«æ©è½ãã¹ãããæ¹æ³ã¯ããã¾ãããå¤ããã©ã¦ã¶ã¼ã®å¯¾å¿ãå¿
è¦ãªå ´åã¯ãã³ã¼ã«ããã¯çã使ç¨ãã¦ãã ããã鿍奍ã§ã¯ããã¾ãããæ°ãããã©ã¦ã¶ã¼ã§ãåä½ãã¾ãã詳ããã¯ããã©ã¦ã¶ã¼äºææ§è¡¨ãåç
§ãã¦ãã ããã
To-do ãªã¹ãã®ãã¢ã§ã¯ã "Enable notifications" ãã¿ã³ãé ç½®ããæ¼ãããã¨ãã«ã¢ããªã®éç¥ãè¦æ±ãã¾ãã
<button id="enable">Enable notifications</button>
ãããã¯ãªãã¯ããã¨ã askNotificationPermission()
颿°ãå¼ã³åºããã¾ãã
function askNotificationPermission() {
// Check if the browser supports notifications
if (!("Notification" in window)) {
console.log("This browser does not support notifications.");
return;
}
Notification.requestPermission().then((permission) => {
// set the button to shown or hidden, depending on what the user answers
notificationBtn.style.display = permission === "granted" ? "none" : "block";
});
}
æåã« 2 çªç®ã®ã¡ã¤ã³ãããã¯ãè¦ã¦ã¿ãã¨ãã¾ãæåã«éç¥ã«å¯¾å¿ãã¦ãããã©ãããã§ãã¯ãã¦ããã®ãåããã¾ããããããã§ããã°ããããã¹ãã¼ã¹çã® Notification.requestPermission()
ãå®è¡ããããã§ãªãå ´åã¯ãã³ã³ã½ã¼ã«ã«ã¡ãã»ã¼ã¸ããã°åºåãã¾ãã
then
ã«æ¸¡ããããããã¹è§£æ±ºãã³ãã©ã¼å
ã§ãã¦ã¼ã¶ã¼ã権éãã¤ã¢ãã°ã§é¸ãã å
容ã«å¿ãã¦ãã¿ã³ã®è¡¨ç¤ºã»é表示ãåãæ¿ãã¾ãããã§ã«ãã®æ¨©éã許å¯ããã¦ããå ´åã¯ãã¿ã³ã表示ããããããã¾ããããã¦ã¼ã¶ã¼ã権éãæå¦ããããé¸ãã å ´åã¯ãå¾ã§èããå¤ãããã£ã³ã¹ãä¸ããã¹ãã§ãã
éç¥ã®ä½æã¯ç°¡åã§ãã Notification
ã³ã³ã¹ãã©ã¯ã¿ã¼ã使ç¨ããã ãã§ãããã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã¯éç¥å
ã«è¡¨ç¤ºããã¿ã¤ãã«ã¨ãéç¥ãæ¡å¼µããããã®ã¢ã¤ã³ã³ (icon
) ãããã¹ãæ¬æ (body
) ãªã©ã®ãªãã·ã§ã³ãåãåãã¾ãã
ä¾ãã° To-do ãªã¹ãã®ä¾ã§ã¯å¿
è¦ã«å¿ãã¦ä»¥ä¸ã®ã¹ããããã使ç¨ãã¦éç¥ã使ãã¾ã (createNotification()
å
ã«ããã¾ã)ã
const img = "/to-do-notifications/img/icon-128.png";
const text = `HEY! Your task "${title}" is now overdue.`;
const notification = new Notification("To do list", { body: text, icon: img });
éç¥ãéãã
close()
ã使ç¨ãã¦ãã¦ã¼ã¶ã¼ã«é¢ä¿ããªããªã£ãéç¥ãé¤å»ãã¾ã (ä¾ãã°ãã¡ãã»ã¼ã¸ã¢ããªãªã©ã§ãã¦ã¼ã¶ã¼ãæ¢ã«ã¦ã§ããã¼ã¸ä¸ã®éç¥ãèªãã å ´åãã鳿¥½ã¢ããªã§ãã§ã«æ¬¡ã®æ²ãå§ã¾ã£ã¦ãããããæ²ã®å¤æ´ãè¡ãããã®éç¥ãéãããªã©)ãæè¿ã®ãã©ã¦ã¶ã¼ã®å¤ãã¯ãæ°ç§ (ç´ 4 ç§) çµéããã¨éç¥ãèªåçã«è§£é¤ãã¾ãããããã¯ã¦ã¼ã¶ã¼ãã¦ã¼ã¶ã¼ã¨ã¼ã¸ã§ã³ãã®å¤æã«å§ãããã¦ãããããä¸è¬çã«ã¯æ°ã«ããå¿
è¦ã¯ããã¾ãããéç¥ã®åé¤ã¯ãªãã¬ã¼ãã£ã³ã°ã·ã¹ãã ã¬ãã«ã§ãçºçããå¯è½æ§ããããã¦ã¼ã¶ã¼ãå¶å¾¡ã§ããããã«ãã¦ããå¿
è¦ãããã¾ããå¤ããã¼ã¸ã§ã³ã® Chrome ã¯éç¥ãèªåçã«åé¤ããªãã®ã§ãä»ã®ãã©ã¦ã¶ã¼ã®éç¥ãã¬ã¤ããéç¥ãåé¤ããªãããã«ãå¤ããã¼ã¸ã§ã³ã®ãã©ã¦ã¶ã¼ã§ã®ã¿ setTimeout()
ã®å¾ã«åé¤ãã¦ãã ããã
const n = new Notification("My Great Song");
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
// The tab has become visible so clear the now-stale Notification.
n.close();
}
});
ã¡ã¢: ãã® API ã¯ã (ç¾ä»£ã®ãã©ã¦ã¶ã¼ã§) ä¸å®æéçµéå¾ã«éç¥ãç»é¢ããæ¶å»ããã ãã®ããã«ä½¿ç¨ãã¹ãã§ã¯ããã¾ãããéç¥ãæåã«è¡¨ç¤ºãããå¾ã«ã¦ã¼ã¶ã¼ãããã¨ããã¨ããããã¨ãé²ãããããã®ã¡ã½ããã¯éç¥ãã¬ã¤ãããéç¥ãåé¤ããããã§ãã
ã¡ã¢: "close" ã¤ãã³ããåãåã£ãã¨ãããããéç¥ãéããã¦ã¼ã¶ã¼ã§ããã¨ããä¿è¨¼ã¯ããã¾ãããããã¯ä»æ§æ¸ã«æºæ ãã¦ããã以ä¸ã®ããã«è¨è¼ããã¦ãã¾ãããéç¥ãåºç¤ã¨ãªãéç¥ãã©ãããã©ã¼ã ã«ãã£ã¦ãã¾ãã¯ã¦ã¼ã¶ã¼ã«ãã£ã¦éããããã¨ãããã®éç¥ã®ããã®éããã¹ããããå®è¡ããªããã°ãªãã¾ãããã
Notification ã¤ãã³ãNotifications API ã®ä»æ§ã§ã¯ãNotification
ã®ã¤ã³ã¹ã¿ã³ã¹ã§çºçããã¤ãã³ãã 2 ã¤å®ç¾©ãã¦ãã¾ã:
click
ã¦ã¼ã¶ã¼ãéç¥ãã¯ãªãã¯ããã¨ãã«çºçãã¾ãã
close
éç¥ãéããããã¨ãã«ä¸åº¦çºçãã¾ãã
error
éç¥ã§åé¡ãçºçããã¨ãã«çºçãã¾ããé常ããªãããã®çç±ã§éç¥ã表示ãããªãã£ãããã§ãã
show
éç¥ãã¦ã¼ã¶ã¼ã«è¡¨ç¤ºãããã¨ãã«çºçãã¾ãã
ãããã®ã¤ãã³ã㯠onclick
, onclose
, onerror
, onshow
ã®åãã³ãã©ã¼ã使ç¨ãã¦è¿½è·¡ãããã¨ãã§ãã¾ãã Notification
㯠EventTarget
ãç¶æ¿ãã¦ãã¾ãã®ã§ addEventListener()
ã¡ã½ããã使ç¨ãããã¨ãã§ãã¾ãã
ã¦ã¼ã¶ã¼ãçæéã«å¤ãã®éç¥ãåãåããã¨ã¯ãé常æã¾ããããã¾ãããä¾ãã°ã¡ãã»ã³ã¸ã£ã¼ã¢ããªãã¡ãã»ã¼ã¸ãåãåããã³ã«éç¥ãè¡ã£ã¦ãããã大éã«ãªã£ããã©ããªãã§ããããã大éã®éç¥ã«ããã¹ãã ç¶æ ãé¿ãããããæºåä¸ã®éç¥ãã¥ã¼ã夿´ã㦠1 ã¤ä»¥ä¸ã®æªè¡¨ç¤ºéç¥ãæ°ãéç¥ã§ç½®ãæãããã¨ãã§ãã¾ãã
ãããè¡ãããã«ãä»»æã®æ°ããªéç¥ã«ã¿ã°ä»ããããã¨ãã§ãã¾ãããã§ã«åãã¿ã°ãã¤ãã¦ããéç¥ãã¾ã 表示ããã¦ããªãå ´åã¯ãæ°ããéç¥ã以åã®éç¥ãç½®ãæãã¾ããåãã¿ã°ãã¤ãã¦ããéç¥ããã§ã«è¡¨ç¤ºããã¦ããå ´åã¯ãåã®éç¥ãéãããã¦æ°ããéç¥ã表示ããã¾ãã
ã¿ã°ã®ä¾ä»¥ä¸ã®åºæ¬ç㪠HTML ãæ³å®ãã¦ãã ããã
<button id="notify">Notify me!</button>
<section id="demo-logs"></section>
#demo-logs {
width: 90%;
height: 100px;
background-color: #ddd;
overflow-x: auto;
}
以ä¸ã®æ¹æ³ã§ãè¤æ°ã®éç¥ãæ±ããã¨ãå¯è½ã§ãã
const demoLogs = document.querySelector("#demo-logs");
window.addEventListener("load", () => {
const button = document.querySelector("#notify");
button.addEventListener("click", () => {
if (Notification?.permission === "granted") {
demoLogs.innerText += `The site has permission to show notifications. Showing notifications.\n`;
// If the user agreed to get notified
// Let's try to send ten notifications
let i = 0;
// Using an interval cause some browsers (including Firefox) are blocking notifications if there are too much in a certain time.
const interval = setInterval(() => {
// Thanks to the tag, we should only see the "Hi no 9 from MDN." notification
const n = new Notification(`Hi no ${i} from MDN.`, {
tag: "soManyNotification",
});
if (i === 9) {
clearInterval(interval);
}
i++;
}, 200);
} else if (Notification && Notification.permission !== "denied") {
demoLogs.innerText += "Requesting notification permission.\n";
// If the user hasn't told if they want to be notified or not
// Note: because of Chrome, we are not sure the permission property
// is set, therefore it's unsafe to check for the "default" value.
Notification.requestPermission().then((status) => {
// If the user said okay
if (status === "granted") {
demoLogs.innerText +=
"User granted the permission. Sending notifications.\n";
let i = 0;
// Using an interval cause some browsers (including Firefox) are blocking notifications if there are too much in a certain time.
const interval = setInterval(() => {
// Thanks to the tag, we should only see the "Hi! 9" notification
const n = new Notification(`Message no ${i} from MDN.`, {
tag: "soManyNotification",
});
if (i === 9) {
clearInterval(interval);
}
i++;
}, 200);
} else {
// Otherwise, we can fallback to a regular modal alert
demoLogs.innerText += `User denied the permission request.\n`;
}
});
} else {
// If the user refuses to get notified, we can fallback to a regular modal alert
demoLogs.innerText += `The site does not have permission to show notifications.\n`;
}
});
});
çµæ
ä¸è¨ã®ä¾ããã¹ãããã«ã¯ãã¦ã§ããµã¤ã https://live.mdnplay.dev
ã®éç¥è¨å®ã夿´ãã¦ãã ããã
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4