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

feat(Khan Academy): fix and update presence #9374

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion websites/K/Khan Academy/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"url": "www.khanacademy.org",
"regExp": "([a-z]+[.])+khanacademy[.]org[/]",
"version": "1.1.22",
"version": "1.2.0",
"logo": "https://cdn.rcd.gg/PreMiD/websites/K/Khan%20Academy/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/K/Khan%20Academy/assets/thumbnail.png",
"color": "#4de682",
Expand Down
102 changes: 48 additions & 54 deletions websites/K/Khan Academy/presence.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
const presence = new Presence({
clientId: '900882829154598952',
})
const presence = new Presence({ clientId: '900882829154598952' })
const strings = presence.getStrings({
homepage: 'general.viewHome',
settings: 'google classroom.settings',
watching: 'general.watching',
reading: 'general.readingAbout',
writing: 'general.writing',
profile: 'general.viewProfile',
homepage: 'Homepage',
settings: 'Settings',
})

enum ActivityAssets {
Expand All @@ -20,77 +14,77 @@
presence.on('UpdateData', async () => {
const presenceData: PresenceData = {
largeImageKey: ActivityAssets.Logo,
details: (await strings).watching,
details: 'Viewing',
}
const path = document.location.pathname
const searchParams = new URLSearchParams(document.location.search)

if (document.location.pathname === '/') {
if (path === '/') {
presenceData.state = `🏠 ${(await strings).homepage}`
}
else if (document.location.pathname.includes('/courses')) {
else if (path.includes('/courses')) {
presenceData.state = '📚 Courses'
}
else if (document.location.pathname.includes('/progress')) {
else if (path.includes('/progress')) {
presenceData.state = '📊 Progress'
}
else if (document.location.pathname.includes('/teachers')) {
else if (path === '/profile/me/teachers') {
presenceData.state = '🎓 Teachers'
}
else if (document.location.pathname.includes('/profile')) {
presenceData.details = (await strings).profile
presenceData.state = `👀 ${
document.querySelector('._o77ufew')?.textContent
}`
else if (path.includes('/profile')) {
presenceData.state = '👤 Profile'
}
else if (document.location.pathname.includes('/settings')) {
else if (path.includes('/settings')) {
presenceData.state = `⚙️ ${(await strings).settings}`
}
else if (document.location.pathname.includes('/search')) {
presenceData.state = `🔍 Searching for '${
document.location.pathname.split('page_search_query=')[1]
}'`
else if (path.includes('/search')) {
presenceData.details = '🔍 Searching'
presenceData.state = `'${searchParams.get('page_search_query') || 'Unknown Search'}'`
}
else if (document.location.pathname.includes('/topics')) {
else if (path.includes('/topics')) {
presenceData.state = '🔍 Community'
}
else if (document.location.pathname.includes('/posts')) {
else if (path.includes('/posts')) {
presenceData.state = '🔍 Community Post'
}
else if (document.location.pathname.includes('/requests/new')) {
else if (path.includes('/requests/new')) {
presenceData.state = '⚠️ Submitting a Request'
}
else if (document.location.hostname.includes('support')) {
presenceData.state = '💡 Support'
}
else if (document.location.pathname.split('/').length < 3) {
presenceData.state = `📖 ${
document.querySelector('._aemo2b3')?.textContent
}`
else if (path.split('/').length < 3) {
presenceData.state = `📖 ${document.querySelector('._aemo2b3')?.textContent}`
}
else {
presenceData.details = document.querySelector(
'._io410w6, span._cmfzobe:nth-child(2) > a:nth-child(2)',
)?.textContent
presenceData.state = `📋 ${document
.querySelector(
'._1eqoe4n8, span._cmfzobe:nth-child(3) > a:nth-child(2), #uid-dialog-0-title > span:nth-child(1)',
)
?.textContent
?.replace(/.*?:\s+/, '')}`
else if (path.match(/\/([vae]|quiz)\//)) {
const breadcrumbLinks = document.querySelectorAll('a[class="_j9iwqrr"]')
const courseName = breadcrumbLinks[1]?.textContent || ' '
const contentTitle =
document.querySelector('h1[data-testid="content-library-content-title"]')
?.textContent || ' '

if (document.location.pathname.match(/\/([vae]|quiz)\//)) {
presenceData.smallImageText = document.querySelector(
'._1l44zfj, [role="dialog"] [data-test-id="modal-title"]',
)?.textContent

if (document.location.pathname.includes('/v/'))
presenceData.smallImageKey = ActivityAssets.Video
else if (document.location.pathname.includes('/a/'))
presenceData.smallImageKey = ActivityAssets.Article
else presenceData.smallImageKey = ActivityAssets.Exercise
if (path.includes('/v/')) {
presenceData.smallImageKey = ActivityAssets.Video
presenceData.smallImageText = 'Watching a Video'
presenceData.details = courseName
presenceData.state = `📺 ${contentTitle}`
}
else if (path.includes('/a/')) {
presenceData.smallImageKey = ActivityAssets.Article
presenceData.smallImageText = 'Reading an Article'
presenceData.details = courseName
presenceData.state = `📖 ${contentTitle}`
}
else {
presenceData.smallImageKey = ActivityAssets.Exercise
presenceData.smallImageText = 'Taking a Quiz'
presenceData.details = courseName
presenceData.state = `📝 ${contentTitle}`
}
}
else {
presenceData.state = `📋 ${document.querySelector('.nav-breadcrumb a:last-child')?.textContent || 'Learning'}`
}

if (!presenceData.details)
presence.setActivity()
else presence.setActivity(presenceData)
presence.setActivity(presenceData.details ? presenceData : {})
})
Loading