113 lines
No EOL
3.5 KiB
Dart
113 lines
No EOL
3.5 KiB
Dart
import 'dart:html' as html;
|
|
import 'dart:convert';
|
|
import 'dart:js_util' as js_util;
|
|
import '../models/tab_data.dart';
|
|
|
|
class BrowserApiService {
|
|
Future<List<TabData>> getTabs() async {
|
|
try {
|
|
final result = await _callBrowserAPI('getTabs');
|
|
if (result != null) {
|
|
final List<dynamic> data = json.decode(result);
|
|
return data.map((item) {
|
|
item['type'] = 'tab';
|
|
return TabData.fromJson(item);
|
|
}).toList();
|
|
}
|
|
} catch (e) {
|
|
print('Error getting tabs: $e');
|
|
}
|
|
return [];
|
|
}
|
|
|
|
Future<List<TabData>> getBookmarks() async {
|
|
try {
|
|
final result = await _callBrowserAPI('getBookmarks');
|
|
if (result != null) {
|
|
final List<dynamic> data = json.decode(result);
|
|
return data.map((item) {
|
|
item['type'] = 'bookmark';
|
|
return TabData.fromJson(item);
|
|
}).toList();
|
|
}
|
|
} catch (e) {
|
|
print('Error getting bookmarks: $e');
|
|
}
|
|
return [];
|
|
}
|
|
|
|
Future<List<TabData>> getHistory() async {
|
|
try {
|
|
final result = await _callBrowserAPI('getHistory', [100]);
|
|
if (result != null) {
|
|
final List<dynamic> data = json.decode(result);
|
|
return data.map((item) {
|
|
item['type'] = 'history';
|
|
return TabData.fromJson(item);
|
|
}).toList();
|
|
}
|
|
} catch (e) {
|
|
print('Error getting history: $e');
|
|
}
|
|
return [];
|
|
}
|
|
|
|
Future<void> switchToTab(String tabId) async {
|
|
await _callBrowserAPI('switchToTab', [tabId]);
|
|
}
|
|
|
|
Future<void> openTab(String url) async {
|
|
await _callBrowserAPI('openTab', [url]);
|
|
}
|
|
|
|
Future<void> closeTab(String tabId) async {
|
|
await _callBrowserAPI('closeTab', [tabId]);
|
|
}
|
|
|
|
Future<void> removeBookmark(String bookmarkId) async {
|
|
await _callBrowserAPI('removeBookmark', [bookmarkId]);
|
|
}
|
|
|
|
Future<void> togglePinTab(String tabId, bool pin) async {
|
|
await _callBrowserAPI('togglePinTab', [tabId, pin]);
|
|
}
|
|
|
|
Future<String?> _callBrowserAPI(String method, [List<dynamic>? args]) async {
|
|
try {
|
|
if (!js_util.hasProperty(html.window, 'BrowserAPI')) {
|
|
print('BrowserAPI not found - running in development mode');
|
|
return null;
|
|
}
|
|
|
|
final browserAPI = js_util.getProperty(html.window, 'BrowserAPI');
|
|
final function = js_util.getProperty(browserAPI, method);
|
|
|
|
final result = args == null
|
|
? await js_util.promiseToFuture(js_util.callMethod(function, 'call', [browserAPI]))
|
|
: await js_util.promiseToFuture(js_util.callMethod(function, 'call', [browserAPI, ...args]));
|
|
|
|
return json.encode(result);
|
|
} catch (e) {
|
|
print('Error calling $method: $e');
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
// This service acts as a bridge between our Flutter app and the browser extension.
|
|
//
|
|
// It provides clean methods to interact with browser APIs for tabs, bookmarks, and history.
|
|
//
|
|
// The getTabs, getBookmarks, and getHistory methods fetch data from the browser and convert it to TabData objects.
|
|
//
|
|
// Action methods like switchToTab, openTab, closeTab allow us to control browser tabs programmatically.
|
|
//
|
|
// The private _callBrowserAPI method handles the low-level JavaScript communication.
|
|
//
|
|
// It uses dart:js_util to call JavaScript functions exposed by the browser extension through window.BrowserAPI.
|
|
//
|
|
// All methods handle errors gracefully and return empty results if the API is unavailable.
|
|
//
|
|
// This abstraction keeps browser-specific code separate from our UI logic making the app easier to maintain.
|
|
//
|
|
// When running in development without the extension, it prints debug messages instead of crashing. |