Skip to content

Commit e2ee4e0

Browse files
authored
Support de l'ouverture dans un nouvel onglet ! (#321)
1 parent 2efd5a3 commit e2ee4e0

2 files changed

Lines changed: 45 additions & 38 deletions

File tree

ophirofox/content_scripts/config.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function getOphirofoxConfigByName(search_name) {
1515

1616
const DEFAULT_SETTINGS = {
1717
partner_name: "Pas d'intermédiaire",
18+
open_links_new_tab: false,
1819
};
1920

2021
let current_settings = DEFAULT_SETTINGS;
@@ -74,51 +75,47 @@ const ophirofox_config = getOphirofoxConfig();
7475
* @returns {Promise<HTMLAnchorElement>}
7576
*/
7677
async function ophirofoxEuropresseLink(keywords, { publishedTime } = {}) {
77-
// Keywords is the article name
7878
keywords = keywords ? keywords.trim() : document.querySelector("h1").textContent;
7979

80-
// Trying generically to determine published time with meta tags (Open Graph values)
81-
// Heuristics would suggest that it's normally UTC in the media world.
82-
publishedTime = publishedTime || document.querySelector( "meta[property='article:published_time'], meta[property='og:article:published_time'], meta[property='date:published_time']")
83-
?.getAttribute("content") || '';
80+
publishedTime = publishedTime || document.querySelector("meta[property='article:published_time'], meta[property='og:article:published_time'], meta[property='date:published_time']")
81+
?.getAttribute("content") || '';
8482
let publishedTimeInstance = new Date(publishedTime);
8583

8684
if (!isNaN(publishedTimeInstance)) {
87-
// JavaScript, what a pleasure.
8885
publishedTime = publishedTimeInstance.toISOString().slice(0, 10);
8986
} else {
90-
publishedTime = ''
87+
publishedTime = '';
9188
}
9289

93-
// Creating HTML anchor element
9490
const a = document.createElement("a");
9591
a.textContent = "Lire sur Europresse";
9692
a.className = "ophirofox-europresse";
97-
93+
9894
const setKeywords = () => new Promise(accept => {
9995
Promise.all([
100-
// set request type (read, or readPDF)
10196
chrome.storage.local.set({
102-
"ophirofox_request_type":
103-
{
104-
'type': 'read'
105-
}
97+
"ophirofox_request_type": { 'type': 'read' }
10698
}),
10799
chrome.storage.local.set({
108-
"ophirofox_read_request":
109-
{
100+
"ophirofox_read_request": {
110101
'search_terms': keywords,
111102
'published_time': publishedTime
112103
}
113104
}),
114105
]).then(() => accept());
115106
});
107+
116108
a.onmousedown = setKeywords;
117109
a.onclick = async function (evt) {
118110
evt.preventDefault();
119-
const [{ AUTH_URL }] = await Promise.all([ophirofox_config, setKeywords()]);
120-
window.location = AUTH_URL
121-
}
111+
const [{ AUTH_URL }, settings] = await Promise.all([ophirofox_config, getSettings()]);
112+
if (settings.open_links_new_tab) {
113+
window.open(AUTH_URL, "_blank");
114+
} else {
115+
window.location = AUTH_URL;
116+
}
117+
};
118+
122119
ophirofox_config.then(({ AUTH_URL }) => { a.href = AUTH_URL });
123120
return a;
124121
}
@@ -130,35 +127,35 @@ async function ophirofoxEuropresseLink(keywords, { publishedTime } = {}) {
130127
* @returns {Promise<HTMLAnchorElement>}
131128
*/
132129
async function ophirofoxEuropressePDFLink(media_id, publishedTime) {
133-
// Creating HTML anchor element
134130
const a = document.createElement("a");
135131
a.textContent = "Lire sur Europresse";
136132
a.className = "ophirofox-europresse";
137-
133+
138134
const setKeywords = () => new Promise(accept => {
139135
Promise.all([
140-
// set request type (read, or readPDF)
141136
chrome.storage.local.set({
142-
"ophirofox_request_type":
143-
{
144-
'type': 'readPDF'
145-
}
137+
"ophirofox_request_type": { 'type': 'readPDF' }
146138
}),
147139
chrome.storage.local.set({
148-
"ophirofox_readPDF_request":
149-
{
140+
"ophirofox_readPDF_request": {
150141
'media_id': media_id,
151142
'published_time': publishedTime
152143
}
153144
}),
154145
]).then(() => accept());
155146
});
147+
156148
a.onmousedown = setKeywords;
157149
a.onclick = async function (evt) {
158150
evt.preventDefault();
159-
const [{ AUTH_URL }] = await Promise.all([ophirofox_config, setKeywords()]);
160-
window.location = AUTH_URL
161-
}
151+
const [{ AUTH_URL }, settings] = await Promise.all([ophirofox_config, getSettings()]);
152+
if (settings.open_links_new_tab) {
153+
window.open(AUTH_URL, "_blank");
154+
} else {
155+
window.location = AUTH_URL;
156+
}
157+
};
158+
162159
ophirofox_config.then(({ AUTH_URL }) => { a.href = AUTH_URL });
163160
return a;
164161
}

ophirofox/content_scripts/europresse_search.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,28 @@ async function onLoad() {
139139
path === "/Pdf"
140140
)) return;
141141

142-
/* Fix une issue avec le proxy BNF qui redirige vers /Pdf */
143-
if (path === '/Pdf' && await hasConsumable()) {
142+
if (!await hasConsumable()) {
143+
console.log("(Ophirofox) No consumable found.");
144+
return;
145+
}
146+
147+
// Fix une issue avec le proxy BNF qui redirige vers /Pdf
148+
if (path === '/Pdf') {
144149
window.location.pathname = '/Search/Reading';
145150
return;
146151
}
147152

148-
const { type } = await consumeRequestType();
149-
console.log("request_type", type);
150-
if (type == "readPDF") {
151-
await loadReadPDF();
153+
const request = await consumeRequestType();
154+
if (request && request.type) {
155+
const { type } = request;
156+
console.log("request_type", type);
157+
if (type === "readPDF") {
158+
await loadReadPDF();
159+
} else {
160+
await loadRead();
161+
}
152162
} else {
153-
await loadRead();
163+
console.error("consumeRequestType() returned undefined or an object without a 'type' property.");
154164
}
155165
}
156166

0 commit comments

Comments
 (0)