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

Addon jetzt auch in git

parent 71af0d37
No related branches found
No related tags found
1 merge request!64Browser addon
let socket;
let trackingAktiv = false;
let isConnected = false;
let distraktions;
function connectWebSocket() {
if (isConnected) {
console.log("Bereits verbunden.");
return;
}
// Versuche, eine Verbindung herzustellen
socket = new WebSocket("ws://localhost:8080");
// Timeout für die Verbindung (z.B. 5 Sekunden)
let connectionTimeout = setTimeout(() => {
console.log("Verbindung timeout, Server reagiert nicht.");
socket.close(); // Schließt den WebSocket
isConnected = false; // Setzt isConnected auf false
}, 5000); // Timeout nach 5 Sekunden
socket.onopen = function() {
console.log("Verbunden mit der WPF-Anwendung.");
clearTimeout(connectionTimeout);
isConnected = true; // Markiere als verbunden
socket.send("Hallo vom Addon");
};
// Empfang von Nachrichten
socket.onmessage = function(event) {
const message = parseMessage(event.data);
console.log("Nachricht empfangen: ", message);
//navigator.clipboard.writeText(message);
if(message.action === "sendJsonContent") {
//navigator.clipboard.writeText(message);
distraktions = createDistractionList(message);
}
if (message.action === "startTracking" && message.status === true) {
console.log("Tracking gestartet.");
addTabListeners();
trackingAktiv = true;
} else if (message.action === "stopTracking" && message.status === false) {
console.log("Tracking gestoppt.");
removeTabListeners();
trackingAktiv = false;
}
};
socket.onclose = function() {
console.log("Verbindung geschlossen.");
isConnected = false;
// Versuche, nach einer kurzen Zeit erneut eine Verbindung herzustellen
setTimeout(connectWebSocket, 5000); // 5 Sekunden warten, bevor erneut versucht wird
};
socket.onerror = function(error) {
console.error("WebSocket Fehler:", error);
socket.close(); // Schließe den Socket, um den Fehler zu behandeln und es erneut zu versuchen
};
}
connectWebSocket();
addTabListeners();
function createDistractionList(message) {
const result = {};
for (content in message) {
const distractingUrls = message["content"]["Games"].filter(item => item.distracting).map(item => item.url);
if (distractingUrls.length > 0) {
result[content] = distractingUrls;
}
}
return result;
}
function parseMessage(message) {
try {
return JSON.parse(message);
} catch {
return message;
}
}
// Funktion zum Hinzufügen der Event-Listener
function addTabListeners() {
browser.tabs.onCreated.addListener(handleTabCreated);
browser.tabs.onUpdated.addListener(handleTabUpdated);
browser.tabs.onActivated.addListener(handlerTabActiv);
console.log("Tab-Listener hinzugefügt.");
}
// Funktion zum Entfernen der Event-Listener
function removeTabListeners() {
browser.tabs.onCreated.removeListener(handleTabCreated);
browser.tabs.onUpdated.removeListener(handleTabUpdated);
browser.tabs.onActivated.removeListener(handlerTabActiv);
console.log("Tab-Listener entfernt.");
}
function handleTabCreated(tab) {
checkUrl(tab.url, tab.tabId);
};
function handleTabUpdated(tabId, changeInfo, tab) {
if (changeInfo.status === 'complete') {
checkUrl(tab.url, tabId);
}
};
function handlerTabActiv(activeInfo) {
browser.tabs.get(activeInfo.tabId).then((tab) => {
checkUrl(tab.url, tab.tabId);
});
}
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 });
});
}
}
};
\ No newline at end of file
function showOverlay() {
// Erstelle das Overlay
let overlay = document.createElement('div');
overlay.style.position = 'fixed';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.width = '100vw';
overlay.style.height = '100vh';
overlay.style.backgroundColor = 'rgba(70, 65, 65, 0.8)';
overlay.style.zIndex = '9999';
overlay.style.display = 'flex'; // Wende Flexbox auf das Overlay an
overlay.style.justifyContent = 'center'; // Horizontale Zentrierung
overlay.style.alignItems = 'center'; // Vertikale Zentrierung
//overlay.style.pointerEvents = 'none'; // Klicks durchlassen, wenn nötig
//document.body.appendChild(overlay);
// Erstelle das Text-Element
let overlayText = document.createElement('div');
overlayText.innerText = "Diese Seite wird von CoFlow geblockt";
overlayText.style.textAlign = "Center";
overlayText.style.color = 'red';
overlayText.style.fontSize = '40px';
overlayText.style.padding = '5rem';
overlayText.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
overlayText.style.borderRadius = '10px';
// Füge den Text zum Overlay hinzu
overlay.appendChild(overlayText);
// Füge das Overlay zum Body hinzu
document.body.appendChild(overlay);
document.body.style.overflow = 'hidden';
document.documentElement.style.overflow = 'hidden';
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "showOverlay") {
showOverlay();
pauseVideo();
}
});
// Funktion zum Pausieren des Videos im Ziel-Tab
function pauseVideo(tabId) {
let video = document.querySelector('video');
if (video && !video.paused) {
video.pause(); // Pausiert das Video
console.log('Video auf Tab ' + document.title + ' pausiert.');
}
}
{
"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>"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"action": {
"default_popup": "popup.html",
"default_icon": "icon.png"
},
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'; connect-src 'self' ws://localhost:8080"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]
}
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>URL Kopierer</title>
<script src="app.js"></script>
<style>
body { font-family: Arial, sans-serif; margin: 10px; text-align: center; }
button { padding: 10px 20px; margin-top: 20px; cursor: pointer; }
</style>
</head>
<body>
<h1>URL kopieren</h1>
<button id="copy-url">Kopiere URL</button>
<!-- Overlay HTML -->
<div id="block-overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: black; z-index: 9999; pointer-events: auto; display: none;">
<!-- Optional: Text oder Ladebalken anzeigen -->
</div>
</body>
</html>
File added
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