Skip to content

Commit

Permalink
Switch to links for adding to gcal
Browse files Browse the repository at this point in the history
  • Loading branch information
TyHil committed Dec 11, 2024
1 parent b8ece12 commit 6bd6bd4
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 253 deletions.
1 change: 0 additions & 1 deletion .env.chrome

This file was deleted.

1 change: 0 additions & 1 deletion .env.firefox

This file was deleted.

8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@
}
},
"manifest": {
"oauth2": {
"client_id": "$CLIENT_ID",
"scopes":["openid", "https://www.googleapis.com/auth/calendar.events"]
},
"permissions": [
"identity",
"storage",
"scripting",
"webNavigation",
Expand All @@ -114,7 +109,6 @@
"id": "skedge@utdnebula",
"strict_min_version": "109.0"
}
},
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvAmiiOsDH3dFqL26yZgZVRJd+NnnlA2l1x0K9HEFa3UgQ98xI+BOpE+NZuH6I7zSuA+vv+vwFnjj30pRTTUlsrDrrKLR80Rgs6/cZojz0o84E6EtWgAOx2g4BCwesvrJ51WoDz+kLOmSI29DNP/oYM0jstZlYuOvFswron9z48n12nhdg5KI9d0i5q0xYBgB8Hbmi2cKKPnW5urDIVez+D7GqIBXFAVMfas8FanTAqVvQ+c9UZiywauaolf+a1wia3D4pBQxztm/uWH23QIWFeHYxywmxZPKqYOBo1XyZYJAW4FaZgkUuMWSZXDUZk/oGELIVdQ8mzaXKDo+9ug97wIDAQAB"
}
}
}
91 changes: 29 additions & 62 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Storage } from '@plasmohq/storage';

import {
addGCalButtons,
type CourseHeader,
listenForTableChange,
scrapeCourseData,
} from '~content';
import { neededOrigins } from '~data/config';
// import { addGoogleOAuth } from '~popup';

export interface ShowCourseTabPayload {
header: CourseHeader;
Expand All @@ -24,14 +24,25 @@ const realBrowser = process.env.PLASMO_BROWSER === 'chrome' ? chrome : browser;

/** Injects the content script if we hit a course page */
realBrowser.webNavigation.onHistoryStateUpdated.addListener((details) => {
if (
const onOptions =
/^.*:\/\/utdallas\.collegescheduler\.com\/terms\/.*\/courses\/.+$/.test(
details.url,
) ||
);
const onCurrentSchedule =
/^.*:\/\/utdallas\.collegescheduler\.com\/terms\/.*\/currentschedule$/.test(
details.url,
)
) {
);
if (onOptions) {
//Listen for table change to rescrape data
realBrowser.tabs.sendMessage(details.tabId, 'disconnectObserver');
realBrowser.scripting.executeScript({
target: {
tabId: details.tabId,
},
func: listenForTableChange,
});
}
if (onOptions || onCurrentSchedule) {
//Scrape data
realBrowser.scripting.executeScript(
{
Expand All @@ -50,21 +61,25 @@ realBrowser.webNavigation.onHistoryStateUpdated.addListener((details) => {
}
},
);
//Listen for table change to rescrape data
realBrowser.tabs.sendMessage(details.tabId, 'disconnectObserver');
realBrowser.scripting.executeScript({
target: {
tabId: details.tabId,
},
func: listenForTableChange,
});
//Store tab info
realBrowser.action.setBadgeText({ text: '!' });
realBrowser.action.setBadgeBackgroundColor({ color: 'green' });
courseTabId = details.tabId;
storage.set('courseTabId', courseTabId);
storage.set('courseTabUrl', details.url);
} else {
}
if (onCurrentSchedule) {
//Add GCal buttons
realBrowser.scripting.executeScript({
target: {
tabId: details.tabId,
},
// content script injection only works reliably on the prod packaged extension
// b/c of the plasmo dev server connections
func: addGCalButtons,
});
}
if (!onOptions && !onCurrentSchedule) {
realBrowser.action.setBadgeText({ text: '' });
}
});
Expand All @@ -90,13 +105,6 @@ realBrowser.runtime.onMessage.addListener(function (message) {
}
});

realBrowser.runtime.onMessage.addListener(function (message) {
if (message.name === 'insertEventToGoogleCalendar') {
console.log(message.event, message.token);
insertEventToGoogleCalendar(message.event);
}
});

/** Sets the icon to be active if we're on a course tab */
realBrowser.tabs.onActivated.addListener(async () => {
const cachedTabUrl: string = await storage.get('courseTabUrl');
Expand Down Expand Up @@ -143,44 +151,3 @@ async function getCurrentTab() {
const [tab] = await realBrowser.tabs.query(queryOptions);
return tab;
}

export async function insertEventToGoogleCalendar(event) {
console.log('added', event.pid, event.toString());

try {
chrome.identity.getAuthToken(
{
interactive: false,
},
(token) => {
if (!token) {
chrome.identity.clearAllCachedAuthTokens();
chrome.storage.local.set({}, function () {});
}
chrome.storage.local.set({ token: token }, function () {});
const headers = new Headers({
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json',
});

const body = JSON.stringify(event);

fetch(
'https://www.googleapis.com/calendar/v3/calendars/primary/events',
{
method: 'POST',
headers: headers,
body: body,
},
)
.then((response) => response.json())
.then((data) => console.log('Event added:', data))
.catch((error) => {
console.error('Error adding event:', error);
});
},
);
} catch (error) {
console.error(error);
}
}
Loading

0 comments on commit 6bd6bd4

Please sign in to comment.