From bee9f4e2b209cba6d423bca4a84d418bd5386961 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 2 Feb 2023 15:57:09 -0500 Subject: [PATCH] Sort repo search results. Prioritizes shared project repos and the current user's repos before other users' --- backtick-comments.js | 2 +- csharp-quotes.js | 2 +- manifest.json | 9 +++++---- repo-search-sorter.js | 33 +++++++++++++++++++++++++++++++++ utils.js | 6 +++++- 5 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 repo-search-sorter.js diff --git a/backtick-comments.js b/backtick-comments.js index 2c66728..cfc66d6 100644 --- a/backtick-comments.js +++ b/backtick-comments.js @@ -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') diff --git a/csharp-quotes.js b/csharp-quotes.js index 506dbe2..17c296d 100644 --- a/csharp-quotes.js +++ b/csharp-quotes.js @@ -1,5 +1,5 @@ const fixCSharpStrings = () => { - const hasCSharpFileName = getFileName().endsWith('.cs') + const hasCSharpFileName = getFileName()?.endsWith('.cs') if (!hasCSharpFileName) { return } diff --git a/manifest.json b/manifest.json index a4a37f4..a2235b4 100644 --- a/manifest.json +++ b/manifest.json @@ -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" diff --git a/repo-search-sorter.js b/repo-search-sorter.js new file mode 100644 index 0000000..dfda2d4 --- /dev/null +++ b/repo-search-sorter.js @@ -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) diff --git a/utils.js b/utils.js index 37315db..5c1f43f 100644 --- a/utils.js +++ b/utils.js @@ -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, '') +}