Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudo-Ivan committed Oct 8, 2024
1 parent 60cc955 commit 0065ce6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 15 deletions.
4 changes: 4 additions & 0 deletions html/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ <h2>Advanced Features</h2>
<label for="caseInsensitiveEncryption">Case-insensitive Encryption (Codebook):</label>
<input type="checkbox" id="caseInsensitiveEncryption">
</div>
<div class="option-row">
<label for="messageCheckInterval">Check for new messages every (seconds):</label>
<input type="number" id="messageCheckInterval" min="1" max="3600" value="5">
</div>
</section>

<section>
Expand Down
35 changes: 35 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,39 @@ chrome.windows.onRemoved.addListener((windowId) => {
if (windowId === popupWindowId) {
popupWindowId = null;
}
});

let messageCheckIntervalId = null;

function startMessageCheckInterval(interval) {
if (messageCheckIntervalId) {
clearInterval(messageCheckIntervalId);
}
messageCheckIntervalId = setInterval(checkForNewMessages, interval * 1000);
}

function checkForNewMessages() {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, { action: 'checkForNewMessages' });
}
});
}

chrome.runtime.onInstalled.addListener(() => {
// ... existing code ...
chrome.storage.local.get('messageCheckInterval', (result) => {
const interval = result.messageCheckInterval || 5;
startMessageCheckInterval(interval);
});
});

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'updateMessageCheckInterval') {
chrome.storage.local.get('messageCheckInterval', (result) => {
const interval = result.messageCheckInterval || 5;
startMessageCheckInterval(interval);
});
}
// ... existing message handling ...
});
52 changes: 40 additions & 12 deletions js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ CrypticChat.init = function() {
'messageBubbleColor',
'messageBubbleOpacity',
'userColors',
'codebook'
'codebook',
'messageCheckInterval' // Add this line
], function(items) {
CrypticChat.messagesToLoad = items.messagesToLoad || 50;
CrypticChat.autoScroll = items.autoScroll !== false;
Expand All @@ -562,14 +563,50 @@ CrypticChat.init = function() {
CrypticChat.messageBubbleOpacity = items.messageBubbleOpacity || 70;
CrypticChat.userColors = items.userColors || [];
CrypticChat.codebook = items.codebook || {};
CrypticChat.messageCheckInterval = items.messageCheckInterval || 5; // Add this line

CrypticChat.updateAllStyles();
CrypticChat.reloadMessages();
});

setInterval(CrypticChat.reloadMessages, 5000);
// Update the interval
if (CrypticChat.messageCheckIntervalId) {
clearInterval(CrypticChat.messageCheckIntervalId);
}
CrypticChat.messageCheckIntervalId = setInterval(CrypticChat.reloadMessages, CrypticChat.messageCheckInterval * 1000);
});
};

// Add a listener for storage changes to update the interval in real-time
chrome.storage.onChanged.addListener(function(changes, namespace) {
if (namespace === 'local' && changes.messageCheckInterval) {
CrypticChat.messageCheckInterval = changes.messageCheckInterval.newValue;
if (CrypticChat.messageCheckIntervalId) {
clearInterval(CrypticChat.messageCheckIntervalId);
}
CrypticChat.messageCheckIntervalId = setInterval(CrypticChat.reloadMessages, CrypticChat.messageCheckInterval * 1000);
}
});

// Modify the existing listener to handle interval updates
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'reloadMessages') {
CrypticChat.reloadMessages();
} else if (request.action === 'updateStyles') {
CrypticChat.applyOptions();
CrypticChat.updateAllStyles();
} else if (request.action === 'checkForNewMessages') {
CrypticChat.reloadMessages();
} else if (request.action === 'updateMessageCheckInterval') {
chrome.storage.local.get('messageCheckInterval', (result) => {
CrypticChat.messageCheckInterval = result.messageCheckInterval || 5;
if (CrypticChat.messageCheckIntervalId) {
clearInterval(CrypticChat.messageCheckIntervalId);
}
CrypticChat.messageCheckIntervalId = setInterval(CrypticChat.reloadMessages, CrypticChat.messageCheckInterval * 1000);
});
}
});

CrypticChat.init();

const chatObserver = new MutationObserver((mutations) => {
Expand Down Expand Up @@ -609,15 +646,6 @@ if (appMount) {
channelSwitchObserver.observe(appMount, { childList: true, subtree: true });
}

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'reloadMessages') {
CrypticChat.reloadMessages();
} else if (request.action === 'updateStyles') {
CrypticChat.applyOptions();
CrypticChat.updateAllStyles();
}
});

// Add a listener for storage changes to update options in real-time
chrome.storage.onChanged.addListener(function(changes, namespace) {
if (namespace === 'local') {
Expand Down
11 changes: 8 additions & 3 deletions js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ document.addEventListener('DOMContentLoaded', function() {
});

// Add event listeners for auto-save
['messagesToLoad', 'autoScroll', 'backgroundColor', 'inputBoxColor', 'headerColor', 'windowTransparency', 'autoSend', 'messageSpacing', 'messageBubbleColor', 'messageBubbleOpacity'].forEach(id => {
['messagesToLoad', 'autoScroll', 'backgroundColor', 'inputBoxColor', 'headerColor', 'windowTransparency', 'autoSend', 'messageSpacing', 'messageBubbleColor', 'messageBubbleOpacity', 'messageCheckInterval'].forEach(id => {
document.getElementById(id).addEventListener('change', debounce(autoSave, 500));
document.getElementById(id).addEventListener('input', debounce(autoSave, 500));
});
Expand Down Expand Up @@ -132,13 +132,16 @@ function saveOptions(showMessage = true) {
userColors: getUserColors(),
mutedUsers: getMutedUsers(),
autoSend: document.getElementById('autoSend').checked,
caseInsensitiveEncryption: document.getElementById('caseInsensitiveEncryption').checked
caseInsensitiveEncryption: document.getElementById('caseInsensitiveEncryption').checked,
messageCheckInterval: document.getElementById('messageCheckInterval').value,
};

chrome.storage.local.set(options, function() {
if (showMessage) {
showStatus('Options saved');
}
// Notify background script to update the message check interval
chrome.runtime.sendMessage({ action: 'updateMessageCheckInterval' });
});
}

Expand All @@ -164,7 +167,8 @@ function loadOptions() {
'crypticPhrase',
'messageBubbleColor',
'messageBubbleOpacity',
'caseInsensitiveEncryption'
'caseInsensitiveEncryption',
'messageCheckInterval',
], function(items) {
document.getElementById('codebookText').value = formatCodebook(items.codebook || {});
document.getElementById('messagesToLoad').value = items.messagesToLoad || 50;
Expand All @@ -187,6 +191,7 @@ function loadOptions() {
document.getElementById('messageBubbleColor').value = items.messageBubbleColor || '#1e1e3f';
document.getElementById('messageBubbleOpacity').value = items.messageBubbleOpacity || '70';
document.getElementById('caseInsensitiveEncryption').checked = items.caseInsensitiveEncryption || false;
document.getElementById('messageCheckInterval').value = items.messageCheckInterval || 5;
});
}

Expand Down

0 comments on commit 0065ce6

Please sign in to comment.