Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add functionality for Disabled tab #54

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
"storage",
"scripting",
"webNavigation",
"tabs"
"tabs",
"nativeMessaging"
],
"host_permissions": [
"https://utdallas.collegescheduler.com/terms/*/courses/*",
Expand Down
33 changes: 32 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Storage } from '@plasmohq/storage';

import { CourseHeader, scrapeCourseData } from '~content';
import { CourseHeader, listenForTableChange, scrapeCourseData } from '~content';

export interface ShowCourseTabPayload {
header: CourseHeader;
Expand All @@ -21,6 +21,7 @@ chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
details.url,
)
) {
//Scrape data
chrome.scripting.executeScript(
{
target: {
Expand All @@ -38,6 +39,15 @@ chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
}
},
);
//Listen for table change to rescrape data
chrome.tabs.sendMessage(details.tabId, 'disconnectObserver');
chrome.scripting.executeScript({
target: {
tabId: details.tabId,
},
func: listenForTableChange,
});
//Store tab info
chrome.action.setBadgeText({ text: '!' });
chrome.action.setBadgeBackgroundColor({ color: 'green' });
courseTabId = details.tabId;
Expand All @@ -48,6 +58,27 @@ chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
}
});

/** Rescrape data on table change */
chrome.runtime.onMessage.addListener(function (message) {
if (message === 'tableChange') {
chrome.scripting.executeScript(
{
target: {
tabId: courseTabId,
},
func: scrapeCourseData,
},
async function (resolve) {
if (resolve && resolve[0] && resolve[0].result) {
const result: ShowCourseTabPayload = resolve[0].result;
scrapedCourseData = result;
await storage.set('scrapedCourseData', scrapedCourseData);
}
},
);
}
});

/** Sets the icon to be active if we're on a course tab */
chrome.tabs.onActivated.addListener(async () => {
const cachedTabUrl: string = await storage.get('courseTabUrl');
Expand Down
32 changes: 32 additions & 0 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,41 @@ export async function scrapeCourseData() {
});
const courseRowCells = courseRow.querySelector('tr');
courseRowCells.insertBefore(newTd, courseRowCells.children[7]);
//Increase Disabled Reasons row colspan if necessary
const sectionDisabled = courseRow.querySelector('tr:nth-child(3) > td');
if (sectionDisabled !== null) {
sectionDisabled.colSpan = sectionDisabled.colSpan + 1;
}
// collapse section details
sectionDetailsButton.click();
});
return [...new Set(professors)];
}
}

/** This listens for clicks on the buttons that switch between the enabled and disabled professor tabs and reports back to background.ts */
export function listenForTableChange() {
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (
mutation.type === 'attributes' &&
mutation.attributeName === 'class'
) {
//button corresponding to shown table is given an active class
if (mutation.target.classList.contains('active')) {
chrome.runtime.sendMessage('tableChange');
}
}
}
});
observer.observe(document.body, {
attributes: true,
subtree: true,
});
//remove observer when ordered by backgroud.ts to avoid duplicates
chrome.runtime.onMessage.addListener(function (message) {
if (message === 'disconnectObserver') {
observer.disconnect();
}
});
}
Loading