Hey everyone,
I’m working on a Chrome extension that adds events to the user’s default Google Calendar. I’m running into a couple of issues and could use some help.
First, I’m getting an error about needing an image from the same domain in my HTML file. When I add one, it complains that the image needs to be from an HTTPS source. How do I fix this?
Second, I’m getting a Content Security Policy error when trying to insert an event. It mentions script sources, but I’ve already included some of the required ones. What am I missing?
function addEvent() {
const eventData = {
title: 'Team Meeting',
start: new Date('2023-06-15T10:00:00'),
end: new Date('2023-06-15T11:00:00'),
description: 'Weekly team sync-up'
};
// Code to insert event goes here
// But it's not working due to CSP error
}
I’ve been stuck on this for weeks. Any ideas on how to resolve these issues? Or is there another way to add events using JavaScript in a Chrome extension? Thanks!
I’ve been down this road before with Chrome extensions, and it can be tricky. For the image issue, have you considered using an SVG inline in your HTML? It’s lightweight and doesn’t require external loading. Just embed the SVG code directly in your HTML file.
Regarding the CSP error, make sure you’re using the correct API. Google Calendar API v3 is the way to go. You’ll need to set up OAuth 2.0 and get the necessary credentials. Don’t forget to add the calendar scope to your manifest:
“scopes”: [“https://www.googleapis.com/auth/calendar.events”]
Also, consider using the chrome.identity.getAuthToken method to handle authentication. It’s more seamless for Chrome extensions.
Lastly, double-check that you’re making the API call correctly. The endpoint should be something like:
https://www.googleapis.com/calendar/v3/calendars/primary/events
Hope this helps you get unstuck!
hey man, i had similar probs. for the image thing, try using a base64 encoded image in ur html. it’s like data:image/png;base64,…
for the csp error, check ur manifest.json. add these:
“content_security_policy”: “script-src ‘self’ https://apis.google.com; object-src ‘self’”
that shud fix it. good luck!
I’ve dealt with similar issues in Chrome extension development. For the image problem, you can use a data URI instead of an external image. This bypasses the HTTPS requirement. As for the CSP error, you need to update your manifest.json file to include necessary permissions and content security policy directives.
Here’s what I’d suggest for your manifest.json:
{
"permissions": [
"https://www.googleapis.com/",
"identity"
],
"content_security_policy": "script-src 'self' https://apis.google.com; object-src 'self'"
}
This should allow your extension to communicate with Google’s APIs. Also, make sure you’re using the chrome.identity API to handle OAuth. It’s more secure and adheres to Chrome’s policies for extensions. Hope this helps you move forward with your project!