Skip to content

Commit d25984e

Browse files
authored
Ajout d'une fonctionnalité pour ouvrir automatiquement le lien Europr… (#336)
1 parent 740a12d commit d25984e

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

ophirofox/content_scripts/europresse_search.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ async function onLoad() {
141141

142142
if (!await hasConsumable()) {
143143
console.log("(Ophirofox) No consumable found.");
144+
if (path.startsWith("/Search/Result")) {
145+
const auto_open_link = await getAutoOpenOption();
146+
if (auto_open_link) {
147+
const numberOfResul = document.querySelector('.resultOperations-count').textContent;
148+
if (numberOfResul === '1') {
149+
await readWhenOnlyOneResult();
150+
}
151+
}
152+
}
144153
return;
145154
}
146155

@@ -172,4 +181,49 @@ function ophirofoxRealoadOnExpired() {
172181
}
173182
}
174183

175-
onLoad();
184+
async function waitForElement(selector, callback, attempts = 0, maxAttempts = 10) {
185+
const element = document.querySelector(selector);
186+
if (element) {
187+
callback(element);
188+
return true; // Indique que l'élément a été trouvé
189+
} else if (attempts < maxAttempts) {
190+
await new Promise(resolve => setTimeout(resolve, 500)); // Attendre 0.5 seconde avant de réessayer
191+
return waitForElement(selector, callback, attempts + 1, maxAttempts);
192+
} else {
193+
console.error('Element not found after maximum attempts');
194+
return false; // Indique que l'élément n'a pas été trouvé
195+
}
196+
}
197+
198+
function readWhenOnlyOneResult() {
199+
const observer = new MutationObserver(async () => {
200+
const found = await waitForElement('a.docList-links', (linkElement) => {
201+
console.log("linkElement", linkElement);
202+
linkElement.click();
203+
});
204+
if (found) {
205+
observer.disconnect(); // Arrêter l'observation une fois l'élément trouvé et cliqué
206+
}
207+
});
208+
observer.observe(document.body, { childList: true, subtree: true });
209+
}
210+
211+
const DEFAULT_SETTINGS = {
212+
auto_open_link: false
213+
};
214+
const OPHIROFOX_SETTINGS_KEY = "ophirofox_settings";
215+
216+
async function getAutoOpenOption() {
217+
const key = OPHIROFOX_SETTINGS_KEY;
218+
return new Promise((accept) => {
219+
chrome.storage.local.get([key], function (result) {
220+
if (result.hasOwnProperty(key)) {
221+
current_settings = JSON.parse(result[key]);
222+
accept(current_settings.auto_open_link);
223+
}
224+
else accept(DEFAULT_SETTINGS.auto_open_link);
225+
});
226+
});
227+
}
228+
229+
onLoad();

ophirofox/settings/options_ui.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
<input type="checkbox" id="open_links_new_tab">
9494
Ouvrir les liens Europresse dans un nouvel onglet
9595
</label>
96+
<label>
97+
<input type="checkbox" id="auto_open_link">
98+
Ouvrir automatiquement le lien Europresse lorsqu'un seul résultat est trouvé
99+
</label>
96100
</fieldset>
97101
<fieldset>
98102
<legend>Partenaire Europresse</legend>

ophirofox/settings/options_ui.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const template = document.getElementById("partner_template");
33
const search = document.getElementById("partner_search");
44
const missing_permissions_btn = document.getElementById("missing_permissions");
55
const openLinksCheckbox = document.getElementById("open_links_new_tab");
6+
const autoOpenLinkCheckbox = document.getElementById("auto_open_link");
67

78
let settings = {};
89

@@ -41,6 +42,7 @@ getSettings().then((retrievedSettings) => {
4142
ophirofox_config_list.forEach(p => add_partner(p.name));
4243
reCheckPermissions(settings.partner_name);
4344
openLinksCheckbox.checked = settings.open_links_new_tab || false;
45+
autoOpenLinkCheckbox.checked = settings.auto_open_link || false;
4446
});
4547

4648
// Gestion des modifications de la case à cocher
@@ -49,6 +51,11 @@ openLinksCheckbox.onchange = () => {
4951
setSettings(settings);
5052
};
5153

54+
autoOpenLinkCheckbox.onchange = () => {
55+
settings.auto_open_link = autoOpenLinkCheckbox.checked;
56+
setSettings(settings);
57+
};
58+
5259
// Gestion du bouton de permissions manquantes
5360
missing_permissions_btn.onclick = async () => {
5461
const partner_name = settings.partner_name;

0 commit comments

Comments
 (0)