improve b96ba7c0, getUserConfig() in realtime when mounting component

This commit is contained in:
josc146
2024-08-04 15:15:48 +08:00
parent 226fb108a1
commit f50249e74f
5 changed files with 18 additions and 14 deletions
+13 -9
View File
@@ -31,9 +31,10 @@ import NotificationForChatGPTWeb from '../components/NotificationForChatGPTWeb'
/** /**
* @param {SiteConfig} siteConfig * @param {SiteConfig} siteConfig
* @param {UserConfig} userConfig
*/ */
async function mountComponent(siteConfig, userConfig) { async function mountComponent(siteConfig) {
const userConfig = await getUserConfig()
if (!userConfig.alwaysFloatingSidebar) { if (!userConfig.alwaysFloatingSidebar) {
const retry = 10 const retry = 10
let oldUrl = location.href let oldUrl = location.href
@@ -74,17 +75,19 @@ async function mountComponent(siteConfig, userConfig) {
e.remove() 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) { 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 let triggered = false
if (userConfig.triggerMode === 'always') triggered = true if (userConfig.triggerMode === 'always') triggered = true
else if (userConfig.triggerMode === 'questionMark' && endsWithQuestionMark(question.trim())) else if (userConfig.triggerMode === 'questionMark' && endsWithQuestionMark(question.trim()))
triggered = true triggered = true
render( render(
<FloatingToolbar <FloatingToolbar
session={initSession({ modelName: userConfig.modelName })} session={initSession({ modelName: userConfig.modelName })}
@@ -98,6 +101,7 @@ async function mountComponent(siteConfig, userConfig) {
) )
return return
} }
const container = document.createElement('div') const container = document.createElement('div')
container.id = 'chatgptbox-container' container.id = 'chatgptbox-container'
render( render(
@@ -313,7 +317,7 @@ async function prepareForStaticCard() {
} }
} }
if (initSuccess) mountComponent(siteConfig[siteName], userConfig) if (initSuccess) mountComponent(siteConfig[siteName])
} }
} }
@@ -14,7 +14,7 @@ export default {
) { ) {
const searchValue = await getInput(config.baidu.inputQuery) const searchValue = await getInput(config.baidu.inputQuery)
if (searchValue) { if (searchValue) {
mountComponent(config.baidu, userConfig) mountComponent(config.baidu)
} }
} }
}) })
@@ -14,7 +14,7 @@ export default {
const newPath = getVideoPath() const newPath = getVideoPath()
if (newPath !== oldPath) { if (newPath !== oldPath) {
oldPath = newPath oldPath = newPath
mountComponent(config.bilibili, userConfig) mountComponent(config.bilibili)
} }
} }
window.setInterval(checkPathChange, 500) window.setInterval(checkPathChange, 500)
@@ -124,13 +124,13 @@ export default {
if (location.href !== oldUrl) { if (location.href !== oldUrl) {
oldUrl = location.href oldUrl = location.href
if (isPull() || isIssue()) { if (isPull() || isIssue()) {
mountComponent(config.github, userConfig) mountComponent(config.github)
return return
} }
const patchUrl = await getPatchUrl() const patchUrl = await getPatchUrl()
if (patchUrl) { if (patchUrl) {
mountComponent(config.github, userConfig) mountComponent(config.github)
} }
} }
} }
@@ -14,7 +14,7 @@ export default {
const checkUrlChange = async () => { const checkUrlChange = async () => {
if (location.href !== oldUrl) { if (location.href !== oldUrl) {
oldUrl = location.href oldUrl = location.href
mountComponent(config.youtube, userConfig) mountComponent(config.youtube)
} }
} }
window.setInterval(checkUrlChange, 500) window.setInterval(checkUrlChange, 500)