-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
346 lines (315 loc) · 9.77 KB
/
background.js
File metadata and controls
346 lines (315 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import { getListOfRelations } from "./helpers/medias.js";
import { tsvJSON, csvJSON } from "./helpers/dataReader.js";
// Global variables to store loaded data
let mediasUrl, mediasArray, organisationsArray, personnesArray;
let organisationOrganisationArray,
personneMediaArray,
personneOrganisationArray,
organisationMediaArray;
const setup = async () => {
if (!mediasUrl) {
const result = await Promise.all([
readCsvFile("./data/medias_extracted.csv"),
readTsvFile("./data/medias.tsv"),
readTsvFile("./data/organisations.tsv"),
readTsvFile("./data/personnes.tsv"),
readTsvFile("./data/organisation-organisation.tsv"),
readTsvFile("./data/personne-media.tsv"),
readTsvFile("./data/personne-organisation.tsv"),
readTsvFile("./data/organisation-media.tsv"),
]);
// Store results in global variables
mediasUrl = result[0];
mediasArray = result[1];
organisationsArray = result[2];
personnesArray = result[3];
organisationOrganisationArray = result[4];
personneMediaArray = result[5];
personneOrganisationArray = result[6];
organisationMediaArray = result[7];
}
};
const notfify = (isKnownMedia) => {
if (isKnownMedia) {
chrome.action.setIcon({
path: {
16: "icons/icon16-green.png",
32: "icons/icon32-green.png",
},
});
} else {
chrome.action.setIcon({
path: {
16: "icons/icon16.png",
32: "icons/icon32.png",
},
});
}
};
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
const { url } = tab;
await setup();
const mediaByUrl = searchWithSanitizedUrl(mediasUrl, getURL(url));
if (mediaByUrl) {
// Check if the media exists in organisationMediaArray
const isMediaInRelations =
organisationMediaArray.some((rel) => rel.cible === mediaByUrl.nom) ||
personneMediaArray.some((rel) => rel.cible === mediaByUrl.nom);
if (isMediaInRelations) {
notfify(true);
} else {
notfify(false);
}
} else {
notfify(false);
}
});
chrome.tabs.onActivated.addListener(async function (activeInfo) {
await setup();
// how to fetch tab url using activeInfo.tabid
chrome.tabs.get(activeInfo.tabId, function (tab) {
const mediaByUrl = searchWithSanitizedUrl(mediasUrl, getURL(tab.url));
if (mediaByUrl) {
// Check if the media exists in organisationMediaArray or personneMediaArray
const isMediaInRelations =
organisationMediaArray.some((rel) => rel.cible === mediaByUrl.nom) ||
personneMediaArray.some((rel) => rel.cible === mediaByUrl.nom);
if (isMediaInRelations) {
notfify(true);
} else {
notfify(false);
}
} else {
notfify(false);
}
});
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message === "handle_get_url_info") {
chrome.tabs.query({ currentWindow: true, active: true }).then((res) => {
const url = res[0].url;
// Directly use data already loaded via setup() or load it if necessary
(mediasUrl ? Promise.resolve() : setup()).then(() => {
// Check if url exists in relations array
const mediaByUrl = searchWithSanitizedUrl(mediasUrl, getURL(url));
if (mediaByUrl) {
// Check if the media exists in a relationship
const isMediaInRelations =
organisationMediaArray.some(
(rel) => rel.cible === mediaByUrl.nom
) || personneMediaArray.some((rel) => rel.cible === mediaByUrl.nom);
if (isMediaInRelations) {
try {
// Get all relationships for this media
const relationship = getRelationShip(getURL(url));
// Get media information from mediasArray
const mediaInfo =
mediasArray.find((media) => media.Nom === mediaByUrl.nom) || {};
sendResponse({
rootUrl: getURL(url),
url,
mediaInfo: {
cible: mediaByUrl.nom,
...mediaInfo,
},
list: relationship,
});
return Promise.resolve({
rootUrl: getURL(url),
url,
mediaInfo: {
cible: mediaByUrl.nom,
...mediaInfo,
},
list: relationship,
});
} catch (e) {
console.error("Error retrieving relationships", e);
sendResponse({
rootUrl: getURL(url),
url,
mediaInfo: null,
list: null,
error: {
message:
"An error occurred. If you want to report it, please visit <a href='https://github.com/StarNoodle/watch-dogs' onClick='chrome.tabs.create({ url: 'https://github.com/StarNoodle/watch-dogs' });'>this page</a>",
},
});
return Promise.resolve({
rootUrl: getURL(url),
url,
mediaInfo: null,
list: null,
error: {
message:
"An error occurred. If you want to report it, please visit <a href='https://github.com/StarNoodle/watch-dogs' onClick='chrome.tabs.create({ url: 'https://github.com/StarNoodle/watch-dogs' });'>this page</a>",
},
});
}
} else {
sendResponse({
rootUrl: getURL(url),
url,
mediaInfo: null,
list: null,
});
return Promise.resolve(
chrome.storage.local.remove(["url", "mediaInfo", "list"])
);
}
} else {
const error = new Error("url not found");
sendResponse({
rootUrl: getURL(url),
url,
mediaInfo: null,
list: null,
error,
});
return Promise.resolve({ error });
}
});
});
}
return true;
});
const readTsvFile = (path) => {
return new Promise((resolve, reject) => {
fetch(path)
.then((response) => {
return response.blob();
})
.then((file) => {
const reader = new FileReader();
reader.onload = function (e) {
const text = e.target.result;
resolve(tsvJSON(text));
};
reader.readAsText(file);
})
.catch((err) => reject(err));
});
};
const readCsvFile = (path) => {
return new Promise((resolve, reject) => {
fetch(path)
.then((response) => {
return response.blob();
})
.then((file) => {
const reader = new FileReader();
reader.onload = function (e) {
const text = e.target.result;
resolve(csvJSON(text));
};
reader.readAsText(file);
})
.catch((err) => reject(err));
});
};
const getURL = (urlStr) => {
if (urlStr) {
const url = new URL(urlStr);
return url.hostname;
} else {
return null;
}
};
const getRelationShip = (url) => {
const mediaRelationsFirstElement = searchWithSanitizedUrl(mediasUrl, url);
if (!mediaRelationsFirstElement) {
return [];
}
// Combine all relevant relationships
const allRelations = [
...organisationMediaArray.map((rel) => ({
origine: rel.origine,
cible: rel.cible,
valeur: rel.valeur,
type: "organisation-media",
})),
...personneMediaArray.map((rel) => ({
origine: rel.origine,
cible: rel.cible,
valeur: rel.valeur,
type: "personne-media",
})),
...organisationOrganisationArray.map((rel) => ({
origine: rel.origine,
cible: rel.cible,
valeur: rel.valeur,
type: "organisation-organisation",
})),
...personneOrganisationArray.map((rel) => ({
origine: rel.origine,
cible: rel.cible,
valeur: rel.valeur,
type: "personne-organisation",
})),
];
// Format values (add % if necessary)
const formattedRelations = allRelations.map((rel) => ({
...rel,
value:
rel.valeur && /[0-9]+/.test(rel.valeur) ? `${rel.valeur}%` : rel.valeur,
}));
// Get structured relationships as before
const listOfRelations = getListOfRelations(
formattedRelations,
mediaRelationsFirstElement.nom
);
// Format to match the structure expected by the front-end
const formattedOutput = listOfRelations.map((chain) => {
// Expected format: each chain element with type (start, middle, end) and value if available
return chain.map((node, index) => {
// Determine type based on position
let type =
index === 0 ? "start" : index === chain.length - 1 ? "end" : "middle";
// Build object with required properties
return {
name: node.name,
...(node.value ? { value: node.value } : {}),
type,
};
});
});
return formattedOutput;
};
const searchWithSanitizedUrl = (list, url) => {
const resultByUrl = list.filter(
(_l) =>
_l.url.replace(/(http:\/\/|https:\/\/)?/, "").replace(/\/$/, "") === url
);
if (resultByUrl.length === 1) {
return resultByUrl[0];
} else if (resultByUrl.length > 1) {
// Check if arrival url matches the url parameter
const resultByArrivalUrl = resultByUrl.find(
(_l) =>
_l.arrival_url
.replace(/(http:\/\/|https:\/\/)?/, "")
.replace(/\/$/, "") === url
);
if (resultByArrivalUrl) {
return resultByArrivalUrl;
} else {
return null;
}
}
return null;
};
const loadData = async () => {
// Reuse the setup function to load data
await setup();
// Return all loaded data
return [
mediasUrl,
mediasArray,
organisationsArray,
personnesArray,
organisationOrganisationArray,
personneMediaArray,
personneOrganisationArray,
organisationMediaArray,
];
};