-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
190 lines (165 loc) · 5.4 KB
/
background.js
File metadata and controls
190 lines (165 loc) · 5.4 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
"use strict";
//
// Classes
//
const DateType = {
PUBLISHED: 'published',
UPDATED: 'updated',
PUBLISHEDORUPDATED: 'published or updated'
};
browser.tabs.onActivated.addListener(async (activeInfo) => {
notifyPopupOfTabChange(activeInfo.tabId, "activated");
});
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
// Only notify when the tab has completed loading and is active
if (changeInfo.status === "complete") {
const activeTabs = await browser.tabs.query({ active: true, currentWindow: true });
if (activeTabs.length > 0 && activeTabs[0].id === tabId) {
notifyPopupOfTabChange(tabId, "complete");
}
}
});
async function notifyPopupOfTabChange(tabId, status) {
try {
await browser.runtime.sendMessage({
type: "tabChanged",
data: { tabId, status }
});
} catch (error) {
}
}
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;
}
function findOutDatesFromUrl(url) {
let urlSearchers = SearcherInstances.GenerateUrlSearchers(url);
return urlSearchers.map(x => x.search()).filter(x => x !== undefined && x !== null).map(x => x.toDto());
}
//
// 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);
}
let scriptTagsResults = await sendQueryToTab(tab, "get-script-tags");
if (scriptTagsResults) {
let jsonScriptTags = scriptTagsResults.response;
let scriptTags = JSON.parse(jsonScriptTags);
let scriptTagSearchers = SearcherInstances.GenerateScriptTagSearchers(scriptTags);
let datesFromScriptTags = scriptTagSearchers.map(x => x.search())
.filter(x => x !== undefined && x !== null)
.map(x => x.toDto());
results = results.concat(datesFromScriptTags);
}
// Extract dates from URL
let datesFromUrl = findOutDatesFromUrl(tab.url);
results = results.concat(datesFromUrl);
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);
}