Skip to content
Snippets Groups Projects
Commit 77555e74 authored by Prénom Nom's avatar Prénom Nom
Browse files

intégration appels api #1

parent d14f255f
No related branches found
No related tags found
No related merge requests found
api.js 0 → 100644
console.log("api.js chargé correctement");
// Fonction pour récupérer les lexiques de l'utilisateur
async function getUserLexicons(authToken) {
const lexiconsApiUrl = 'https://babalex.lezinter.net/api/user/lexicons';
try {
const response = await fetch(lexiconsApiUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Erreur lors de la récupération des lexiques : ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error('Erreur lors de la récupération des lexiques :', error);
throw error;
}
}
// Fonction pour récupérer les graphies d'un lexique donné
async function getLexiconEntries(authToken, idLexicon) {
const entriesApiUrl = `https://babalex.lezinter.net/api/lexicon/entries/${idLexicon}`;
try {
const response = await fetch(entriesApiUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Erreur lors de la récupération des graphies : ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error('Erreur lors de la récupération des graphies :', error);
throw error;
}
}
// Fonction pour obtenir les informations d'une graphie
async function getWordInfo(authToken, word) {
const definitionApiUrl = `https://babalex.lezinter.net/api/entry/search?graphy=${encodeURIComponent(word)}&language=fr&target_lex=3&target_lex=4`;
try {
const response = await fetch(definitionApiUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Erreur lors de la récupération de la définition : ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error('Erreur lors de la récupération de la définition :', error);
throw error;
}
}
// Fonction pour obtenir les définitions
function getDefinition(data) {
// Vérifier que les données sont bien structurées
if (!Array.isArray(data)) {
console.warn('Les données fournies ne contiennent pas de définitions.');
return [];
}
//Liste qui contient les id / définitions
const definitions = [];
// Parcours de chaque entrée pour extraire les définitions
data.forEach((entry) => {
if (entry.attributes && entry.attributes.Items) {
entry.attributes.Items.forEach((item) => {
if (item.Sense && Array.isArray(item.Sense.Definitions)) {
item.Sense.Definitions.forEach((def) => {
definitions.push({
id: def.id || 'ID non disponible',
definition: def.Def || 'Définition non disponible',
});
});
}
});
}
});
return definitions;
}
// Fonction pour obtenir une définition depuis le Wiktionnaire
async function getWiktionaryDefinition(authToken, word) {
const wiktionaryApiUrl = `https://babalex.lezinter.net/api/wiktionary/search?graphy=${encodeURIComponent(word)}&language=fr`;
try {
const response = await fetch(wiktionaryApiUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Erreur lors de la récupération de la définition depuis le Wiktionnaire : ${response.statusText}`);
}
const data = await response.json();
// Extraire les définitions et leurs IDs
const definitions = [];
if (Array.isArray(data)) {
data.forEach((entry) => {
if (entry.science && entry.science.senses) {
for (const [senseKey, senseValue] of Object.entries(entry.science.senses)) {
if (senseValue.Definitions) {
senseValue.Definitions.forEach((def) => {
definitions.push({
id: def.id || 'ID non disponible',
definition: def.definition || 'Définition non disponible',
});
});
}
}
}
});
}
return definitions;
} catch (error) {
console.error('Erreur lors de la récupération de la définition depuis le Wiktionnaire :', error);
throw error;
}
}
// METTRE LES FONCTIONS GLOBALES
window.getUserLexicons = getUserLexicons;
window.getLexiconEntries = getLexiconEntries;
window.getWordInfo = getWordInfo;
window.getDefinition = getDefinition;
window.getWiktionaryDefinition = getWiktionaryDefinition;
\ No newline at end of file
token.js 0 → 100644
console.log("token.js chargé correctement");
// Fonction pour lire le contenu du fichier token
async function readTokenFile() {
const tokenFilePath = browser.runtime.getURL('token.txt');
try {
const response = await fetch(tokenFilePath);
if (!response.ok) {
throw new Error(`Erreur lors de la lecture du fichier token : ${response.statusText}`);
}
const token = (await response.text()).trim();
console.log('Token chargé :', token);
return token;
} catch (error) {
console.error('Erreur lors de la lecture du fichier token :', error);
throw error;
}
}
// Exposer la fonction en tant que globale
window.readTokenFile = readTokenFile;
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJwcmlzbXMiLCJqdGkiOiJlMjk1ZmI0N2E4NGM4Y2Y2YTZmNDM2M2QxMGU0ZDMwNmU4NGYwOWQ1MDVkOTcxNzNkNzg1NTI4MWQxYzE3NjYyMzJkMGRjNmQ2ZGVkNWMyYSIsImlhdCI6MTczNzA4Njk5Ny4xMjcxMDgsIm5iZiI6MTczNzA4Njk5Ny4xMjcxMjksImV4cCI6MTczNzA5MDU5Ny4xMDEzMiwic3ViIjoiYXNvdW5hbGF0aCIsInNjb3BlcyI6WyJlbWFpbCIsInBzZXVkbyIsInV1aWQiXSwia2lkIjoiMSIsImN1c3RvbSI6eyJmb28iOiJiYXIifX0.E0ApQUZLsJShxq94jZOyt5XHZS4C-1xx4Nabzp5jUw0W7GE2FpzRhrUSC2DVXIu5oCw_LOh_uuv0kkBRD2YjsdFeCn3NBoJP_2sz8vJLzutfeR7Fl4MF166IM8RPYAT14werTQy82MUUXcvKli8E12y7SGM7qwwq5c7I_C3eNr2NJ_S-ivj_1JKZIv4vqsCR4NHk4MJC8Wmt0VDXBJuX5LUgAnvHBSxiEFdrQm7vuKg-nXk33biGYjMQAFepJhr-Oax2lvs-OwC-UuNOV87vpmr79umX4fRdA2USFOV8B_ro25LqJq6vugXAYCCLjXD4e0fLwDUhjzkFfXXslAqX7A
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment