-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackground.js
More file actions
70 lines (63 loc) · 2.32 KB
/
background.js
File metadata and controls
70 lines (63 loc) · 2.32 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
// background.js
console.log("[ROVAS Background] Service Worker started.");
// Fetching from background is necessary to respect CORS
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.contentScriptQuery == 'fetchUrl') {
fetch(request.site, request.payload)
.then( async (response) => {
const text = await response.text();
sendResponse(
{
status: response.status,
ok: response.ok,
text: text // is a string, NOT A PROMISE
}
);
})
.catch((error) => {
console.error("[ROVAS] error with sending a message: ", error);
sendResponse(
{
status: 400,
ok: false,
text: ""
}
);
});
return true;
}
});
// Listener to capture changeset ID by OSM API request
chrome.webRequest.onCompleted.addListener(
function(details) {
// Looking for URL '/changeset/{id}/upload' or '/changeset/{id}/close'
const url = new URL(details.url);
// Regex to get id from /changeset/123456789/upload or /changeset/123456789/close
const match = url.pathname.match(/\/changeset\/(\d+)\/(upload|close)/);
if (match && match[1]) {
const changesetId = match[1];
console.log(`%c[ROVAS Background] ID Changeset Found: ${changesetId} from request: ${details.url}`, 'color: cyan; font-weight: bold;');
// Send changeset ID to the content script
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
if (
tabs[0] &&
(tabs[0].url.startsWith("https://www.openstreetmap.org/edit") ||
tabs[0].url.startsWith("https://www.openhistoricalmap.org/edit") ||
tabs[0].url.startsWith("https://rapideditor.org/edit"))
) {
// Sends only if active tab is ID editor
chrome.tabs.sendMessage(tabs[0].id, {
type: "CHANGESET_ID_DETECTED",
changesetId: changesetId,
siteName: tabs[0].url.startsWith("https://www.openhistoricalmap.org/edit") ? "OpenHistoricalMap" : "OpenStreetMap"
});
}
});
}
},
// Filters only URLs of OSM API that we are interested to check
{
urls: ["https://api.openstreetmap.org/api/0.6/changeset/*",
"https://www.openhistoricalmap.org/api/0.6/changeset/*"] // yes, it's "www.", not "api."
}
);