Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

service_workers.html 1.9KB

před 5 roky
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <script>
  2. /* Service workers */
  3. if (navigator.serviceWorker) {
  4. window.addEventListener('load', function () {
  5. var selector = 'a[href^="/david/cache/"], a[rel=prev], a[rel=next]'
  6. function sendLinks (selector) {
  7. var links = [].slice.call(document.querySelectorAll(selector)).map(function (link) {
  8. return link.getAttribute('href')
  9. })
  10. links.push(location.pathname) // Put the current page in cache too.
  11. navigator.serviceWorker.controller.postMessage({ links: links })
  12. }
  13. navigator.serviceWorker.getRegistration()
  14. .then(function (registration) {
  15. if (!registration || !navigator.serviceWorker.controller) {
  16. return navigator.serviceWorker.register('/serviceworker.js')
  17. .then(navigator.serviceWorker.ready)
  18. .then(function () {
  19. console.log('[ServiceWorker] Ready to go!')
  20. })
  21. .catch(console.error.bind(console))
  22. } else {
  23. console.log('[ServiceWorker] Send links via registration')
  24. sendLinks(selector)
  25. }
  26. })
  27. navigator.serviceWorker.addEventListener('controllerchange', function () {
  28. console.log('[ServiceWorker] Send links via controller change')
  29. sendLinks(selector)
  30. })
  31. navigator.serviceWorker.addEventListener('message', function (event) {
  32. var link = document.querySelector('a[href="' + event.data.link + '"]')
  33. if (event.data.status && link) {
  34. link.style.backgroundColor = '#2d7474'
  35. link.style.color = '#f0f0ea'
  36. {% if (note and note.lang == 'en') or (article and article.lang == 'en') %}
  37. link.setAttribute('title', 'Put in cache for offline use')
  38. {% else %}
  39. link.setAttribute('title', 'En cache pour consultation sans connexion')
  40. {% endif %}
  41. }
  42. })
  43. })
  44. } else {
  45. console.warn('[ServiceWorker] No cache for old browsers.')
  46. }
  47. </script>