From fa23741f0ef1d25a13ae966028906f435bcb47cb Mon Sep 17 00:00:00 2001 From: Marcel <marcel.kehrberg@student.reutlingen-university.de> Date: Mon, 10 Mar 2025 18:34:42 +0100 Subject: [PATCH] =?UTF-8?q?=C3=84nderungen=20-=20=20wenn=20das=20Addon=20r?= =?UTF-8?q?econenter=20widerherstellung=20allter=20informationen=20-=20blo?= =?UTF-8?q?ck=20Einstellungen=20werden=20=C3=BCbergeben=20-=20Beim=20Ende,?= =?UTF-8?q?=20beim=20Abbruch=20oder=20beim=20Beednen=20des=20Programs=20wi?= =?UTF-8?q?rd=20das=20Addon=20benachrichtigt=20-=20das=20Distration=20Obje?= =?UTF-8?q?kt=20wird=20jetzt=20richtig=20im=20addon=20erstellt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BrowserAddon/{vOG.xpi => V2.xpi} | Bin 3732 -> 3593 bytes BrowserAddon/background.js | 48 ++++++++++------ BrowserAddon/manifest.json | 20 +++---- .../Screens/Regulaer/01Overview.xaml.cs | 54 ++++++++++-------- .../Services/ProcessMonitor.cs | 4 ++ .../Services/WebSocketServer/Server.cs | 53 ++++++++++++++++- 6 files changed, 125 insertions(+), 54 deletions(-) rename BrowserAddon/{vOG.xpi => V2.xpi} (68%) diff --git a/BrowserAddon/vOG.xpi b/BrowserAddon/V2.xpi similarity index 68% rename from BrowserAddon/vOG.xpi rename to BrowserAddon/V2.xpi index 220d0bc7702f6f8d3f72933bb053ea09f91adaa5..90e8906007b069ce7e03bcec189aaa5c7b5e2f07 100644 GIT binary patch delta 535 zcmV+y0_gpe9f=&XIR~``u>w&80%#wTRRc4BgjLRGa2?KPVynz&ZBa{V!!Qut7jlO| zn`}}i?JA_Dixzr;7P=_G$nw}$B1=Le$Aprb^eEZoVwG&iCQvWTeBQiy^Yk;jgYdDM zYgr)q^ni|}(QE^ci=n4^jUi`m=3SYVUCR@aJtV8KoFfS*tsh#K-wd5~mviC4WT2&g zs63R|Ue20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5z*OstQIVn70H3<?a11qFIp#ibS842&!wDKHTl!pp#J zcK%>`LDgw+)G-6iV&?$K1C0fP4O>|!Gx2IL138l~u*yvK<P}y36U>T2wx5?l0;-yU zVOygo^W+*{WhN1u$qRXPn3!27pXXKcVq_9wMs*y_0yY%QkJ%V-IhB=x5#-d#Yk9Po iE!i0+%F4?Eg~|-ir3ZMk0<|$Puma&EV3@q&0r3E^3-WUS diff --git a/BrowserAddon/background.js b/BrowserAddon/background.js index 1a2993f..a7f0db0 100644 --- a/BrowserAddon/background.js +++ b/BrowserAddon/background.js @@ -2,6 +2,7 @@ let socket; let trackingAktiv = false; let isConnected = false; let distraktions; +let withBlock; function connectWebSocket() { if (isConnected) { @@ -23,13 +24,13 @@ function connectWebSocket() { console.log("Verbunden mit der WPF-Anwendung."); clearTimeout(connectionTimeout); isConnected = true; // Markiere als verbunden - socket.send("Hallo vom Addon"); + socket.send("{\"action\": \"connected\", \"status\": true}"); }; // Empfang von Nachrichten socket.onmessage = function(event) { const message = parseMessage(event.data); - console.log("Nachricht empfangen: ", message); + //console.log("Nachricht empfangen: ", message); //navigator.clipboard.writeText(message); if(message.action === "sendJsonContent") { @@ -46,11 +47,21 @@ function connectWebSocket() { removeTabListeners(); trackingAktiv = false; } + + if (message.action === "BlockMode" && message.status === true) { + withBlock = true; + console.log("Nachricht empfangen: ", message); + } else if(message.action === "noBlockMode" && message.status === false) { + withBlock = false; + console.log("Nachricht empfangen: ", message); + } }; socket.onclose = function() { console.log("Verbindung geschlossen."); isConnected = false; + removeTabListeners(); + trackingAktiv = false; // Versuche, nach einer kurzen Zeit erneut eine Verbindung herzustellen setTimeout(connectWebSocket, 5000); // 5 Sekunden warten, bevor erneut versucht wird }; @@ -59,19 +70,18 @@ function connectWebSocket() { console.error("WebSocket Fehler:", error); socket.close(); // Schließe den Socket, um den Fehler zu behandeln und es erneut zu versuchen }; - } connectWebSocket(); -addTabListeners(); +//addTabListeners(); function createDistractionList(message) { const result = {}; - for (content in message) { - const distractingUrls = message["content"]["Games"].filter(item => item.distracting).map(item => item.url); + for (category in message["content"]) { + const distractingUrls = message["content"][category].filter(item => item.distracting).map(item => item.url); if (distractingUrls.length > 0) { - result[content] = distractingUrls; + result[category] = distractingUrls; } } return result; @@ -121,15 +131,21 @@ function handlerTabActiv(activeInfo) { function checkUrl(url, tabId) { - for (content in distraktions.content) { - //const content = "youtube.com"; - if (url.includes(distraktions["content"][content])) { - socket.send(`Distraktion Bemerkt: ${url}`); - - browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { - const activeTab = tabs[0]; - browser.tabs.sendMessage(activeTab.id, { action: "showOverlay", tabId: tabId }); - }); + for (category in distraktions) { + //const content = "youtube.com"; + //console.log(category); + + for (element in distraktions[category]) { + if (url.includes(distraktions[category][element])) { + socket.send(`Distraktion Bemerkt: ${url}`); + + if(withBlock) { + browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { + const activeTab = tabs[0]; + browser.tabs.sendMessage(activeTab.id, { action: "showOverlay", tabId: tabId }); + }); + } + } } } }; \ No newline at end of file diff --git a/BrowserAddon/manifest.json b/BrowserAddon/manifest.json index 1252b8c..2b763c3 100644 --- a/BrowserAddon/manifest.json +++ b/BrowserAddon/manifest.json @@ -1,21 +1,17 @@ { - "manifest_version": 3, - "name": "URL-Kopierer", - "version": "1.0", - "description": "Kopiert die URL des aktiven Tabs in die Zwischenablage.", - "permissions": ["tabs", "activeTab", "scripting", "clipboardWrite"], - "host_permissions": ["<all_urls>"], + "manifest_version": 2, + "name": "CoFlow", + "version": "1.1", + "description": "Überprüft ob nicht gewünschte Tab aufgerufne werden und Blockiert diese dann wenn dies wenn die Setting das vrogeben.", + "permissions": ["tabs", "activeTab", "scripting", "clipboardWrite", "<all_urls>"], "background": { "scripts": ["background.js"], "persistent": false }, - "action": { - "default_popup": "popup.html", - "default_icon": "icon.png" + "browser_action": { + "default_popup": "popup.html" }, - "content_security_policy": { - "extension_pages": "script-src 'self'; object-src 'self'; connect-src 'self' ws://localhost:8080" - }, + "content_security_policy": "script-src 'self'; object-src 'self'; connect-src 'self' ws://localhost:8080", "content_scripts": [ { "matches": ["<all_urls>"], diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/01Overview.xaml.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/01Overview.xaml.cs index 832ee9d..50c91cf 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/01Overview.xaml.cs +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/01Overview.xaml.cs @@ -267,30 +267,7 @@ namespace InnoLabProjektDektopApp // Ereignis-Handler für den "Start"-Button private void StartButton_Click(object sender, RoutedEventArgs e) { - Server.Instance.SendMessageToAllClients("{\"action\": \"startTracking\", \"status\": true}"); - - try - { - string jsonContent = File.ReadAllText("F:\\Dokumente\\Schule\\Studium\\InoLab\\procrastinator\\InnoLabProjektDektopApp\\InnoLabProjektDektopApp\\Assets\\distractingWebsites.json"); - //string message = $"{{\"action\": \"sendJsonContent\", \"status\": true, \"content\": {jsonContent}}}"; - - // Erstelle ein Objekt für die Nachricht - var messageObject = new - { - action = "sendJsonContent", - status = true, - content = JsonSerializer.Deserialize<object>(jsonContent) // Parse den JSON-Inhalt - }; - - // Serialisiere das Objekt zu einem JSON-String - string message = JsonSerializer.Serialize(messageObject); - - Server.Instance.SendMessageToAllClients(message); - } - catch - { - - } + @@ -426,6 +403,35 @@ namespace InnoLabProjektDektopApp tmpSession = new Session(focusPeriod, breakPeriod, cycles, distractionMode, mascotVisible, wordsOfAffirmation, insultingWords); this.NavigationService.Navigate(tmpSession); + + + Server.Instance.SendMessageToAllClients("{\"action\": \"startTracking\", \"status\": true}"); + Server.Instance.isTracking = true; + Server.Instance.blockingMode = distractionMode == "Full-blocking mode" ? true : false; + Server.Instance.SendMessageToAllClients($"{{\"action\": \"{(Server.Instance.blockingMode ? "BlockMode" : "noBlockMode")}\", \"status\": {(Server.Instance.blockingMode ? "true" : "false")}}}"); + + try + { + string relativePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "distractingWebsites.json"); + string jsonContent = File.ReadAllText(relativePath); + + // Erstelle ein Objekt für die Nachricht + var messageObject = new + { + action = "sendJsonContent", + status = true, + content = JsonSerializer.Deserialize<object>(jsonContent) // Parse den JSON-Inhalt + }; + + // Serialisiere das Objekt zu einem JSON-String + string message = JsonSerializer.Serialize(messageObject); + + Server.Instance.SendMessageToAllClients(message); + } + catch + { + + } } public static Session getSessionInstance() { diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs index b546777..c1de516 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs @@ -5,6 +5,7 @@ using System.Runtime; using System.Threading.Tasks; using System.Windows; using System.Windows.Navigation; +using InnoLabProjektDektopApp.Services.WebSocketServer; using Newtonsoft.Json; namespace InnoLabProjektDektopApp.Services @@ -166,6 +167,9 @@ namespace InnoLabProjektDektopApp.Services public string FinishSession() { + Server.Instance.SendMessageToAllClients("{\"action\": \"stopTracking\", \"status\": false}"); + Server.Instance.isTracking = false; + StopMonitoring(); if (IsBreak) { diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/WebSocketServer/Server.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/WebSocketServer/Server.cs index 7d172fb..fd3c3da 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/WebSocketServer/Server.cs +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/WebSocketServer/Server.cs @@ -1,9 +1,15 @@ using System; using System.Collections.Generic; +using System.ComponentModel.Design.Serialization; using System.Linq; +using System.Net.WebSockets; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using Fleck; +using Newtonsoft.Json.Linq; +using System.IO; namespace InnoLabProjektDektopApp.Services.WebSocketServer { @@ -12,6 +18,8 @@ namespace InnoLabProjektDektopApp.Services.WebSocketServer private static Server instance = null; private Fleck.WebSocketServer server; private List<IWebSocketConnection> clients = new List<IWebSocketConnection>(); + public bool isTracking = false; + public bool blockingMode = false; private Server() { @@ -37,9 +45,44 @@ namespace InnoLabProjektDektopApp.Services.WebSocketServer { Console.WriteLine($"Empfangen: {message}"); socket.Send("Hallo vom Server"); - }; - + try + { + var messageObj = JsonSerializer.Deserialize<WebSocketMessage>(message); + + Console.WriteLine($"Empfangen: {messageObj}"); + + if(messageObj != null && messageObj.action == "connected" && messageObj.status == true) + { + Console.WriteLine("connected"); + + SendMessageToAllClients($"{{\"action\": \"{(isTracking ? "startTracking" : "stopTracking")}\", \"status\": {isTracking}}}"); + SendMessageToAllClients($"{{\"action\": \"{(blockingMode ? "BlockMode" : "noBlockMode")}\", \"status\": {(blockingMode ? "true" : "false")}}}"); + + string relativePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "distractingWebsites.json"); + string jsonContent = File.ReadAllText(relativePath); + + // Erstelle ein Objekt für die Nachricht + var messageObject = new + { + action = "sendJsonContent", + status = true, + content = JsonSerializer.Deserialize<object>(jsonContent) // Parse den JSON-Inhalt + }; + + // Serialisiere das Objekt zu einem JSON-String + string response = JsonSerializer.Serialize(messageObject); + + SendMessageToAllClients(response); + + } + } + catch (JsonException ex) + { + Console.WriteLine($"Fehler beim Parsen der Nachricht: {ex.Message}"); + // Falls das Parsen fehlschlägt, kannst du den Fehler behandeln oder eine Default-Nachricht zurückgeben + } + }; }); } @@ -67,3 +110,9 @@ namespace InnoLabProjektDektopApp.Services.WebSocketServer } } } + +public class WebSocketMessage +{ + public string action { get; set; } + public bool status { get; set; } +} -- GitLab