Skip to content

Commit

Permalink
fix mounting failure caused by DuckDuckGo's initial rendering delay. #…
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Jan 26, 2024
1 parent 4510cf4 commit 5929a18
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/content-script/site-adapters/duckduckgo/index.mjs
Original file line number Diff line number Diff line change
@@ -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
},
}
4 changes: 4 additions & 0 deletions src/content-script/site-adapters/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']"],
Expand Down
8 changes: 7 additions & 1 deletion src/utils/wait-for-element-to-exist-and-select.mjs
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -15,5 +15,11 @@ export function waitForElementToExistAndSelect(selector) {
subtree: true,
childList: true,
})

if (timeout)
setTimeout(() => {
observer.disconnect()
resolve(null)
}, timeout)
})
}

0 comments on commit 5929a18

Please sign in to comment.