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

4 years ago
  1. function sendRequest(path, method, body, sendResp) {
  2. fetch(`http://127.0.0.1:9999/api${path}`, {
  3. method: method,
  4. headers: method !== "GET" ? {
  5. "Content-Type": "application/json",
  6. } : undefined,
  7. body: body !== null ? JSON.stringify(body) : undefined,
  8. }).then(r => r.json())
  9. .then(data => sendResp({success: true, data}))
  10. .catch(e => sendResp({success: false, message: e.message}));
  11. }
  12. chrome.runtime.onMessage.addListener(
  13. function (request, sender, sendResponse) {
  14. if (request.query === "fetchBikes") {
  15. sendRequest("/bike", "GET", null, sendResponse);
  16. } else if (request.query === "fetchPrograms") {
  17. sendRequest("/program", "GET", null, sendResponse);
  18. } else if (request.query === "fetchActiveWorkouts") {
  19. sendRequest("/workout?active=true", "GET", null, sendResponse);
  20. } else if (request.query === "createNewWorkout") {
  21. sendRequest("/workout", "POST", request, sendResponse);
  22. } else if (request.query === "connectWorkout") {
  23. sendRequest(`/workout/${request.id}/connect`, "POST", {}, sendResponse);
  24. } else if (request.query === "stopWorkout") {
  25. sendRequest(`/workout/${request.id}/stop`, "POST", {}, sendResponse);
  26. } else if (request.query === "startWorkout") {
  27. sendRequest(`/workout/${request.id}/start`, "POST", {}, sendResponse);
  28. } else if (request.query === "pauseWorkout") {
  29. sendRequest(`/workout/${request.id}/pause`, "POST", {}, sendResponse);
  30. } else if (request.query === "updateCooldownMins") {
  31. sendRequest(`/workout/${request.id}`, "PUT", request, sendResponse);
  32. } else {
  33. return false;
  34. }
  35. return true;
  36. });