You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.6 KiB
38 lines
1.6 KiB
function sendRequest(path, method, body, sendResp) {
|
|
fetch(`http://127.0.0.1:9999/api${path}`, {
|
|
method: method,
|
|
headers: method !== "GET" ? {
|
|
"Content-Type": "application/json",
|
|
} : undefined,
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
}).then(r => r.json())
|
|
.then(data => sendResp({success: true, data}))
|
|
.catch(e => sendResp({success: false, message: e.message}));
|
|
}
|
|
|
|
chrome.runtime.onMessage.addListener(
|
|
function (request, sender, sendResponse) {
|
|
if (request.query === "fetchBikes") {
|
|
sendRequest("/bike", "GET", null, sendResponse);
|
|
} else if (request.query === "fetchPrograms") {
|
|
sendRequest("/program", "GET", null, sendResponse);
|
|
} else if (request.query === "fetchActiveWorkouts") {
|
|
sendRequest("/workout?active=true", "GET", null, sendResponse);
|
|
} else if (request.query === "createNewWorkout") {
|
|
sendRequest("/workout", "POST", request, sendResponse);
|
|
} else if (request.query === "connectWorkout") {
|
|
sendRequest(`/workout/${request.id}/connect`, "POST", {}, sendResponse);
|
|
} else if (request.query === "stopWorkout") {
|
|
sendRequest(`/workout/${request.id}/stop`, "POST", {}, sendResponse);
|
|
} else if (request.query === "startWorkout") {
|
|
sendRequest(`/workout/${request.id}/start`, "POST", {}, sendResponse);
|
|
} else if (request.query === "pauseWorkout") {
|
|
sendRequest(`/workout/${request.id}/pause`, "POST", {}, sendResponse);
|
|
} else if (request.query === "updateCooldownMins") {
|
|
sendRequest(`/workout/${request.id}`, "PUT", request, sendResponse);
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
});
|