have this bit of code in a chrome extension background.js file, it should only run when a button is clicked that I'm loading onto the page using contentScript.js in the service worker I only see 4 API calls per button press (there are two videos on the page), there's no way I'm using this more than 500 times let alone 10,000 per day yet i'm reaching the quota limit. Any ideas?
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'SEARCH_YOUTUBE') {
const title = request.title;
console.log('about to call searchyoutube w/ title: ' + title);
searchYouTube(title)
.then(viewCount => {
sendResponse({ viewCount });
})
.catch(err => {
console.error('Error searching YouTube:', err);
sendResponse({ viewCount: null });
});
return true; // Keep the message channel open for async response
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'SEARCH_YOUTUBE') {
const title = request.title;
console.log('about to call searchYouTube w/ title: ' + title);
searchYouTube(title)
.then(viewCount => {
sendResponse({ viewCount });
})
.catch(err => {
console.error('Error searching YouTube:', err);
sendResponse({ viewCount: null });
});
return true; // Keep the message channel open for async response
}
});
async function searchYouTube(query) {
console.log('YouTube search is running with query: ' + query);
const searchUrl = `https://www.googleapis.com/youtube/v3/search?part=snippet&q=${encodeURIComponent(query)}&type=video&key=${API_KEY}&maxResults=1`;
const searchResp = await fetch(searchUrl);
const searchData = await searchResp.json();
if (!searchData.items || searchData.items.length === 0) {
return null; // No results found
}
const videoItem = searchData.items[0];
const videoTitle = videoItem.snippet.title.trim();
// Check if the query matches the video title exactly
console.log(decodeHtml(query) + ' vs ' + decodeHtml(videoTitle));
if (decodeHtml(videoTitle).toLowerCase() !== decodeHtml(query).toLowerCase()) {
return null; // No exact match
}
const videoId = videoItem.id.videoId;
// Get video statistics
const statsUrl = `https://www.googleapis.com/youtube/v3/videos?part=statistics&id=${videoId}&key=${API_KEY}`;
const statsResp = await fetch(statsUrl);
const statsData = await statsResp.json();
if (!statsData.items || statsData.items.length === 0) {
return null;
}
const viewCount = statsData.items[0].statistics.viewCount;
return viewCount;
}
function decodeHtml(html) {
const entities = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'"
// Add more entities as needed
};
return html.replace(/&[a-zA-Z0-9#]+;/g, (match) => entities[match] || match);
}
as per quota cost page Search list costs 100 quota points.
10000 / 100 = 100
That means that you can make max 100 calls per day. The quota resets at mid night west cost USA time. Being that your using javascript even a refresh of the page is going to rerun the calls.
Remember this is 10k is the development quota. You can request an extension.