Skip to content

Commit

Permalink
improve b96ba7c, getUserConfig() in realtime when mounting component
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Aug 4, 2024
1 parent 226fb10 commit f50249e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
22 changes: 13 additions & 9 deletions src/content-script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import NotificationForChatGPTWeb from '../components/NotificationForChatGPTWeb'

/**
* @param {SiteConfig} siteConfig
* @param {UserConfig} userConfig
*/
async function mountComponent(siteConfig, userConfig) {
async function mountComponent(siteConfig) {
const userConfig = await getUserConfig()

if (!userConfig.alwaysFloatingSidebar) {
const retry = 10
let oldUrl = location.href
Expand Down Expand Up @@ -74,17 +75,19 @@ async function mountComponent(siteConfig, userConfig) {
e.remove()
})

const position = {
x: window.innerWidth - 300 - Math.floor((20 / 100) * window.innerWidth),
y: window.innerHeight / 2 - 200,
}
const toolbarContainer = createElementAtPosition(position.x, position.y)
toolbarContainer.className = 'chatgptbox-toolbar-container-not-queryable'
if (userConfig.alwaysFloatingSidebar && question) {
const position = {
x: window.innerWidth - 300 - Math.floor((20 / 100) * window.innerWidth),
y: window.innerHeight / 2 - 200,
}
const toolbarContainer = createElementAtPosition(position.x, position.y)
toolbarContainer.className = 'chatgptbox-toolbar-container-not-queryable'

let triggered = false
if (userConfig.triggerMode === 'always') triggered = true
else if (userConfig.triggerMode === 'questionMark' && endsWithQuestionMark(question.trim()))
triggered = true

render(
<FloatingToolbar
session={initSession({ modelName: userConfig.modelName })}
Expand All @@ -98,6 +101,7 @@ async function mountComponent(siteConfig, userConfig) {
)
return
}

const container = document.createElement('div')
container.id = 'chatgptbox-container'
render(
Expand Down Expand Up @@ -313,7 +317,7 @@ async function prepareForStaticCard() {
}
}

if (initSuccess) mountComponent(siteConfig[siteName], userConfig)
if (initSuccess) mountComponent(siteConfig[siteName])
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/baidu/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
) {
const searchValue = await getInput(config.baidu.inputQuery)
if (searchValue) {
mountComponent(config.baidu, userConfig)
mountComponent(config.baidu)
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/bilibili/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
const newPath = getVideoPath()
if (newPath !== oldPath) {
oldPath = newPath
mountComponent(config.bilibili, userConfig)
mountComponent(config.bilibili)
}
}
window.setInterval(checkPathChange, 500)
Expand Down
4 changes: 2 additions & 2 deletions src/content-script/site-adapters/github/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ export default {
if (location.href !== oldUrl) {
oldUrl = location.href
if (isPull() || isIssue()) {
mountComponent(config.github, userConfig)
mountComponent(config.github)
return
}

const patchUrl = await getPatchUrl()
if (patchUrl) {
mountComponent(config.github, userConfig)
mountComponent(config.github)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/site-adapters/youtube/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
const checkUrlChange = async () => {
if (location.href !== oldUrl) {
oldUrl = location.href
mountComponent(config.youtube, userConfig)
mountComponent(config.youtube)
}
}
window.setInterval(checkUrlChange, 500)
Expand Down

0 comments on commit f50249e

Please sign in to comment.