chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { const data = message.data; data.url = message.url; if (message.action === 'saveMedia') { fetch('https://media.gregoryleeman.com/api/items', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) .then((response) => response.json()) .then((responseData) => { console.log('Success:', responseData); sendResponse({ success: true, responseData }); }) .catch((error) => { console.error('Error:', error); sendResponse({ success: false, error }); }); return true; // Keep the message channel open for async response } else if ( message.action == 'checkMedia' ) { fetch('https://media.gregoryleeman.com/api/items/check?url=' + encodeURIComponent(data.url), { method: 'GET', headers: { 'Content-Type': 'application/json' } }) .then((response) => response.json()) .then((responseData) => { console.log('Success:', responseData); sendResponse({ success: true, responseData }); }) .catch((error) => { console.error('Error:', error); sendResponse({ success: false, error }); }); return true; // Keep the message channel open for async response } });