Initial Sortierer
This commit is contained in:
parent
9a3edd4046
commit
1d32171e21
7 changed files with 119 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
|
||||||
|
package-lock.json
|
||||||
15
README.md
15
README.md
|
|
@ -1,2 +1,17 @@
|
||||||
# SichtDerDinge_Sortierer
|
# SichtDerDinge_Sortierer
|
||||||
|
|
||||||
|
Diese Dokumentation beschreibt die Funktionen und den Aufbau des SichtDerDinge-Sortierers.
|
||||||
|
|
||||||
|
## Bestandteile
|
||||||
|
|
||||||
|
### Extension
|
||||||
|
|
||||||
|
Die Extension wird benutzt, um das HTML der Sicht der Dinge an den Server zu senden.
|
||||||
|
|
||||||
|
### Server
|
||||||
|
|
||||||
|
Der Server parsed das HTML und fragt in der Redis-Instanz die Werte ab. Das Ergebnis wird dann als HTML dargestellt.
|
||||||
|
|
||||||
|
### Redis
|
||||||
|
|
||||||
|
Redis hält die Informationen über Verkaufspreise und Realwert aller Items.
|
||||||
|
|
|
||||||
18
extension/background.js
Normal file
18
extension/background.js
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
browser.tabs.onActivated.addListener((activeInfo) => {
|
||||||
|
browser.tabs.executeScript(tabId, {
|
||||||
|
code: 'document.documentElement.outerHTML'
|
||||||
|
}).then(result => {
|
||||||
|
const html = result[0];
|
||||||
|
fetch('http://localhost:3000/notify', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ html })
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('Error sending HTML:', err);
|
||||||
|
});
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('Error fetching HTML:', err);
|
||||||
|
});
|
||||||
|
});
|
||||||
11
extension/manifest.json
Normal file
11
extension/manifest.json
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"manifest_version": 2,
|
||||||
|
"name": "Firefox Tab Monitor",
|
||||||
|
"version": "1.0",
|
||||||
|
"permissions": [
|
||||||
|
"tabs"
|
||||||
|
],
|
||||||
|
"background": {
|
||||||
|
"scripts": ["background.js"],
|
||||||
|
"persistent": true
|
||||||
|
}
|
||||||
21
package.json
Normal file
21
package.json
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
{
|
||||||
|
"name": "sichtderdinge_sortierer",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Sortiert die Items der Sicht der Dinge nach Einkaufspreis und Realwert",
|
||||||
|
"main": "index.mjs",
|
||||||
|
"scripts": {
|
||||||
|
"watch": "webpack --watch --progress --colors --config webpack.conf.js",
|
||||||
|
"dev": "webpack --progress --mode development",
|
||||||
|
"prod": "NODE_ENV=production webpack -p --config webpack.conf.js"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://forgejo.nuhp.de/bwe/SichtDerDinge_Sortierer.git"
|
||||||
|
},
|
||||||
|
"author": "bwe",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"cheerio": "^1.2.0",
|
||||||
|
"redis": "^5.11.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/.index.mjs.kate-swp
Normal file
BIN
src/.index.mjs.kate-swp
Normal file
Binary file not shown.
50
src/index.mjs
Normal file
50
src/index.mjs
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
import { createServer } from "node:http";
|
||||||
|
import { createClient } from "redis";
|
||||||
|
import cheerio from "cheerio";
|
||||||
|
|
||||||
|
const redis = createClient();
|
||||||
|
await redis.connect();
|
||||||
|
|
||||||
|
const server = createServer((req, res) => {
|
||||||
|
if (req.method === "POST" && req.url === "/notify") {
|
||||||
|
let body = "";
|
||||||
|
req.on("data", chunk => (body += chunk));
|
||||||
|
|
||||||
|
req.on("end", async () => {
|
||||||
|
// 1️⃣ HTML parsen
|
||||||
|
const $ = cheerio.load(body);
|
||||||
|
const items = $(".item").map((i, el) => $(el).text()).get();
|
||||||
|
|
||||||
|
const results = [];
|
||||||
|
|
||||||
|
// 2️⃣ Redis abfragen (Set pro Item)
|
||||||
|
for (const item of items) {
|
||||||
|
const setMembers = await redis.sMembers(`item:${item}`);
|
||||||
|
|
||||||
|
if (setMembers.length === 2) {
|
||||||
|
// Angenommen: [verkaufspreis, realwert]
|
||||||
|
const [verkaufspreisStr, realwertStr] = setMembers;
|
||||||
|
const verkaufspreis = parseInt(verkaufspreisStr, 10);
|
||||||
|
const realwert = parseInt(realwertStr, 10);
|
||||||
|
|
||||||
|
results.push({ item, verkaufspreis, realwert });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3️⃣ Sortieren & Top 5
|
||||||
|
const topVerkaufspreis = [...results].sort((a, b) => b.verkaufspreis - a.verkaufspreis).slice(0, 5);
|
||||||
|
const topRealwert = [...results].sort((a, b) => b.realwert - a.realwert).slice(0, 5);
|
||||||
|
|
||||||
|
// 4️⃣ JSON-Antwort
|
||||||
|
res.writeHead(200, { "Content-Type": "application/json" });
|
||||||
|
res.end(JSON.stringify({ topVerkaufspreis, topRealwert }, null, 2));
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.writeHead(404);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(3000, () => console.log("Server läuft auf http://localhost:3000"));
|
||||||
Loading…
Add table
Add a link
Reference in a new issue