1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <script>
- /* Service workers */
- if (navigator.serviceWorker) {
- window.addEventListener('load', function () {
- var selector = 'a[href^="/david/cache/"], a[rel=prev], a[rel=next]'
- function sendLinks (selector) {
- var links = [].slice.call(document.querySelectorAll(selector)).map(function (link) {
- return link.getAttribute('href')
- })
- links.push(location.pathname) // Put the current page in cache too.
- navigator.serviceWorker.controller.postMessage({ links: links })
- }
- navigator.serviceWorker.getRegistration()
- .then(function (registration) {
- if (!registration || !navigator.serviceWorker.controller) {
- return navigator.serviceWorker.register('/serviceworker.js')
- .then(navigator.serviceWorker.ready)
- .then(function () {
- console.log('[ServiceWorker] Ready to go!')
- })
- .catch(console.error.bind(console))
- } else {
- console.log('[ServiceWorker] Send links via registration')
- sendLinks(selector)
- }
- })
- navigator.serviceWorker.addEventListener('controllerchange', function () {
- console.log('[ServiceWorker] Send links via controller change')
- sendLinks(selector)
- })
- navigator.serviceWorker.addEventListener('message', function (event) {
- var link = document.querySelector('a[href="' + event.data.link + '"]')
- if (event.data.status && link) {
- link.style.backgroundColor = '#2d7474'
- link.style.color = '#f0f0ea'
- {% if (note and note.lang == 'en') or (article and article.lang == 'en') %}
- link.setAttribute('title', 'Put in cache for offline use')
- {% else %}
- link.setAttribute('title', 'En cache pour consultation sans connexion')
- {% endif %}
- }
- })
- })
- } else {
- console.warn('[ServiceWorker] No cache for old browsers.')
- }
- </script>
|