Sort repo search results.

Prioritizes shared project repos and the current user's repos before other users'
This commit is contained in:
Sage Vaillancourt 2023-02-02 15:57:09 -05:00
parent 92fc3dd021
commit bee9f4e2b2
5 changed files with 45 additions and 7 deletions

View File

@ -56,7 +56,7 @@ const getClass = (text, keywords) => {
return 'hl-variable'
}
const getKeywords = () => keywordMap[getFileName().replace(/.*\./g, '')] || keywordMap.none
const getKeywords = () => keywordMap[getFileName()?.replace(/.*\./g, '')] || keywordMap.none
const commentSpan = innerText => {
const span = document.createElement('span')

View File

@ -1,5 +1,5 @@
const fixCSharpStrings = () => {
const hasCSharpFileName = getFileName().endsWith('.cs')
const hasCSharpFileName = getFileName()?.endsWith('.cs')
if (!hasCSharpFileName) {
return
}

View File

@ -1,7 +1,7 @@
{
"name": "ADD Git NoFork",
"description": "Ensure searches don't include forks",
"version": "1.0.2",
"name": "Sage's BitBucket Addon",
"description": "Ensure searches don't include forks (and other enhancements)",
"version": "1.0.3",
"manifest_version": 2,
"browser_specific_settings": {
"gecko": {
@ -25,8 +25,9 @@
"matches": ["*://git.add123.com/*"],
"js": [
"utils.js",
"csharp-quotes.js",
"backtick-comments.js",
"csharp-quotes.js",
"repo-search-sorter.js",
"var-highlighter.js"
],
"run_at": "document_start"

33
repo-search-sorter.js Normal file
View File

@ -0,0 +1,33 @@
const compareResults = currentUser => (a, b) => {
return ['/projects/', currentUser]
.some(href => a.children[0].href.includes(href)) ? -1 : 1
}
const sortChildren = ol => {
const children = Array.from(ol.children)
children.forEach(child => ol.removeChild(child))
children
.sort(compareResults(getCurrentUser()))
.forEach(child => ol.appendChild(child))
}
/// Re-order the search results to list projects and the current user first
let claim = 0
const sortSearchResult = () => {
const searchBox = Array.from(document.getElementsByTagName('input'))
.filter(input => input.name === 'repository-search')[0]
searchBox.onkeyup = e => {
claim++
const myClaim = claim
setTimeout(() => {
if (claim != myClaim) {
return
}
const ol = getClassNameElementsArray('search-results')[0]?.firstChild
sortChildren(ol)
claim = 0
}, 1000)
}
}
addFix(sortSearchResult)

View File

@ -2,7 +2,7 @@ const NEW_PAGE_DELAY = 1000
const CURRENT_PAGE_DELAY = 300
const getClassNameElementsArray = className =>
Object.values(document.getElementsByClassName(className))
Array.from(document.getElementsByClassName(className))
// Runs on "complete" load and when https://urlurl.url#this-hash-value changes
const addFix = fixFunc => {
@ -11,3 +11,7 @@ const addFix = fixFunc => {
}
const getFileName = () => getClassNameElementsArray('file-breadcrumbs-segment-highlighted').map(e => e.innerText)[0]
const getCurrentUser = () => {
return getClassNameElementsArray("user-dropdown-trigger")[0].title.replace(/(.*\()|\)/g, '')
}