Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
Ajout des service workers hors-ligne
Browse files Browse the repository at this point in the history
C'est RÉ-VO-LU-TIO-NNAIRE !!!
Cache des pages "emploi du temps", "devoirs" et "actualités" plus l'interface pour le moment
  • Loading branch information
ecnivtwelve committed Sep 7, 2022
1 parent 99e2afd commit 719d18d
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 45 deletions.
24 changes: 15 additions & 9 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ window.addEventListener('beforeinstallprompt', (e) => {
});

if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/js/sw.js');
navigator.serviceWorker.register('/papillonWorker.js');
};

// Vérifie qu'une font custom n'est pas appliquée
Expand Down Expand Up @@ -219,16 +219,18 @@ let userEverything;

function loadPronoteData() {
progressStart();
$.get(`https://ams01.pronote.plus/user?token=${token}&rand=${uuidv4()}`, function(data, success) {

fetch(`https://ams01.pronote.plus/user?token=${token}`)
.then((resp) => resp.json())
.then(function(data) {

// Si la requête fail, rafrachir le token
if (JSON.parse(data).message !== undefined) {
if (data.message !== undefined) {
tokenRefreshBkg()
}
progressEnd();

// Décode le JSON depuis l'API
let resp = JSON.parse(data).data.user;
let resp = data.data.user;
userEverything = resp;

// Récupère le prénom à partir des données de l'utilisateur
Expand Down Expand Up @@ -479,8 +481,10 @@ function tokenRefreshBkg() {
let authURL = auth[0];
let authENT = auth[3];

$.get(`https://ams01.pronote.plus/auth?url=${authURL}&username=${authUsername}&password=${authPasswordUnsecure}&cas=${authENT}&rand=${uuidv4()}`, function(data) {
let resp = JSON.parse(data);
fetch(`https://ams01.pronote.plus/auth?url=${authURL}&username=${authUsername}&password=${authPasswordUnsecure}&cas=${authENT}`)
.then((resp) => resp.json())
.then(function(data) {
let resp = data;

tries++;

Expand Down Expand Up @@ -541,8 +545,10 @@ function openApp() {
buttonNext.click()
} else if (dateString.includes("samedi")) {
// requête pour checker si y'a cours le samedi
$.get(`https://ams01.pronote.plus/edt?token=${token}&from=${from}&rand=${uuidv4()}`, function( data ) {
if(JSON.parse(data).data.timetable === null) {
fetch(`https://ams01.pronote.plus/edt?token=${token}&from=${from}`)
.then((resp) => resp.json())
.then(function(data) {
if(data.data.timetable === null) {
// avance de 2 jours
buttonNext.click()
buttonNext.click()
Expand Down
17 changes: 0 additions & 17 deletions js/sw.js

This file was deleted.

2 changes: 1 addition & 1 deletion manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Pronote+",
"start_url": "https://v3.pronote.plus",
"start_url": "/",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#2BA873",
Expand Down
92 changes: 92 additions & 0 deletions papillonWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2016 Google Inc. 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.
*/

// Names of the two caches used in this version of the service worker.
// Change to v2, etc. when you update any of the local resources, which will
// in turn trigger the install event again.
const PRECACHE = 'precache-v1';
const RUNTIME = 'runtime';

// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
'index.html',
'/',
'/css/main.css',
'/js/app.js',
'papillonWorker.js',
'/views/about.html',
'/views/absences.html',
'/views/edt.html',
'/views/evaluations.html',
'/views/files.html',
'/views/hw.html',
'/views/info.html',
'/views/mark.html',
'/views/marks.html',
'/views/news.html',
'/views/theme.html',
'/views/update.html',
'/views/user.html',
'/views/userdata.html',
'/login/index.html',
'/login/login.css',
'/login/login.js'
];

// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', event => {
event.waitUntil(
caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
);
});

// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', event => {
const currentCaches = [PRECACHE, RUNTIME];
event.waitUntil(
caches.keys().then(cacheNames => {
return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
}).then(cachesToDelete => {
return Promise.all(cachesToDelete.map(cacheToDelete => {
return caches.delete(cacheToDelete);
}));
}).then(() => self.clients.claim())
);
});

// The fetch handler serves responses for same-origin resources from a cache.
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', event => {
// Skip cross-origin requests, like those for Google Analytics.
if (1 == 1) {
event.respondWith(
caches.match(event.request).then(cachedResponse => {
if (cachedResponse) {
return cachedResponse;
}

return caches.open(RUNTIME).then(cache => {
return fetch(event.request).then(response => {
// Put a copy of the response in the runtime cache.
return cache.put(event.request, response.clone()).then(() => {
return response;
});
});
});
})
);
}
});
19 changes: 8 additions & 11 deletions views/edt.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@
return (dt.getMinutes() < 10 ? '0' : '') + dt.getMinutes();
}

if(localStorage.getItem("cache_edt") !== null){
// parse_Edt(JSON.parse(localStorage.getItem("cache_edt")));
mainView_Edt();
}
else {
mainView_Edt();
}
mainView_Edt();

function mainView_Edt() {
let from = rn.toISOString().split("T")[0]
progressStart();
$.get(`https://ams01.pronote.plus/edt?token=${token}&from=${from}&rand=${uuidv4()}`, function( data ) {

fetch(`https://ams01.pronote.plus/edt?token=${token}&from=${from}`)
.then((resp) => resp.json())
.then(function(data) {

if(JSON.parse(data).errors !== undefined) {
if(JSON.parse(data).code == 2) {
if(data.errors !== undefined) {
if(data.code == 2) {
Toastify({
text: "Vous avez envoyé trop de demandes à Pronote, veuillez patienter quelques minutes.",
gravity: "top",
Expand Down Expand Up @@ -62,7 +59,7 @@
try{
$('#ListeCours').html(``);

let resp = JSON.parse(data).data.timetable;
let resp = data.data.timetable;

if(resp == null) {
if(EdtRefLimit == 0) {
Expand Down
15 changes: 11 additions & 4 deletions views/hw.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
$('#ListeDevoirs').append(`<div class="devoirDay fst">${dayName}</div>`);

progressStart();
$.get(`https://ams01.pronote.plus/hw?token=${token}&from=${from}&rand=${uuidv4()}`, function( data ) {
if(JSON.parse(data).message !== undefined) {

fetch(`https://ams01.pronote.plus/hw?token=${token}&from=${from}`)
.then((resp) => resp.json())
.then(function(data) {

if(data.message !== undefined) {
tokenRefreshBkg()
}
progressEnd();

let resp = JSON.parse(data).data.homeworks;
let resp = data.data.homeworks;

for(hw in resp) {
let classes = "";
Expand Down Expand Up @@ -76,7 +80,10 @@ <h3>🥳 Pas de travail à faire</h3>
$('#ListeDevoirs').append(`<div class="devoirDay">${dayName}</div>`);

progressStart();
$.get(`https://ams01.pronote.plus/hw?token=${token}&from=${from}&rand=${uuidv4()}`, function( data ) {

fetch(`https://ams01.pronote.plus/hw?token=${token}&from=${from}`)
.then((resp) => resp.json())
.then(function(data) {
if(JSON.parse(data).message !== undefined) {
tokenRefreshBkg()
}
Expand Down
9 changes: 6 additions & 3 deletions views/news.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
half.setDate(half.getDate() + 1);
let from = half.toISOString().split("T")[0]
progressStart(4);
$.get(`https://ams01.pronote.plus/news?token=${token}&rand=${uuidv4()}`, function( data ) {
if(JSON.parse(data).errors !== undefined) {

fetch(`https://ams01.pronote.plus/news?token=${token}`)
.then((resp) => resp.json())
.then(function(data) {
if(data.errors !== undefined) {
refreshToken();
}
progressEnd();

let resp = JSON.parse(data).data.infos;
let resp = data.data.infos;

if (resp !== null) {

Expand Down

0 comments on commit 719d18d

Please sign in to comment.