Skip to content

Commit

Permalink
✨ Introduce the support of article published time (#192)
Browse files Browse the repository at this point in the history
* ✨Introduce the support of article published time to improve Europresse engine search time

* 🐛 Utilise 00:00:00 UTC pour le moteur de recherche Europresse

* Update ophirofox/content_scripts/config.js

Co-authored-by: Loïc Noss <74484200+lnoss@users.noreply.github.com>

* ♻️ Détection simplifiée de la date de publication

---------

Co-authored-by: Ophir LOJKINE <contact@ophir.dev>
  • Loading branch information
lnoss and lovasoa committed Jan 29, 2024
1 parent 5a3d2d0 commit 4cc5ef3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
17 changes: 16 additions & 1 deletion ophirofox/content_scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,27 @@ const ophirofox_config = getOphirofoxConfig();
* @returns {Promise<HTMLAnchorElement>}
*/
async function ophirofoxEuropresseLink(keywords) {
// Keywords is the article name
keywords = keywords ? keywords.trim() : document.querySelector("h1").textContent;

// Trying to determine published time with meta tags (Open Graph values)
let publishedTime = document.querySelector( "meta[property='article:published_time'], meta[property='og:article:published_time'], meta[property='date:published_time']")
?.getAttribute("content") || '';

// Creating HTML anchor element
const a = document.createElement("a");
a.textContent = "Lire sur Europresse";
a.className = "ophirofox-europresse";

const setKeywords = () => new Promise(accept =>
chrome.storage.local.set({ "ophirofox_keywords": keywords }, accept));
chrome.storage.local.set({
"ophirofox_read_request":
{
'search_terms': keywords,
'published_time': publishedTime
}
},
accept));
a.onmousedown = setKeywords;
a.onclick = async function (evt) {
evt.preventDefault();
Expand Down
62 changes: 56 additions & 6 deletions ophirofox/content_scripts/europresse_search.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
async function consumeSearchTerms() {
async function consumeReadRequest() {
return new Promise((accept, reject) => {
chrome.storage.local.get("ophirofox_keywords",
chrome.storage.local.get("ophirofox_read_request",
(r) => {
accept(r.ophirofox_keywords);
chrome.storage.local.remove("ophirofox_keywords");
accept(r.ophirofox_read_request);
chrome.storage.local.remove("ophirofox_read_request");
});
})
}
Expand All @@ -18,7 +18,7 @@ async function onLoad() {
path.startsWith("/Search/Express") ||
path.startsWith("/Search/Simple")
)) return;
const search_terms = await consumeSearchTerms();
const { search_terms, published_time } = await consumeReadRequest();
if (!search_terms) return;
const stopwords = new Set(['d', 'l', 'et', 'sans']);

Expand All @@ -36,8 +36,58 @@ async function onLoad() {
.join(' ');
const keyword_field = document.getElementById("Keywords");
keyword_field.value = 'TIT_HEAD=' + keywords;

// Looking for a time range
const date_filter = document.getElementById("DateFilter_DateRange");
if (date_filter) date_filter.value = 9;

if (date_filter) {
if (!published_time) { // Full expand the time range
date_filter.value = 9;
} else {
const publishedDate = new Date(published_time);
publishedDate.setUTCHours(0, 0, 0, 0); // Europresse saves the exact UTC date, but "depuis X jours" is based on midnight
const currentDate = new Date();

const timeDifference = currentDate.getTime() - publishedDate.getTime();
// Rounds up for tolerance to be sure to not filtering badly
const daysDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24));

let filterValue = 9;

switch (true) {
case (daysDifference <= 1):
filterValue = 2; // Depuis hier
break;
case (daysDifference <= 3):
filterValue = 11; // Depuis 3 jours
break;
case (daysDifference <= 7):
filterValue = 3; // Depuis 7 jours
break;
case (daysDifference <= 30):
filterValue = 4; // Depuis 30 jours
break;
case (daysDifference <= 90):
filterValue = 5; // Depuis 3 mois
break;
case (daysDifference <= 180):
filterValue = 6; // Depuis 6 mois
break;
case (daysDifference <= 365):
filterValue = 7; // Depuis 1 an
break;
case (daysDifference <= 730):
filterValue = 8; // Depuis 2 ans
break;
default:
filterValue = 9; // Dans toutes les archives
break;
}

date_filter.value = filterValue;
}
}

keyword_field.form.submit();
}

Expand Down

0 comments on commit 4cc5ef3

Please sign in to comment.