forked from alanhuang122/fl-oneclick-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
72 lines (62 loc) · 2.91 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
function openNewTab(url, position) {
chrome.tabs.create({url: url, active: true, index: position});
}
function generateConditions(request){
let conditions = [];
conditions.push(`:+`); // only request main namespace, i.e. no User:, Help:, etc pages
conditions.push(`ID::${request.entityId}`);
if (request.filterCategories) {
const categoryExpr = request.filterCategories.join("||");
conditions.push(`Has Game Type::${categoryExpr}`);
}
// each part of the conditions must start with \u001f
return `\u001f` + conditions.join(`\u001f`);
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
const destination = "https://fallenlondon.wiki/wiki/Special:Search/" + request.encodedTitle;
const targetPosition = sender.tab ? sender.tab.index : 0;
if (request.entityId != null) {
const conditions = generateConditions(request);
let formData = new FormData();
formData.append("action", "askargs");
formData.append("format", "json");
formData.append("conditions", conditions);
formData.append("parameters", "limit=21");
formData.append("printouts", "ID");
fetch("https://fallenlondon.wiki/w/api.php", {method: "POST", mode: "cors", body: formData})
.then(response => {
if (!response.ok) {
throw new Error("Server did not like our query")
}
return response.json();
})
.then(result => {
if ("error" in result) {
throw new Error(result.error.info);
}
const entries = Object.entries(result.query.results);
if (entries.length > 0) {
if (entries.length > 5){
console.warn(`[FL 1-Click Wiki] Wiki server returned ${entries.length} results for the query '${conditions}'.\n`
+ `Please notify the wiki admins about this.`);
}
for (let [key, entry] of entries) {
console.debug(`[FL 1-Click Wiki] Opening tab for ${entry.fullurl}`);
openNewTab(entry.fullurl, targetPosition);
}
} else {
console.debug(`[FL 1-Click Wiki] No pages found for ID ${request.entityId}, falling back to using title.`);
openNewTab(destination, targetPosition);
}
})
.catch(error => {
console.error(error);
console.debug("[FL 1-Click Wiki] Error has occurred, falling back to using title.")
openNewTab(destination, targetPosition);
});
} else {
console.debug("[FL 1-Click Wiki] No ID found for the storylet, falling back to using title.");
openNewTab(destination, targetPosition);
}
sendResponse({});
})