Browse Source

Circumvent CORS pls?

master
Stian Fredrik Aune 4 years ago
parent
commit
b3607e5f66
  1. 38
      my-bois/background.js
  2. 1
      my-bois/build.sh
  3. 3
      my-bois/public/manifest.json
  4. 5
      my-bois/src/components/Bois.jsx
  5. 2
      my-bois/src/components/Score.jsx
  6. 6
      my-bois/src/helpers/diff.test.js
  7. 56
      my-bois/src/hooks/net.js

38
my-bois/background.js

@ -0,0 +1,38 @@
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;
});

1
my-bois/build.sh

@ -3,3 +3,4 @@ npm run build
mv build/static/css/main.*.css build/static/css/main.css
mv build/static/js/main.*.js build/static/js/main.js
mv build/static/js/runtime-main.*.js build/static/js/runtime-main.js
cp ./background.js build/background.js

3
my-bois/public/manifest.json

@ -2,6 +2,9 @@
"short_name": "YKonsole Helper",
"version": "1.0",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["https://www.youtube.com/*"],

5
my-bois/src/components/Bois.jsx

@ -2,7 +2,7 @@ import React, {useContext, useEffect, useState} from 'react';
import "./Bois.css";
import {StatusContext} from "./Contexts";
import {CalorieScore, CpmScore, LevelScore, RpmScore, Timer} from "./Score";
import {CalorieScore, CpmScore, LevelScore, PulseScore, RpmScore, Timer} from "./Score";
import calculateDiff from "../helpers/diff";
import useKey from "../hooks/useKey";
import {Milestones} from "./Milestones";
@ -23,7 +23,7 @@ export const LeftBoi = () => {
}
const {cooldownMin} = workout;
const {minutes, seconds, calories, level, rpm} = workoutStatus;
const {minutes, seconds, calories, level, rpm, pulse} = workoutStatus;
const diff = calculateDiff({program, cooldownMin, minutes, seconds, calories});
const cpm = calories / (minutes + (seconds / 60));
@ -34,6 +34,7 @@ export const LeftBoi = () => {
<RpmScore rpm={rpm}/>
<CpmScore cpm={cpm}/>
<LevelScore level={level}/>
<PulseScore pulse={pulse} />
</Boi>
);
};

2
my-bois/src/components/Score.jsx

@ -46,6 +46,8 @@ export const CalorieScore = ({calories, diff, prevDiff}) => (
export const LevelScore = ({level}) => <Score value={level} suffix="lvl"/>;
export const PulseScore = ({pulse}) => pulse ? <Score value={pulse} suffix=" puls"/> : null;
export const RpmScore = ({rpm}) => <Score value={rpm} suffix="rpm"/>;
export const CpmScore = ({cpm}) => <Score value={cpm.toFixed(1)} suffix="kpm"/>;

6
my-bois/src/helpers/diff.test.js

@ -99,7 +99,7 @@ describe("calculateDiff", () => {
expect(diff).toBe(0);
});
it("should require 50 calories after 2.5 min of GIS", () => {
it("should expect 50 calories after 2.5 min of GIS", () => {
const diff = calculateDiff({
program: programGis,
cooldownMin: -1,
@ -111,7 +111,7 @@ describe("calculateDiff", () => {
expect(diff).toBe(3);
});
it("should require 175 calories after 7.5 min of GIS", () => {
it("should expect 175 calories after 7.5 min of GIS", () => {
const diff = calculateDiff({
program: programGis,
cooldownMin: -1,
@ -123,7 +123,7 @@ describe("calculateDiff", () => {
expect(diff).toBe(0);
});
it("should require 310 calories after 12.5 min of GIS", () => {
it("should expect 310 calories after 12.5 min of GIS", () => {
const diff = calculateDiff({
program: programGis,
cooldownMin: 11,

56
my-bois/src/hooks/net.js

@ -1,72 +1,54 @@
export async function fetchBikes() {
return await get("/bike");
return await runBack({query: "fetchBikes"});
}
export async function fetchPrograms() {
return await get("/program");
return await runBack({query: "fetchPrograms"});
}
export async function fetchActiveWorkouts() {
return await get("/workout?active=true");
return await runBack({query: "fetchActiveWorkouts"});
}
export async function createNewWorkout(bike, program) {
const bikeId = bike.id;
const programId = program.id;
return await post("/workout", {bikeId, programId});
return await runBack({query: "fetchActiveWorkouts", bikeId, programId});
}
export async function connectWorkout(workout) {
return await post(`/workout/${workout.id}/connect`);
return await runBack({query: "connectWorkout", ...workout});
}
export async function stopWorkout(workout) {
return await post(`/workout/${workout.id}/stop`);
return await runBack({query: "stopWorkout", ...workout});
}
export async function startWorkout(workout) {
return await post(`/workout/${workout.id}/start`);
return await runBack({query: "startWorkout", ...workout});
}
export async function pauseWorkout(workout) {
return await post(`/workout/${workout.id}/pause`);
return await runBack({query: "pauseWorkout", ...workout});
}
export async function updateCooldownMins(workout, cooldownMin) {
return await put(`/workout/${workout.id}`, {cooldownMin});
return await runBack({query: "pauseWorkout", cooldownMin, ...workout});
}
export function openWebsocket(workout) {
return new WebSocket(url(`/workout/${workout.id}/subscribe`, "ws"));
return new WebSocket(`ws://127.0.0.1:9999/api/workout/${workout.id}/subscribe`);
}
function get(path) {
return fetch(url(path), {
method: "GET",
}).then(r => r.json());
async function runBack(query) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(query, result => {
if (result.success) {
resolve(result.data)
} else {
reject(result.message);
}
function post(path, data = {}) {
return fetch(url(path), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data)
}).then(r => r.json());
}
function put(path, data = {}) {
return fetch(url(path), {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data)
}).then(r => r.json());
}
function url(path, prefix = "http") {
return `${prefix}://127.0.0.1:9999/api${path}`;
});
});
}
Loading…
Cancel
Save