Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ff2balex
Manage
Activity
Members
Labels
Plan
Issues
9
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lex gaMe
ff2balex
Commits
d288e537
Commit
d288e537
authored
4 months ago
by
Prénom Nom
Browse files
Options
Downloads
Patches
Plain Diff
correction activation/désactivation
parent
db4cf257
No related branches found
No related tags found
1 merge request
!9
Affichage stats
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/background/background.js
+104
-16
104 additions, 16 deletions
src/background/background.js
src/utils/stats.js
+13
-30
13 additions, 30 deletions
src/utils/stats.js
src/workers/pyodide_worker.js
+1
-1
1 addition, 1 deletion
src/workers/pyodide_worker.js
with
118 additions
and
47 deletions
src/background/background.js
+
104
−
16
View file @
d288e537
...
...
@@ -322,13 +322,12 @@ function initWorker() {
}}
}
//Initialisation
//Initialisation
du worker
initWorker
()
// ─────────────────────────────────────────────────────────────────────────────
// Écoute des messages de la popup et transmission au WebWorker
// ─────────────────────────────────────────────────────────────────────────────
browser
.
runtime
.
onMessage
.
addListener
(
async
(
message
,
sender
,
sendResponse
)
=>
{
console
.
log
(
"
[Background] Message reçu :
"
,
message
);
...
...
@@ -338,10 +337,16 @@ browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
if
(
message
.
command
===
"
toggle-stats
"
)
{
console
.
log
(
`[Background] Statistiques
${
message
.
isActive
?
"
activées
"
:
"
désactivées
"
}
`
);
//Valeur du storage : actif
const
{
isActive
}
=
message
;
await
browser
.
storage
.
local
.
set
({
isTrackingActive
:
isActive
});
// Transmettre l'activation des statistiques au WebWorker
worker
.
postMessage
({
command
:
"
toggle-stats
"
,
isActive
:
message
.
isActive
});
//Si on désactive les stats, on met aussi la valeur autoAdd false
if
(
!
isActive
)
{
await
browser
.
storage
.
local
.
set
({
autoAdd
:
false
});
//console.log("[Background] autoAdd désactivé car les statistiques ont été désactivées.");
//Vérifier et activer le tracking
checkAndUpdateTracking
();
}
}
if
(
message
.
command
===
"
pyodide-simplemma
"
)
{
...
...
@@ -349,25 +354,108 @@ browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
worker
.
postMessage
({
command
:
"
pyodide-simplemma
"
});
}
return
true
;
// Permet une réponse asynchrone si nécessaire
if
(
message
.
command
===
"
process-text
"
)
{
console
.
log
(
"
[Background] Envoi du texte au Worker pour traitement :
"
,
message
.
text
);
// // Récupérer les préférences utilisateur depuis le stockage local
// const { accessToken, trackedLanguages, threshold, autoAdd } = await browser.storage.local.get([
// "accessToken", "trackedLanguages", "threshold", "autoAdd"
// ]) || { trackedLanguages: [], threshold: 10, autoAdd: false };
// // Envoyer les données au Worker
// worker.postMessage({
// type: "process-text",
// text: message.text,
// accessToken,
// trackedLanguages,
// threshold,
// autoAdd
// });
}
return
true
;
});
// ─────────────────────────────────────────────────────────────────────────────
// Écouter les réponses du WebWorker
// ─────────────────────────────────────────────────────────────────────────────
worker
.
onmessage
=
(
event
)
=>
{
worker
.
onmessage
=
async
(
event
)
=>
{
console
.
log
(
"
[Background] Message du WebWorker :
"
,
event
.
data
);
//Pyodide et Simplemma
prêts
//
Si
Pyodide et Simplemma
sont prêts : AJOUTER UNE INFO VISUELLE
if
(
event
.
data
.
type
===
"
pyodide-simplemma
"
&&
event
.
data
.
status
===
"
success
"
)
{
//Notifier tous les onglets
browser
.
tabs
.
query
({}).
then
((
tabs
)
=>
{
tabs
.
forEach
((
tab
)
=>
{
browser
.
tabs
.
sendMessage
(
tab
.
id
,
{
command
:
"
pyodide-simplemma-ready
"
})
.
catch
((
error
)
=>
console
.
warn
(
`[Background] Impossible d'envoyer un message à l'onglet
${
tab
.
id
}
:
${
error
}
`
));
});
});
console
.
log
(
"
[Background] Pyodide et Simplemma prêts. Mise à jour de l'état.
"
);
// Stocker l'information dans le localStorage
await
browser
.
storage
.
local
.
set
({
pyodideSimplemmaReady
:
true
});
checkAndUpdateTracking
();
}
};
//Démarrer le tracking uniquement lorsque pyodide et simplemma sont prêts
//Injecter le tracking partout (nouveaux onglets et actifs)
// ─────────────────────────────────────────────────────────────────────────────
// Statistiques : Vérification et activation/désactivation du tracking
// ─────────────────────────────────────────────────────────────────────────────
async
function
checkAndUpdateTracking
()
{
const
{
isTrackingActive
,
pyodideSimplemmaReady
}
=
await
browser
.
storage
.
local
.
get
([
"
isTrackingActive
"
,
"
pyodideSimplemmaReady
"
]);
if
(
isTrackingActive
&&
pyodideSimplemmaReady
)
{
console
.
log
(
"
[Background] Activation du tracking.
"
);
notifyAllTabs
({
command
:
"
activate-stats
"
});
}
else
{
console
.
log
(
"
[Background] Désactivation du tracking.
"
);
notifyAllTabs
({
command
:
"
deactivate-stats
"
});
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Statistiques : Gestion des onglets
// ─────────────────────────────────────────────────────────────────────────────
// Fonction pour envoyer des messages à TOUS les onglets
async
function
notifyAllTabs
(
message
)
{
browser
.
tabs
.
query
({}).
then
((
tabs
)
=>
{
tabs
.
forEach
((
tab
)
=>
{
browser
.
tabs
.
sendMessage
(
tab
.
id
,
message
)
.
catch
((
error
)
=>
console
.
warn
(
`[Background] Impossible d'envoyer un message à l'onglet
${
tab
.
id
}
:
${
error
}
`
));
});
});
}
// Gestion des nouveaux onglets
browser
.
tabs
.
onUpdated
.
addListener
((
tabId
,
changeInfo
,
tab
)
=>
{
if
(
changeInfo
.
status
===
"
complete
"
)
{
browser
.
storage
.
local
.
get
([
"
isTrackingActive
"
,
"
pyodideSimplemmaReady
"
]).
then
(({
isTrackingActive
,
pyodideSimplemmaReady
})
=>
{
if
(
isTrackingActive
&&
pyodideSimplemmaReady
)
{
browser
.
tabs
.
executeScript
(
tabId
,
{
file
:
"
src/utils/stats.js
"
})
.
then
(()
=>
console
.
log
(
`[Background] stats.js injecté dans le nouvel onglet
${
tabId
}
`
))
.
catch
((
error
)
=>
console
.
warn
(
`[Background] Impossible d'injecter stats.js dans
${
tabId
}
:
${
error
}
`
));
setTimeout
(()
=>
{
browser
.
tabs
.
sendMessage
(
tabId
,
{
command
:
"
activate-stats
"
})
.
catch
((
error
)
=>
console
.
warn
(
`[Background] Impossible d'envoyer 'activate-stats' à
${
tabId
}
:
${
error
}
`
));
},
1000
);
}
});
}
});
// Gestion du tracking pour les nouveaux onglets
browser
.
tabs
.
onCreated
.
addListener
((
tab
)
=>
{
setTimeout
(()
=>
{
browser
.
storage
.
local
.
get
([
"
isTrackingActive
"
,
"
pyodideSimplemmaReady
"
]).
then
(({
isTrackingActive
,
pyodideSimplemmaReady
})
=>
{
if
(
isTrackingActive
&&
pyodideSimplemmaReady
)
{
console
.
log
(
`[Background] Nouveau onglet détecté (
${
tab
.
id
}
), activation du tracking.`
);
browser
.
tabs
.
sendMessage
(
tab
.
id
,
{
command
:
"
activate-stats
"
})
.
catch
((
error
)
=>
console
.
warn
(
`[Background] Impossible d'activer le tracking sur
${
tab
.
id
}
:
${
error
}
`
));
}
});
},
1000
);
});
// ─────────────────────────────────────────────────────────────────────────────
// Écoute des modifications du stockage et mise à jour du tracking
// ─────────────────────────────────────────────────────────────────────────────
browser
.
storage
.
onChanged
.
addListener
(
async
(
changes
,
area
)
=>
{
if
(
area
===
"
local
"
&&
(
changes
.
isTrackingActive
||
changes
.
pyodideSimplemmaReady
))
{
checkAndUpdateTracking
();
}
});
This diff is collapsed.
Click to expand it.
src/utils/stats.js
+
13
−
30
View file @
d288e537
...
...
@@ -7,18 +7,11 @@
let
isTrackingActive
=
false
;
let
scrollListenerAttached
=
false
;
const
seenContent
=
new
Set
();
let
simplemmaReady
=
false
;
/**
* Fonction pour extraire le texte visible et l'envoyer au background script.
*/
function
trackVisibleContent
()
{
// pas de tracking tant que Pyodide n’est pas prêt
if
(
!
simplemmaReady
)
{
console
.
log
(
"
[Stats] Pyodide n'est pas encore prêt, attente...
"
);
return
;
}
let
selectors
=
"
p, h1, h2, h3, h4, h5, h6, ul, ol, li, table, tr, td, th, blockquote
"
;
// Sélecteurs spécifiques à exclure sur Wikipedia et d'autres sites
...
...
@@ -44,37 +37,27 @@
if
(
text
.
length
>
10
&&
!
seenContent
.
has
(
text
))
{
seenContent
.
add
(
text
);
console
.
log
(
"
[Stats] Envoi du texte filtré au background.js :
"
,
text
);
browser
.
runtime
.
sendMessage
({
command
:
"
process-text
"
,
text
:
text
});
}
}
});
}
function
cleanText
(
text
)
{
// Supprimer les puces et symboles inutiles
text
=
text
.
replace
(
/
[\u
2022
\u
00b7•·■◆▪▸▹▶►▻⇨
]
/g
,
"
"
);
// Supprime puces et flèches
text
=
text
.
replace
(
/
[\t\n\r]
+/g
,
"
"
);
// Supprime les sauts de ligne inutiles
text
=
text
.
replace
(
/
\s{2,}
/g
,
"
"
);
// Remplace plusieurs espaces par un seul
// Essayer d'extraire le contenu utile des tableaux (ex: "1 | Chat | Animal" → "1 Chat Animal")
text
=
text
.
replace
(
/
(\|
|
\t)
+/g
,
"
"
);
// Remplace les séparateurs de tableau par un espace
text
=
text
.
replace
(
/
(\s
*-+
\s
*
)
+/g
,
"
"
);
// Supprime les lignes de séparation des tableaux
// Supprimer les espaces en début et fin de texte
text
=
text
.
trim
();
return
text
;
}
// STATISTIQUES ------------------------------------------------------------------------------------------
async
function
initializeTrackingState
()
{
const
{
isTrackingActive
:
storedState
}
=
await
browser
.
storage
.
local
.
get
(
"
isTrackingActive
"
);
isTrackingActive
=
storedState
??
false
;
console
.
log
(
"
[Stats] État initial récupéré :
"
,
isTrackingActive
);
if
(
isTrackingActive
)
{
startTracking
();
}
else
{
...
...
@@ -137,21 +120,21 @@
}
}
// GESTION DES MESSAGES
-
-----------------------------------------------------------------------------------------
// GESTION DES MESSAGES
envoyé depuis le background
-----------------------------------------------------------------------------------------
browser
.
runtime
.
onMessage
.
addListener
((
message
)
=>
{
if
(
message
.
command
===
"
pyodide-simplemma-ready
"
)
{
console
.
log
(
"
[Stats] Pyodide et Simplemma prêt, démarrage du scraping :
"
);
simplemmaReady
=
true
;
initializeTrackingState
();
// démarre le tracking une fois Pyodide prêt
if
(
message
.
command
===
"
activate-stats
"
)
{
console
.
log
(
"
[Stats] Activation demandée par le background script.
"
);
startTracking
();
}
if
(
message
.
command
===
"
update-stats
"
)
{
if
(
message
.
isActive
&&
simplemmaReady
)
{
startTracking
();
}
else
{
stopTracking
();
}
if
(
message
.
command
===
"
deactivate-stats
"
)
{
console
.
log
(
"
[Stats] Désactivation du tracking.
"
);
stopTracking
();
}
});
// Initialisation
// initializeTrackingState();
// Envoyer les données au WebWorker pour qu'il traite le texte scrappé
})();
This diff is collapsed.
Click to expand it.
src/workers/pyodide_worker.js
+
1
−
1
View file @
d288e537
...
...
@@ -77,7 +77,7 @@ self.onmessage = async (event) => {
}
}
//
Activer/désactiver les statistiques (
stats.js)
//
Traitement du texte (envoyé par
stats.js)
if
(
data
.
command
===
"
toggle-stats
"
)
{
console
.
log
(
`[WebWorker] Statistiques
${
data
.
isActive
?
"
activées
"
:
"
désactivées
"
}
`
);
return
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment