// Popup script for SecureBit Chat Extension
// Handles the popup UI and communication with background script
class SecureBitPopup {
constructor() {
this.isConnected = false;
this.currentChannel = null;
this.settings = {};
this.initializePopup();
}
async initializePopup() {
try {
// Hide loading and show content
document.getElementById('loading').classList.add('hidden');
document.getElementById('app-content').classList.remove('hidden');
// Load settings
await this.loadSettings();
// Initialize the popup UI
this.renderPopup();
// Set up event listeners
this.setupEventListeners();
// Check connection status
await this.checkConnectionStatus();
} catch (error) {
console.error('Popup initialization error:', error);
this.showError('Failed to initialize SecureBit Chat');
}
}
async loadSettings() {
try {
const response = await this.sendMessage({ action: 'getSettings' });
this.settings = response.data || {};
} catch (error) {
console.error('Failed to load settings:', error);
this.settings = {};
}
}
renderPopup() {
const appContent = document.getElementById('app-content');
appContent.innerHTML = `
SecureBit Chat
`;
}
setupEventListeners() {
// Settings toggle
document.getElementById('settings-btn').addEventListener('click', () => {
const settingsPanel = document.getElementById('settings-panel');
settingsPanel.classList.toggle('hidden');
});
// Create channel
document.getElementById('create-channel-btn').addEventListener('click', () => {
this.createChannel();
});
// Join channel
document.getElementById('join-channel-btn').addEventListener('click', () => {
this.joinChannel();
});
// Scan QR
document.getElementById('scan-qr-btn').addEventListener('click', () => {
this.showQRScanner();
});
// Close QR scanner
document.getElementById('close-qr-scanner').addEventListener('click', () => {
this.hideQRScanner();
});
// Save settings
document.getElementById('save-settings-btn').addEventListener('click', () => {
this.saveSettings();
});
// Load saved settings into form
this.loadSettingsIntoForm();
}
async checkConnectionStatus() {
try {
// This would check if there's an active WebRTC connection
// For now, we'll simulate the status
this.updateConnectionStatus(false);
} catch (error) {
console.error('Connection check error:', error);
this.updateConnectionStatus(false);
}
}
updateConnectionStatus(connected) {
this.isConnected = connected;
const indicator = document.getElementById('status-indicator');
const text = document.getElementById('status-text');
if (connected) {
indicator.className = 'w-3 h-3 rounded-full bg-green-500 mr-3';
text.textContent = 'Connected';
} else {
indicator.className = 'w-3 h-3 rounded-full bg-red-500 mr-3';
text.textContent = 'Disconnected';
}
}
async createChannel() {
try {
// Generate a new channel ID
const channelId = this.generateChannelId();
// Show notification
await this.sendMessage({
action: 'showNotification',
data: {
title: 'Channel Created',
message: `Channel ID: ${channelId}`
}
});
// Update UI
this.updateConnectionStatus(true);
this.currentChannel = channelId;
// Add to recent channels
this.addToRecentChannels(channelId, 'New Channel');
} catch (error) {
console.error('Create channel error:', error);
this.showError('Failed to create channel');
}
}
async joinChannel() {
// This would open a QR scanner or input field
this.showQRScanner();
}
showQRScanner() {
document.getElementById('qr-scanner-modal').classList.remove('hidden');
// Here you would initialize the QR scanner
this.initializeQRScanner();
}
hideQRScanner() {
document.getElementById('qr-scanner-modal').classList.add('hidden');
}
initializeQRScanner() {
// Initialize QR scanner functionality
// This would integrate with your existing QR scanner component
console.log('QR Scanner initialized');
}
addToRecentChannels(channelId, name) {
const channelsList = document.getElementById('channels-list');
const channelElement = document.createElement('div');
channelElement.className = 'flex items-center justify-between p-2 bg-gray-800 rounded-lg hover:bg-gray-700 cursor-pointer';
channelElement.innerHTML = `
`;
channelsList.appendChild(channelElement);
}
loadSettingsIntoForm() {
document.getElementById('auto-connect').checked = this.settings.autoConnect || false;
document.getElementById('notifications').checked = this.settings.notifications || false;
document.getElementById('theme-select').value = this.settings.theme || 'dark';
}
async saveSettings() {
try {
const newSettings = {
autoConnect: document.getElementById('auto-connect').checked,
notifications: document.getElementById('notifications').checked,
theme: document.getElementById('theme-select').value
};
await this.sendMessage({
action: 'updateSettings',
data: newSettings
});
this.settings = { ...this.settings, ...newSettings };
this.showSuccess('Settings saved');
} catch (error) {
console.error('Save settings error:', error);
this.showError('Failed to save settings');
}
}
generateChannelId() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
async sendMessage(message) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(message, (response) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
} else {
resolve(response);
}
});
});
}
showError(message) {
// Simple error notification
console.error(message);
// You could implement a toast notification here
}
showSuccess(message) {
// Simple success notification
console.log(message);
// You could implement a toast notification here
}
}
// Initialize popup when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new SecureBitPopup();
});