Skip to content

Commit

Permalink
feat: 서울시립대학교에서 이용가능하도록 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kangju2000 committed Sep 29, 2024
1 parent 922d221 commit 86562bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
15 changes: 8 additions & 7 deletions manifest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineManifest } from '@crxjs/vite-plugin'

import packageJson from './package.json'
import { UNIVERITY_LINK_LIST } from './src/constants/univ'

const [major, minor, patch, label = '0'] = packageJson.version.replace(/[^\d.-]+/g, '').split(/[.-]/)

Expand Down Expand Up @@ -34,12 +35,12 @@ export default defineManifest(async () => ({
},
content_scripts: [
{
matches: ['https://cyber.gachon.ac.kr/*'],
exclude_matches: [
'https://cyber.gachon.ac.kr/login.php*',
'https://cyber.gachon.ac.kr/mod/ubfile/viewer.php*',
'https://cyber.gachon.ac.kr/mod/vod/viewer.php*',
],
matches: UNIVERITY_LINK_LIST.map(univ => `${univ}/*`),
exclude_matches: UNIVERITY_LINK_LIST.map(univ => [
`${univ}/login.php*`,
`${univ}/mod/ubfile/viewer.php*`,
`${univ}/mod/vod/viewer.php*`,
]).flat(),
js: isDev ? ['src/content/index.dev.tsx'] : ['src/content/index.prod.tsx'],
run_at: 'document_start',
},
Expand All @@ -51,6 +52,6 @@ export default defineManifest(async () => ({
matches: ['*://*/*'],
},
],
host_permissions: ['https://cyber.gachon.ac.kr/*'],
host_permissions: UNIVERITY_LINK_LIST.map(univ => `${univ}/*`),
permissions: ['storage', 'unlimitedStorage', 'scripting', 'activeTab'],
}))
7 changes: 7 additions & 0 deletions src/constants/univ.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type University = '가천대학교' | '서울시립대학교'
export const UNIVERITY_LINK_LIST = ['https://cyber.gachon.ac.kr', 'https://uclass.uos.ac.kr']

export const UNIVERITY_NAME_MAP: Record<(typeof UNIVERITY_LINK_LIST)[number], University> = {
'https://cyber.gachon.ac.kr': '가천대학교',
'https://uclass.uos.ac.kr': '서울시립대학교',
}
25 changes: 19 additions & 6 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import * as cheerio from 'cheerio'

import { UNIVERITY_NAME_MAP } from '@/constants/univ'
import type { Activity, Assignment, Course, Video } from '@/types'
import { getLinkId, mapElement, getAttr, getText } from '@/utils'

import type { AnyNode } from 'domhandler'

type CheerioAPI = cheerio.CheerioAPI

const origin = document.location.origin
const university = UNIVERITY_NAME_MAP[origin]

const univSpecific = {
가천대학교: {
titleRegex: /\((\w{5}_\w{3})\)/,
},
서울시립대학교: {
titleRegex: /\[(?:\w{5}_\w{2}|\w{2})]/,
},
}[university]

// 네트워크 요청을 담당하는 함수
export async function fetchHtml(url: string): Promise<string> {
const response = await fetch(url)
const response = await fetch(document.location.origin + url)
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
return await response.text()
}
Expand Down Expand Up @@ -38,14 +51,14 @@ function parseCourses($: CheerioAPI): Course[] {
return mapElement($('.coursefullname'), (_, el) => {
const $el = $(el)
const id = getLinkId(getAttr($el, 'href'))
const title = getText($el).replace(/ \((\w{5}_\w{3})\)/, '')
const title = getText($el).replace(univSpecific.titleRegex, '')
return { id, title }
})
}

// 강의 목록 가져오기
export const getCourses = async (): Promise<Course[]> => {
const $ = await getDocument('https://cyber.gachon.ac.kr/local/ubion/user')
const $ = await getDocument('/local/ubion/user')
return parseCourses($)
}

Expand Down Expand Up @@ -105,7 +118,7 @@ export const getActivities = async (
assignmentSubmittedArray: Awaited<ReturnType<typeof getAssignmentSubmitted>>,
videoSubmittedArray: Awaited<ReturnType<typeof getVideoSubmitted>>,
): Promise<Activity[]> => {
const $ = await getDocument(`https://cyber.gachon.ac.kr/course/view.php?id=${courseId}`)
const $ = await getDocument(`/course/view.php?id=${courseId}`)

const assignments = parseAssignments($, courseId).reduce<Assignment[]>((acc, cur) => {
const findAssignment = assignmentSubmittedArray.find(a => a.id === cur.id)
Expand Down Expand Up @@ -141,7 +154,7 @@ export function parseAssignmentSubmitted(
export const getAssignmentSubmitted = async (
courseId: string,
): Promise<Array<Pick<Assignment, 'id' | 'title' | 'hasSubmitted' | 'endAt'>>> => {
const $ = await getDocument(`https://cyber.gachon.ac.kr/mod/assign/index.php?id=${courseId}`)
const $ = await getDocument(`/mod/assign/index.php?id=${courseId}`)
return parseAssignmentSubmitted($)
}

Expand Down Expand Up @@ -173,6 +186,6 @@ function parseVideoSubmitted($: CheerioAPI): Array<Pick<Video, 'title' | 'hasSub
export const getVideoSubmitted = async (
courseId: string,
): Promise<Array<Pick<Video, 'title' | 'hasSubmitted' | 'sectionTitle'>>> => {
const $ = await getDocument(`https://cyber.gachon.ac.kr/report/ubcompletion/user_progress.php?id=${courseId}`)
const $ = await getDocument(`/report/ubcompletion/user_progress.php?id=${courseId}`)
return parseVideoSubmitted($)
}

0 comments on commit 86562bb

Please sign in to comment.