fix mounting failure caused by DuckDuckGo's initial rendering delay. #610

This commit is contained in:
josc146
2024-01-26 11:27:31 +08:00
parent 4510cf421e
commit 5929a184a4
3 changed files with 22 additions and 1 deletions
@@ -0,0 +1,11 @@
import { waitForElementToExistAndSelect } from '../../../utils/index.mjs'
import { config } from '../index'
export default {
init: async (hostname, userConfig) => {
if (userConfig.insertAtTop) {
return !!(await waitForElementToExistAndSelect(config.duckduckgo.resultsContainerQuery[0], 5))
}
return true
},
}
@@ -10,6 +10,7 @@ import stackoverflow from './stackoverflow'
import juejin from './juejin'
import weixin from './weixin'
import followin from './followin'
import duckduckgo from './duckduckgo'
/**
* @typedef {object} SiteConfigAction
@@ -55,6 +56,9 @@ export const config = {
sidebarContainerQuery: ['.js-react-sidebar', '.react-results--sidebar'],
appendContainerQuery: ['#links_wrapper'],
resultsContainerQuery: ['.react-results--main'],
action: {
init: duckduckgo.init,
},
},
startpage: {
inputQuery: ["input[name='query']"],
@@ -1,4 +1,4 @@
export function waitForElementToExistAndSelect(selector) {
export function waitForElementToExistAndSelect(selector, timeout = 0) {
return new Promise((resolve) => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector))
@@ -15,5 +15,11 @@ export function waitForElementToExistAndSelect(selector) {
subtree: true,
childList: true,
})
if (timeout)
setTimeout(() => {
observer.disconnect()
resolve(null)
}, timeout)
})
}