Skip to content

Commit

Permalink
refactor: adds comments and breaks donw code info smaller fucntions
Browse files Browse the repository at this point in the history
Signed-off-by: hitg00 <[email protected]>
  • Loading branch information
hitgo00 committed Apr 13, 2021
1 parent 1ace6c6 commit 4526f5e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
71 changes: 60 additions & 11 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@
chrome.runtime.onConnectExternal.addListener((port) => {
let strInfo, cpuInfo, memInfo;
port.onMessage.addListener((message) => {
//to fetch cpu stats
const getCpuInfo = async () => {
return new Promise((resolve, reject) => {
chrome.system.cpu.getInfo((info) => {
cpuInfo = info;
if (info) {
resolve(info);
} else {
reject("Unable to get cpu information");
}
});
});
};

//to fetch memory information
const getMemoryInfo = async () => {
return new Promise((resolve, reject) => {
chrome.system.memory.getInfo((info) => {
memInfo = info;
if (info) {
resolve(info);
} else {
reject("Unable to get memory information");
}
});
});
};

//to fetch storage information
const getStorageInfo = async () => {
return new Promise((resolve, reject) => {
chrome.system.storage.getInfo((info) => {
strInfo = info;
});
port.postMessage({
strInfo,
cpuInfo,
memInfo,
if (info) {
resolve(info);
} else {
reject("Unable to get storage information");
}
});
});
};

const fetchDataFromSystem = () => {
return new Promise((resolve, reject) => {
Promise.all([getCpuInfo(), getMemoryInfo(), getStorageInfo()]).then(
(values) => {
const cpuInfo = values[0];
const memInfo = values[1];
const strInfo = values[2];
resolve({
strInfo,
cpuInfo,
memInfo,
});
}
);
});
};

//listen to incoming port connection requests
chrome.runtime.onConnectExternal.addListener((port) => {
port.onMessage.addListener(async (message) => {
try {
const systemInfo = await fetchDataFromSystem();
console.log(systemInfo);
port.postMessage(systemInfo);
} catch (error) {
port.postMessage(error);
}
});
});
6 changes: 5 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
"default_popup": "popup.html"
},
"externally_connectable": {
"matches": ["http://localhost:3000/*", "https://telemetry-pwa.vercel.app/*"]
"matches": [
"http://localhost:3000/*",
"https://telemetry-pwa.vercel.app/*",
"http://localhost:3001/*"
]
},
"permissions": [
"system.cpu",
Expand Down
3 changes: 2 additions & 1 deletion popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

<body>
<h1 style="border: 1px solid black; width: 512px;">This is a simple Chrome Extension</h1>
<h3>It is responsible for sending System Info to </br><a href="https://telemetry-pwa.vercel.app/">Telemetry Web
<h3>It is responsible for sending System Info to </br><a target="_blank"
href="https://telemetry-pwa.vercel.app/">Telemetry Web
App</a></h3>
</body>

Expand Down

0 comments on commit 4526f5e

Please sign in to comment.