|
1 | | -import optionsStorage from './options-storage.js'; |
| 1 | +console.log('💌 "she ve seen it" loaded'); |
2 | 2 |
|
3 | | -console.log('💈 Content script loaded for', chrome.runtime.getManifest().name); |
| 3 | +// Fetch inbox conversations |
| 4 | +async function fetchInbox() { |
| 5 | + try { |
| 6 | + const response = await fetch('https://fetlife.com/api/v1/conversations'); |
| 7 | + const data = await response.json(); |
| 8 | + return data; |
| 9 | + } catch (error) { |
| 10 | + console.error('Error fetching inbox:', error); |
| 11 | + return null; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +// Fetch user activity |
| 16 | +async function fetchUserActivity(userId) { |
| 17 | + try { |
| 18 | + const response = await fetch(`https://fetlife.com/api/v2/members/${userId}/feed`); |
| 19 | + const data = await response.json(); |
| 20 | + return data; |
| 21 | + } catch (error) { |
| 22 | + console.error(`Error fetching activity for user ${userId}:`, error); |
| 23 | + return null; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Determine if user has likely seen the message |
| 28 | +function hasSeenMessage(lastMessageTime, lastActivityTime) { |
| 29 | + // If user was active after the message was sent, they probably saw it |
| 30 | + return lastActivityTime > lastMessageTime; |
| 31 | +} |
| 32 | + |
| 33 | +// Get the most recent activity timestamp from user's feed |
| 34 | +function getLastActivityTime(activityData) { |
| 35 | + if (!activityData || !activityData.story_groups || activityData.story_groups.length === 0) { |
| 36 | + return 0; |
| 37 | + } |
| 38 | + |
| 39 | + // Get the timestamp from the first story (most recent) |
| 40 | + const firstStory = activityData.story_groups[0].stories[0]; |
| 41 | + return firstStory.timestamp / 1000; // Convert microseconds to seconds |
| 42 | +} |
| 43 | + |
| 44 | +// Main function to process conversations |
| 45 | +async function processConversations() { |
| 46 | + const inboxData = await fetchInbox(); |
| 47 | + |
| 48 | + if (!inboxData || !inboxData.conversations) { |
| 49 | + console.error('No inbox data available'); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const results = []; |
| 54 | + |
| 55 | + for (const conversation of inboxData.conversations) { |
| 56 | + // Check if the last message was sent by the current user |
| 57 | + const currentUserId = conversation.you.id; |
| 58 | + const lastMessage = conversation.last_message; |
| 59 | + |
| 60 | + if (lastMessage.author.id === currentUserId) { |
| 61 | + // Get the other user(s) in the conversation |
| 62 | + const otherUsers = conversation.with_users; |
| 63 | + |
| 64 | + for (const otherUser of otherUsers) { |
| 65 | + const activityData = await fetchUserActivity(otherUser.id); |
| 66 | + const lastActivityTime = getLastActivityTime(activityData); |
| 67 | + const lastMessageTime = lastMessage.created_at; |
| 68 | + |
| 69 | + const seen = hasSeenMessage(lastMessageTime, lastActivityTime); |
| 70 | + |
| 71 | + results.push({ |
| 72 | + conversationId: conversation.conversation_id, |
| 73 | + userId: otherUser.id, |
| 74 | + nickname: otherUser.nickname, |
| 75 | + lastMessageTime, |
| 76 | + lastActivityTime, |
| 77 | + probablySeen: seen |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return results; |
| 84 | +} |
| 85 | + |
| 86 | +// Add visual indicators to the inbox page |
| 87 | +async function addIndicators() { |
| 88 | + const results = await processConversations(); |
| 89 | + |
| 90 | + if (!results) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + // Wait for the inbox UI to be ready |
| 95 | + await waitForElement('[data-component="Conversations"]'); |
| 96 | + |
| 97 | + // Add indicators for each conversation |
| 98 | + for (const result of results) { |
| 99 | + addIndicatorToConversation(result); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Wait for an element to exist in the DOM |
| 104 | +function waitForElement(selector, timeout = 10000) { |
| 105 | + return new Promise((resolve, reject) => { |
| 106 | + const element = document.querySelector(selector); |
| 107 | + if (element) { |
| 108 | + resolve(element); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + const observer = new MutationObserver(() => { |
| 113 | + const element = document.querySelector(selector); |
| 114 | + if (element) { |
| 115 | + observer.disconnect(); |
| 116 | + resolve(element); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + observer.observe(document.body, { |
| 121 | + childList: true, |
| 122 | + subtree: true |
| 123 | + }); |
| 124 | + |
| 125 | + setTimeout(() => { |
| 126 | + observer.disconnect(); |
| 127 | + reject(new Error('Element not found within timeout')); |
| 128 | + }, timeout); |
| 129 | + }); |
| 130 | +} |
| 131 | + |
| 132 | +// Add indicator to a specific conversation |
| 133 | +function addIndicatorToConversation(result) { |
| 134 | + // Find the conversation element by conversation ID |
| 135 | + const conversationLinks = document.querySelectorAll('a[href*="/conversations/"]'); |
| 136 | + |
| 137 | + for (const link of conversationLinks) { |
| 138 | + if (link.href.includes(`/conversations/${result.conversationId}`)) { |
| 139 | + // Find a suitable place to add the indicator |
| 140 | + const parent = link.closest('li') || link.closest('div[class*="conversation"]'); |
| 141 | + |
| 142 | + if (parent && !parent.querySelector('.seen-indicator')) { |
| 143 | + const indicator = document.createElement('div'); |
| 144 | + indicator.className = 'seen-indicator'; |
| 145 | + |
| 146 | + if (result.probablySeen) { |
| 147 | + indicator.classList.add('seen'); |
| 148 | + indicator.innerHTML = '👁️ Probably seen'; |
| 149 | + indicator.title = `Last active: ${new Date(result.lastActivityTime * 1000).toLocaleString()}`; |
| 150 | + } else { |
| 151 | + indicator.classList.add('not-seen'); |
| 152 | + indicator.innerHTML = '⏱️ Probably not seen'; |
| 153 | + indicator.title = `Last active: ${new Date(result.lastActivityTime * 1000).toLocaleString()}`; |
| 154 | + } |
| 155 | + |
| 156 | + parent.appendChild(indicator); |
| 157 | + } |
| 158 | + |
| 159 | + break; |
| 160 | + } |
| 161 | + } |
| 162 | +} |
4 | 163 |
|
| 164 | +// Initialize |
5 | 165 | async function init() { |
6 | | - const options = await optionsStorage.getAll(); |
7 | | - const color = `rgb(${options.colorRed}, ${options.colorGreen},${options.colorBlue})`; |
8 | | - const text = options.text; |
9 | | - const notice = document.createElement('div'); |
10 | | - notice.innerHTML = text; |
11 | | - document.body.prepend(notice); |
12 | | - notice.id = 'text-notice'; |
13 | | - notice.style.border = '2px solid ' + color; |
14 | | - notice.style.color = color; |
| 166 | + // Check if we're on the inbox page |
| 167 | + if (window.location.pathname === '/inbox' || window.location.pathname.startsWith('/inbox')) { |
| 168 | + console.log('Processing inbox...'); |
| 169 | + await addIndicators(); |
| 170 | + } |
15 | 171 | } |
16 | 172 |
|
17 | 173 | init(); |
0 commit comments