Skip to content
Snippets Groups Projects
Commit df56a359 authored by Marcel Kehrberg's avatar Marcel Kehrberg
Browse files

Merge branch 'BrowserAddonV1.1' into 'develop'

Änderungen

See merge request !68
parents 8090b1c3 fa23741f
No related branches found
No related tags found
1 merge request!68Änderungen
No preview for this file type
......@@ -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
{
"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>"],
......
......@@ -268,30 +268,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
{
}
......@@ -427,6 +404,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() {
......
......@@ -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)
{
......
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; }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment