Basic POC for frontend page manipulation

This commit is contained in:
Dennis Schoepf 2020-11-23 13:27:37 +01:00
parent 4f38af0a13
commit 85998c634c
5 changed files with 40 additions and 15 deletions

View file

@ -1,7 +1,7 @@
import mqtt from "mqtt";
const mqttHeartbeatTopic = "dennissc/feeds/heartbeat/json";
const mqttTiltTopic = "dennissc/feeds/tilt-sensor/json";
const mqttHeartbeatTopic = `${process.env.AIO_USERNAME}/feeds/heartbeat/json`;
const mqttTiltTopic = `${process.env.AIO_USERNAME}/feeds/tilt-sensor/json`;
const mqttEdnpoint = process.env.AIO_ENDPOINT;
const mqttOptions = {
port: Number(process.env.AIO_PORT),

View file

@ -1,11 +1,12 @@
import { subscribeToAllSensors } from "./api";
import dotenv from "dotenv";
import { storeValue } from "./storage";
dotenv.config();
chrome.runtime.onInstalled.addListener(() => {
subscribeToAllSensors({
onHeartbeat: (heartbeatData) => console.log(heartbeatData),
onTilt: (tiltData) => console.log(tiltData),
onHeartbeat: (heartbeatData) => storeValue("heartbeat", heartbeatData),
onTilt: (tiltData) => storeValue("tilt", tiltData),
});
});

View file

@ -0,0 +1,3 @@
export function storeValue(key, value) {
chrome.storage.sync.set({ [key]: value });
}

View file

@ -1 +1,23 @@
console.log("Frontend registered");
const testEl = document.createElement("div");
testEl.style["position"] = "fixed";
testEl.style["top"] = "0";
testEl.style["left"] = "0";
testEl.style["z-index"] = "1000";
testEl.style["background-color"] = "red";
document.body.appendChild(testEl);
chrome.storage.onChanged.addListener((changes, namespace) => {
for (let key in changes) {
const newValue = changes[key];
if (key === "heartbeat") {
console.log("heartbeat", newValue);
testEl.innerHTML += `\nNew heartbeat: ${JSON.stringify(newValue)}`;
}
if (key === "tilt") {
console.log("tilt", newValue);
testEl.innerHTML += `\nNew tilt: ${JSON.stringify(newValue)}`;
}
}
});