-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
122 lines (109 loc) · 4.81 KB
/
background.js
File metadata and controls
122 lines (109 loc) · 4.81 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
function handleApiRequest(url, method = 'GET', customHeaders = {}, body = null, callback) {
const isSparqlRequest = url.includes('query.wikidata.org/sparql');
const options = {
method: method,
headers: new Headers(customHeaders),
};
// Se è una richiesta SPARQL, non includere le credenziali
if (!isSparqlRequest) {
options.credentials = 'include'; // Gestione credenziali per altri endpoint
} else {
options.credentials = 'same-origin'; // No credenziali per le richieste SPARQL
}
// Se è una richiesta POST, aggiungi il corpo
if (method === 'POST' && body) {
if (url.includes('wikidata.org')){
options.body = body;
options.headers.set('Content-Type', 'application/x-www-form-urlencoded');
} else {
options.body = JSON.stringify(body);
}
}
fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error('Errore nella risposta HTTP: ' + response.status);
}
return response.json();
})
.then(data => {
callback({ success: true, data: data });
})
.catch(error => {
console.error('Errore fetch:', error);
callback({ success: false, error: error.message });
});
}
// Ascolto connessioni via port (comunicazione lunga)
chrome.runtime.onConnect.addListener(function(port) {
if (port.name === "waping") {
port.onMessage.addListener(function(msg) {
if (msg.event === "wapi_request") {
const base = "https://www.wikidata.org/w/api.php?action=wbsearchentities&search=";
const tail = "&format=json&language=it&uselang=it&type=item&limit=10";
const fullUrl = base + encodeURIComponent(msg.text) + tail;
handleApiRequest(
fullUrl,
'GET',
{ 'Accept': 'application/json' },
null,
(response) => {
if (response.success && response.data?.search) {
const filterList = response.data.search.map((ent) => ({
id: ent.id,
label: ent.label,
description: ent.description,
uri: ent.concepturi
}));
port.postMessage({ event: "wapi_response", entities: filterList });
} else {
port.postMessage({ event: "wapi_response", entities: [] });
}
});
} else if (msg.event === "wapi_request_properties") {
const query = `
SELECT ?relatedProp ?relatedPropLabel ?relatedPropDescription
WHERE {
{
wd:${msg.text.toUpperCase()} wdt:P1659 ?relatedProp .
} UNION {
?relatedProp wdt:P1659 wd:${msg.text.toUpperCase()} .
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],it,en".
?relatedProp rdfs:label ?relatedPropLabel ;
schema:description ?relatedPropDescription .
}
}
`;
const url = "https://query.wikidata.org/sparql?query=" + encodeURIComponent(query);
handleApiRequest(
url,
'GET',
{ 'Accept': 'application/sparql-results+json' },
null,
(response) => {
console.log(response)
if (response.success && response.data.results?.bindings) {
const filterList = response.data.results.bindings.map((prop) => ({
id: prop.relatedProp.value.split("/").pop(),
label: prop.relatedPropLabel?.value || "",
description: prop.relatedPropDescription?.value || "",
uri: prop.relatedProp.value
}));
port.postMessage({ event: "wapi_response_properties", properties: filterList });
} else {
port.postMessage({ event: "wapi_response_properties", properties: [] });
}
});
}
});
}
});
// Ascolto richieste dirette (messaggi brevi)
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "proxy-api") {
handleApiRequest(request.url, request.method, request.headers, request.body, sendResponse);
return true;
}
});