2025-10-19 21:02:06 +02:00
|
|
|
import 'dart:html' as html;
|
|
|
|
|
import '../models/tab_data.dart';
|
|
|
|
|
|
2025-10-26 19:10:23 +01:00
|
|
|
|
|
|
|
|
// communication with browser extension
|
|
|
|
|
// Listens for messages
|
|
|
|
|
// callbacks to notify UI
|
|
|
|
|
// start/stop tracking
|
|
|
|
|
// Converts extension
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-19 21:02:06 +02:00
|
|
|
class ExtensionService {
|
|
|
|
|
Function(List<TabData>)? onTabsUpdate;
|
|
|
|
|
Function()? onTrackingStart;
|
|
|
|
|
Function()? onTrackingStop;
|
|
|
|
|
|
|
|
|
|
void setupListener() {
|
|
|
|
|
html.window.onMessage.listen((event) {
|
|
|
|
|
final data = event.data;
|
|
|
|
|
if (data is Map && data['source'] == 'tab-tracker-extension') {
|
|
|
|
|
_handleExtensionMessage(Map<String, dynamic>.from(data));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _handleExtensionMessage(Map<String, dynamic> data) {
|
|
|
|
|
print('Received from extension: $data');
|
|
|
|
|
|
|
|
|
|
if (data['action'] == 'updateTabs') {
|
|
|
|
|
final extensionTabs = (data['tabs'] as List).map((tab) {
|
|
|
|
|
tab['type'] = 'tab';
|
|
|
|
|
return TabData.fromJson(tab);
|
|
|
|
|
}).toList();
|
|
|
|
|
|
|
|
|
|
onTabsUpdate?.call(extensionTabs);
|
|
|
|
|
|
|
|
|
|
} else if (data['action'] == 'clear') {
|
|
|
|
|
onTrackingStop?.call();
|
|
|
|
|
|
|
|
|
|
} else if (data['response'] != null) {
|
|
|
|
|
final response = data['response'];
|
|
|
|
|
if (response['status'] == 'started') {
|
|
|
|
|
onTrackingStart?.call();
|
|
|
|
|
} else if (response['status'] == 'stopped') {
|
|
|
|
|
onTrackingStop?.call();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sendMessage(Map<String, dynamic> message) {
|
|
|
|
|
html.window.postMessage({
|
|
|
|
|
'source': 'tab-tracker-webapp',
|
|
|
|
|
...message
|
|
|
|
|
}, '*');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startTracking() {
|
|
|
|
|
sendMessage({'action': 'startTracking'});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stopTracking() {
|
|
|
|
|
sendMessage({'action': 'stopTracking'});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void getStatus() {
|
|
|
|
|
sendMessage({'action': 'getStatus'});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-27 21:11:21 +01:00
|
|
|
// Handles communication, browser extension for live tab tracking.
|
2025-10-19 21:02:06 +02:00
|
|
|
//
|
2025-10-27 21:11:21 +01:00
|
|
|
// Callback triggered when extension updates.
|
2025-10-19 21:02:06 +02:00
|
|
|
//
|
2025-10-27 21:11:21 +01:00
|
|
|
// SetupListener listens for messages from browser extension through window.postMessage.
|
2025-10-19 21:02:06 +02:00
|
|
|
//
|
2025-10-27 21:11:21 +01:00
|
|
|
// Messages arrive, filters only process ones from specific extension.
|
2025-10-19 21:02:06 +02:00
|
|
|
//
|
2025-10-27 21:11:21 +01:00
|
|
|
// _handleExtensionMessage processes different types of messages, tab updates, tracking status changes.
|
2025-10-19 21:02:06 +02:00
|
|
|
//
|
2025-10-27 21:11:21 +01:00
|
|
|
// SendMessage method sends commands back to the extension with a source identifier.
|
2025-10-19 21:02:06 +02:00
|
|
|
//
|
2025-10-27 21:11:21 +01:00
|
|
|
// startTracking, stopTracking, getStatus interface to control extension
|
|
|
|
|
// callback allows UI to react immediately when data arrives.
|