history.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. class MessageHistory {
  2. constructor(maxSize = 512) {
  3. this.maxSize = maxSize;
  4. this.history = [];
  5. }
  6. // Add a new message to history
  7. addMessage(message) {
  8. // format timestamp to string
  9. message.timestamp = message.timestamp.toISOString();
  10. this.history.push(message);
  11. // Prune old messages if we exceed maxSize
  12. if (this.history.length > this.maxSize) {
  13. this.history = this.history.slice(-this.maxSize);
  14. }
  15. // Save to chrome storage
  16. this.saveToStorage();
  17. }
  18. // Load history from storage
  19. async loadHistory() {
  20. return new Promise((resolve) => {
  21. chrome.storage.local.get(['messageHistory'], (result) => {
  22. if (result.messageHistory) {
  23. this.history = result.messageHistory;
  24. }
  25. resolve(this.history);
  26. });
  27. });
  28. }
  29. // Save current history to storage
  30. saveToStorage() {
  31. chrome.storage.local.set({ messageHistory: this.history });
  32. }
  33. // Clear all history
  34. clearHistory() {
  35. this.history = [];
  36. this.saveToStorage();
  37. }
  38. // Get all history
  39. getHistory() {
  40. return this.history;
  41. }
  42. }