Repository navigation
Expand file tree
/
Copy pathsort.user.js
More file actions
141 lines (121 loc) · 5.01 KB
/
Copy pathsort.user.js
File metadata and controls
141 lines (121 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// ==UserScript==
// @name Ampido Sorting
// @namespace shiftgeist
// @icon https://www.google.com/s2/favicons?sz=64&domain=ampido.com
// @version 0.0.1
// @timestamp 20261030.1200
//
// @match https://www.ampido.com/searches*
// @grant none
// @run-at document-idle
//
// @author shiftgeist
// @description Adds sorting (distance, rating, reviews, price) to Ampido parking search results
// @license GNU GPLv3
//
// @homepage https://github.com/shiftgeist/userscripts
// @updateURL https://raw.githubusercontent.com/shiftgeist/userscripts/refs/heads/main/ampido.com/sort.user.js
// @downloadURL https://raw.githubusercontent.com/shiftgeist/userscripts/refs/heads/main/ampido.com/sort.user.js
// @supportURL https://github.com/shiftgeist/userscripts/issues
// ==/UserScript==
;(() => {
'use strict'
const id = '[Ampido Sorting]'
const debug = window.localStorage.getItem('debug') === 'true'
const $ = (selector, parent = document) => parent.querySelector(selector)
const $$ = (selector, parent = document) => [...parent.querySelectorAll(selector)]
const log = (...params) => debug && console.debug(id, ...params)
const error = (...params) => debug && console.error(id, ...params)
const storageKey = 'ampido-sorting'
const missingAscending = 99999
const missingDescending = -1
const sortOptions = [
{ value: 'original', label: 'Standard' },
{ value: 'distance-ascending', label: 'Entfernung (nah zuerst)' },
{ value: 'rating-descending', label: 'Bewertung (beste zuerst)' },
{ value: 'reviews-descending', label: 'Anzahl Bewertungen' },
{ value: 'price-ascending', label: 'Preis (günstig zuerst)' }
]
const comparators = {
original: (a, b) => a.originalIndex - b.originalIndex,
'distance-ascending': (a, b) => a.distanceMinutes - b.distanceMinutes,
'rating-descending': (a, b) => b.rating - a.rating,
'reviews-descending': (a, b) => b.reviewCount - a.reviewCount,
'price-ascending': (a, b) => a.price - b.price
}
function createElement(tagName, properties = {}, children = []) {
const element = document.createElement(tagName)
Object.assign(element, properties)
element.append(...children)
return element
}
function waitForElement(selector, callback) {
const existing = $(selector)
if (existing) return callback(existing)
const observer = new MutationObserver(() => {
const element = $(selector)
if (!element) return
observer.disconnect()
callback(element)
})
observer.observe(document.documentElement, { childList: true, subtree: true })
}
const firstNumber = (text) => {
const match = text?.match(/\d+(?:[.,]\d+)?/)
return match ? parseFloat(match[0].replace(',', '.')) : null
}
function readResult(listItem) {
const distanceLabel = $('[aria-label^="Entfernung"]', listItem)?.getAttribute('aria-label')
const ratingLabel = $('[aria-label^="Rating"], [aria-label^="Bewertung"]', listItem)
?.getAttribute('aria-label')
const reviewCountText = $('.text-gray-600.text-sm', listItem)?.textContent
const priceMatch = listItem.textContent.match(/(\d+(?:[.,]\d+)?)\s*€/)
return {
listItem,
originalIndex: Number(listItem.dataset.originalIndex),
distanceMinutes: firstNumber(distanceLabel) ?? missingAscending,
rating: firstNumber(ratingLabel) ?? missingDescending,
reviewCount: firstNumber(reviewCountText) ?? missingDescending,
price: priceMatch ? firstNumber(priceMatch[1]) : missingAscending
}
}
function main(resultsList) {
const section = resultsList.closest('section')
let nextOriginalIndex = 0
const select = createElement(
'select',
{ className: 'p-2 m-2 bg-white border border-gray-400 rounded' },
sortOptions.map(({ value, label }) => createElement('option', { value, textContent: label }))
)
select.value = window.localStorage.getItem(storageKey) ?? 'original'
section.before(select)
const observer = new MutationObserver(applySorting)
function applySorting() {
observer.disconnect()
try {
// special_results items live in a nested turbo-frame, flatten them into one list
const listItems = $$('li', section).filter((listItem) => $('h3', listItem))
for (const listItem of listItems) {
if (listItem.dataset.originalIndex) continue
listItem.dataset.originalIndex = nextOriginalIndex++
}
const sortedItems = listItems.map(readResult).sort(comparators[select.value])
resultsList.append(...sortedItems.map((result) => result.listItem))
log('Sorted by', select.value, sortedItems.length)
} catch (e) {
error(e)
}
observer.observe(resultsList, { childList: true, subtree: true })
}
select.addEventListener('change', () => {
window.localStorage.setItem(storageKey, select.value)
applySorting()
})
applySorting()
}
try {
waitForElement('section ul > li', (firstItem) => main(firstItem.parentElement))
} catch (e) {
error(e)
}
})()