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

Selma: Content script sometimes gets injected after DOMContentLoaded event on OperaGX #158

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 20 additions & 13 deletions src/contentScripts/other/selma/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ const currentView = document.location.pathname
// This is used to get the URL which would be opened in a popup
const popupScriptsRegex = /dl_popUp\("\/scripts\/mgrqispi\.dll\?APPNAME=CampusNet&PRGNAME=(\w+)&ARGUMENTS=([^"]+)"/

// A promise that resolves to the setting value of `improveSelma`
const improveSelmaEnabledPromise: Promise<boolean> = chrome.storage.local
.get(['improveSelma'])
.then((s) => s.improveSelma)

function scriptToURL(script: string): string {
const matches = script.match(popupScriptsRegex)!

Expand Down Expand Up @@ -195,12 +200,13 @@ Actual logic
// Create a small banner that indicates the user that the site was modified
// It also adds a small toggle to disable the table
async function createCreditsBanner() {
const { improveSelma: settingEnabled } = await chrome.storage.local.get(['improveSelma'])
const settingEnabled = await improveSelmaEnabledPromise

const imgUrl = chrome.runtime.getURL('/assets/images/tufast48.png')
const credits = document.createElement('p')

credits.style.margin = 'auto'
credits.style.marginLeft = '10px'
credits.style.marginRight = '0'
credits.style.color = '#002557' // Selma theme color
credits.id = 'TUfastCredits'
Expand Down Expand Up @@ -237,24 +243,25 @@ async function createCreditsBanner() {
return credits
}

// Apply all custom changes once documentd loaded
;(async () => {
const { improveSelma } = await chrome.storage.local.get(['improveSelma'])

// Apply all custom changes
document.addEventListener('DOMContentLoaded', async () => {
// Add Credit banner with toggle button
const creditElm = await createCreditsBanner()
document.querySelector('.semesterChoice')!.appendChild(creditElm)

if (!improveSelma) return

eventListener()
})
if (document.readyState !== 'loading') {
await eventListener()
} else {
document.addEventListener('DOMContentLoaded', eventListener)
}
})()

async function eventListener() {
document.removeEventListener('DOMContentLoaded', eventListener)

// Add Credit banner with toggle button
const creditElm = await createCreditsBanner()
document.querySelector('.semesterChoice')!.appendChild(creditElm)

const improveSelma = await improveSelmaEnabledPromise
if (!improveSelma) return

// Inject css
injectCSS('base')
if (currentView.startsWith('/APP/EXAMRESULTS/') || currentView.startsWith('/APP/COURSERESULTS/')) {
Expand Down
Loading