Skip to content

Commit

Permalink
test: sw
Browse files Browse the repository at this point in the history
  • Loading branch information
freeshineit committed May 31, 2024
1 parent 5bdba55 commit 4b8a4f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
35 changes: 18 additions & 17 deletions public/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,36 @@
</head>

<body>
<h3>Hello 这是一个可以安装到桌面的网页</h3>
<h3 onclick="showNotification()">Hello 这是一个可以安装到桌面的网页</h3>
<div id="installContainer">
用谷歌浏览器打开,看一下地址栏后面有一个安装的图标,安装一下试试吧
</div>
</body>
<script>
// 检测浏览器是否支持SW
if (navigator.serviceWorker != null) {

function showNotification() {
Notification.requestPermission().then((result) => {
if (result === "granted") {
navigator.serviceWorker.ready.then((registration) => {
registration.showNotification("Vibration Sample", {
body: "Buzz! Buzz!",
icon: "./logo.png",
vibrate: [200, 100, 200, 100, 200, 100, 200],
tag: "vibration-sample",
});
});
}
});
}

navigator.serviceWorker.register('./sw.js')
.then(function (registartion) {
console.log('支持sw:', registartion.scope)
showNotification()
})

// function showNotification() {
Notification.requestPermission().then((result) => {
if (result === "granted") {
navigator.serviceWorker.ready.then((registration) => {
registration.showNotification("Vibration Sample", {
body: "Buzz! Buzz!",
icon: "../images/touch/chrome-touch-icon-192x192.png",
vibrate: [200, 100, 200, 100, 200, 100, 200],
tag: "vibration-sample",
});
});
}
});
// }


} else {
console.log('不支持SW:')
}
Expand Down
Binary file added public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
importScripts("./service-worker.js");
var cacheStorageKey = "190pwa";
var cacheList = ["/", "index.html", "main.css", "logo.png"];
var cacheList = ["/", "./demo.html", "./main.css", "./logo.png"];
self.addEventListener("install", (e) => {
e.waitUntil(
caches
.open(cacheStorageKey)
.then((cache) => cache.addAll(cacheList))
.then((cache) => cache?.addAll?.(cacheList))
.then(() => self.skipWaiting())
);
});
Expand Down

0 comments on commit 4b8a4f6

Please sign in to comment.