Files
2025-10-29 19:26:50 +01:00

54 lines
1.7 KiB
JavaScript

// This is the background script for the extension
console.log('Background script running');
// Listen for messages from the content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'search') {
const query = message.query;
const apiUrl = `http://127.0.0.1:5001/api/search?q=${encodeURIComponent(query)}`; // Add the http:// protocol to the URL
console.log(`Searching for: ${query}`);
fetch(apiUrl)
.then(response => response.json())
.then(data => {
const results = data.results || [];
console.log('Search results:', results);
sendResponse({ results });
// Send the results to the popup
chrome.runtime.sendMessage({ action: 'displayResults', results });
})
.catch(error => {
console.error('Error fetching search results:', error);
sendResponse({ results: [] });
});
// Return true to indicate that the response will be sent asynchronously
return true;
}
});
// Periodically check the active tab's URL every 0.5 seconds
setInterval(() => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs.length > 0) {
const activeTab = tabs[0];
const url = new URL(activeTab.url);
const hostname = url.hostname;
// List of supported sites
const supportedSites = [
'mangadex.org',
'anilist.co',
'kitsu.app',
'myanimelist.net'
];
if (supportedSites.includes(hostname)) {
console.log(`Sending addLens message to content script for hostname: ${hostname}`);
chrome.tabs.sendMessage(activeTab.id, { action: 'addLens', hostname });
}
}
});
}, 500);