diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b6257f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +tchap.py +.venv/ \ No newline at end of file diff --git a/cat-lol.svg b/cat-lol.svg new file mode 100644 index 0000000..73ae8d2 --- /dev/null +++ b/cat-lol.svg @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cat-tears.svg b/cat-tears.svg index 73ae8d2..f1fbe1c 100644 --- a/cat-tears.svg +++ b/cat-tears.svg @@ -4,103 +4,83 @@ viewBox="0 0 128 128" style="enable-background:new 0 0 128 128;" xml:space="preserve"> - - - - - - - + + + + - - - - + + + + + + + + + + - - - - + + - - + - + + + + + + + - - - - - - - - - - - - - - - - + - - + + + - - - + - - + + + - - - - diff --git a/index.html b/index.html index 318a679..09787bd 100644 --- a/index.html +++ b/index.html @@ -193,17 +193,37 @@ formulaire.addEventListener('submit', async (e) => { conversation.appendChild(citation); formulaire.reset(); - const paragraphe = document.createElement('p'); + let paragraphe = document.createElement('p'); paragraphe.setAttribute("aria-busy", "true"); conversation.appendChild(paragraphe); conversation.scrollTop = conversation.scrollHeight; - const requete = await fetch(formulaire.action, { - method: formulaire.method, - body: formulaireData - }); paragraphe.setAttribute('aria-busy', 'true'); - const reponse = await requete.text(); + let reponse + try { + const requete = await fetch(formulaire.action, { + method: formulaire.method, + body: formulaireData + }); + reponse = await requete.text(); + } catch (error) { + paragraphe.setAttribute('aria-busy', 'false'); + + paragraphe = document.createElement('p'); + paragraphe.innerHTML = "😿 Je ne suis pas disponible pour le moment. Merci de laisser un message !"; + favicon.href = 'cat-tears.svg'; + conversation.appendChild(paragraphe); + document.scrollingElement.scrollBy({top: window.visualViewport.height, behavior: 'smooth'}); + + bouton_envoyer.disabled = false; + bouton_envoyer.setAttribute("aria-busy", false); + bouton_envoyer.innerHTML = bouton_envoyer_innerHTML; + question.focus(); + question.onfocus(); + + return + } + paragraphe.setAttribute('aria-busy', 'false'); nb_reponses++; @@ -214,6 +234,8 @@ formulaire.addEventListener('submit', async (e) => { let t = 0; Array.from(reponse).forEach((lettre, i) => { + t += 100 * Math.random() + if (lettre == " ") t += 100 setTimeout(() => { if (lettre == "\n") { paragraphe.innerHTML += "
"; @@ -222,12 +244,12 @@ formulaire.addEventListener('submit', async (e) => { } document.scrollingElement.scrollBy({top: window.visualViewport.height, behavior: 'smooth'}); - }, t += 100 * Math.random()); + }, t); }); setTimeout(() => { const paragraphe = document.createElement('p'); paragraphe.innerHTML = (nb_reponses % 5 ? '😸' : '😹') + ' Voulez-vous que je réponde à une autre question ?'; - favicon.href = (nb_reponses % 5 ? 'cat-troll.svg' : 'cat-tears.svg'); + favicon.href = (nb_reponses % 5 ? 'cat-troll.svg' : 'cat-lol.svg'); conversation.appendChild(paragraphe); document.scrollingElement.scrollBy({top: window.visualViewport.height, behavior: 'smooth'}); @@ -335,6 +357,8 @@ window.onresize = function() { } window.visualViewport?.addEventListener('resize', window.onresize); window.onresize(); + +navigator?.serviceWorker.register('service-worker.js') diff --git a/service-worker.js b/service-worker.js new file mode 100644 index 0000000..b3e69ed --- /dev/null +++ b/service-worker.js @@ -0,0 +1,86 @@ +/* +Copyright 2015, 2019, 2020 Google LLC. All Rights Reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +// Incrementing OFFLINE_VERSION will kick off the install event and force +// previously cached resources to be updated from the network. +const OFFLINE_VERSION = 1; +const CACHE_NAME = "offline"; +// Customize this with a different URL if needed. +const OFFLINE_URL = "index.html"; + +self.addEventListener("install", (event) => { + event.waitUntil( + (async () => { + const cache = await caches.open(CACHE_NAME); + // Setting {cache: 'reload'} in the new request will ensure that the + // response isn't fulfilled from the HTTP cache; i.e., it will be from + // the network. + await cache.add(new Request(OFFLINE_URL, { cache: "reload" })); + })() + ); + // Force the waiting service worker to become the active service worker. + self.skipWaiting(); +}); + +self.addEventListener("activate", (event) => { + event.waitUntil( + (async () => { + // Enable navigation preload if it's supported. + // See https://developers.google.com/web/updates/2017/02/navigation-preload + if ("navigationPreload" in self.registration) { + await self.registration.navigationPreload.enable(); + } + })() + ); + + // Tell the active service worker to take control of the page immediately. + self.clients.claim(); +}); + +self.addEventListener("fetch", (event) => { + // We only want to call event.respondWith() if this is a navigation request + // for an HTML page. + if (event.request.mode === "navigate") { + event.respondWith( + (async () => { + try { + // First, try to use the navigation preload response if it's supported. + const preloadResponse = await event.preloadResponse; + if (preloadResponse) { + return preloadResponse; + } + + // Always try the network first. + const networkResponse = await fetch(event.request); + return networkResponse; + } catch (error) { + // catch is only triggered if an exception is thrown, which is likely + // due to a network error. + // If fetch() returns a valid HTTP response with a response code in + // the 4xx or 5xx range, the catch() will NOT be called. + console.log("Fetch failed; returning offline page instead.", error); + + const cache = await caches.open(CACHE_NAME); + const cachedResponse = await cache.match(OFFLINE_URL); + return cachedResponse; + } + })() + ); + } + + // If our if() condition is false, then this fetch handler won't intercept the + // request. If there are any other fetch handlers registered, they will get a + // chance to call event.respondWith(). If no fetch handlers call + // event.respondWith(), the request will be handled by the browser as if there + // were no service worker involvement. +});