-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
146 lines (127 loc) · 3.87 KB
/
background.js
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
"use strict";
//
// Classes
//
const DateType = {
PUBLISHED: 'published',
UPDATED: 'updated',
PUBLISHEDORUPDATED: 'published or updated'
};
function findOutDatesFromLdJsons(jsons) {
// https://schema.org/Date
let searchers = [];
for (let json of jsons) {
// cleanse, because some websites have multiline json ld or similar...
let cleansedJsonLd = json.replace(/(\r\n|\n|\r|\t)/gm, "");
let ld;
try {
ld = JSON.parse(cleansedJsonLd);
} catch(e) {
console.warn("Parsing LD JSON failed.");
return [];
}
if(Array.isArray(ld)) {
for(let subld of ld) {
searchers.push(...SearcherInstances.GenerateJsonLdSearchers(subld))
}
} else {
searchers.push(...SearcherInstances.GenerateJsonLdSearchers(ld))
}
}
return searchers.map(x => x.search()).filter(x => x !== undefined && x !== null).map(x => x.toDto());
}
function findOutDatesFromMetas(metas) {
let metaSearchers = SearcherInstances.GenerateMetaSearchers(metas);
return metaSearchers.map(x => x.search()).filter(x => x !== undefined && x !== null).map(x => x.toDto());
}
function findOutDatesFromTimeTagDatetimes(datetimes) {
let results = [];
datetimes.forEach(d => {
let date = DateParser.parse(d);
if (date != undefined)
results.push(new SearchResult(DateType.PUBLISHEDORUPDATED, d, date, "time-tag", 10));
});
return results;
}
//
// Communication
//
const requestHandlers = {
getDateInformation: getDateInformation,
getUrl: getUrl,
};
browser.runtime.onMessage.addListener(async (message, sender) => {
const { type, data } = message;
if (type in requestHandlers) {
const response = new Promise(async (resolve, reject) => {
let response = await requestHandlers[type](data);
resolve(response);
});
return response;
} else {
const response = "Unknown request received";
console.error(response);
}
});
async function getDateInformation(data) {
let tabs = await browser.tabs.query({
currentWindow: true,
active: true
});
let tab = tabs[0];
if (tab) {
// test connection to content script
let testConntectionResult = await sendQueryToTab(tab, "test-connection");
if (!testConntectionResult)
return Promise.resolve(undefined);
let results = [];
let metasResult = await sendQueryToTab(tab, "get-metas");
if (metasResult) {
let metasJson = metasResult.response;
let metas = JSON.parse(metasJson);
results = results.concat(findOutDatesFromMetas(metas));
}
let jsonLdResult = await sendQueryToTab(tab, "get-ld-jsons");
if (jsonLdResult) {
let ldJsons = jsonLdResult.response;
let lds = JSON.parse(ldJsons);
let dateFromLdJsons = findOutDatesFromLdJsons(lds);
results = results.concat(dateFromLdJsons);
}
let timeTagsResults = await sendQueryToTab(tab, "get-time-tag-datetimes");
if (timeTagsResults) {
let jsonDatetimes = timeTagsResults.response;
let datetimes = JSON.parse(jsonDatetimes);
let dateFromTimeTagDatetimes = findOutDatesFromTimeTagDatetimes(datetimes);
results = results.concat(dateFromTimeTagDatetimes);
}
return Promise.resolve(results);
} else {
console.error("no active tab");
return Promise.resolve("ooops");
}
}
async function getUrl(data) {
let tabs = await browser.tabs.query({
currentWindow: true,
active: true
});
let tab = tabs[0];
if (tab)
return tab.url;
return "about:blank";
}
async function sendQueryToTab(tab, query) {
let response;
try {
response = await browser.tabs.sendMessage(
tab.id,
{ query: query }
);
} catch (e) {
console.warn("Could not send query to tab.", e); // e.g. because tab returns json and conent is not injected.
return Promise.resolve(undefined);
}
console.log(`Response from the content script for query '${query}':`, response);
return Promise.resolve(response);
}