Skip to content

Commit fb1afe7

Browse files
committed
fix: fixed error for [Reddit Search Options Persist]
1 parent 3306654 commit fb1afe7

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

src/plugins/reddit-search-options-persist/userscript.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,57 @@ observeElement({
3131
root: document.body,
3232
})
3333

34+
let controller = new AbortController()
35+
observeElement({
36+
selector: 'a[type="button"][href*="/search/"]',
37+
onElement: debounce(() => {
38+
controller.abort()
39+
controller = new AbortController()
40+
document.querySelectorAll('a[type="button"][href*="/search/"]').forEach((anchor) => {
41+
const searchAnchor = anchor as HTMLAnchorElement
42+
searchAnchor.addEventListener(
43+
'click',
44+
(ev) => {
45+
ev.preventDefault()
46+
ev.stopPropagation()
47+
const url = new URL(searchAnchor.href)
48+
// merge existing search params from current location
49+
new URLSearchParams(location.search).forEach((value, key) => {
50+
if (!url.searchParams.has(key)) {
51+
url.searchParams.set(key, value)
52+
}
53+
})
54+
url.searchParams.delete('iId')
55+
location.href = url.toString()
56+
},
57+
{
58+
signal: controller.signal,
59+
},
60+
)
61+
})
62+
}, 100),
63+
supportShadowDOM: true,
64+
root: document.body,
65+
})
66+
3467
function performSearch(query: string) {
3568
let baseURL = '/search'
3669
if (querySelectorDeep('faceplate-search-input #search-input-chip')) {
3770
const subredditMatch = location.pathname.match(/^\/r\/([^\/]+)/)
3871
if (subredditMatch) {
3972
baseURL = `/r/${subredditMatch[1]}/search`
4073
}
41-
const userMatch = location.pathname.match(/^\/user\/([^\/]+)/)
42-
if (userMatch) {
43-
baseURL = `/user/${userMatch[1]}/search`
74+
}
75+
const filter = querySelectorDeep('#search-input-remove-filter')
76+
if (filter && filter.innerText.trim().startsWith('u/')) {
77+
const username = filter.innerText.trim().slice(2)
78+
if (username) {
79+
baseURL = `/user/${username}/search`
4480
}
4581
}
82+
4683
const params = new URLSearchParams(location.search)
47-
const paramsToKeep = ['sort', 't', 'type']
84+
const paramsToKeep = ['type', 'sort', 't']
4885
const keysToDelete = Array.from(params.keys()).filter((key) => !paramsToKeep.includes(key))
4986
keysToDelete.forEach((key) => params.delete(key))
5087
params.set('q', query)

0 commit comments

Comments
 (0)