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 !== null ? 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; });