diff --git a/website/src/queries-init.js b/website/src/queries-init.js index 5b977a47..7c104e47 100644 --- a/website/src/queries-init.js +++ b/website/src/queries-init.js @@ -169,6 +169,10 @@ function getQueryOrDefault(defaultVal) { return JQ_REPO_FIELD.val(); } +function setQuery(query) { + JQ_REPO_FIELD.val(query); +} + function hideFilterContainer() { JQ_FILTER_CONTAINER.hide(); } diff --git a/website/src/queries-logic.js b/website/src/queries-logic.js index c5599a96..733ff2ca 100644 --- a/website/src/queries-logic.js +++ b/website/src/queries-logic.js @@ -462,8 +462,32 @@ function initial_request(user, repo) { } /** Extracts and sanitizes 'user' and 'repo' values from potential inputs. */ -function initiate_search() { +function parse_query(queryString) { + const shorthand = /^(?[\w.-]+)\/(?[\w.-]+)$/; + const shorthandMatch = shorthand.exec(queryString); + + if (shorthandMatch) { // we are dealing with "user/repo" input format + const {user, repo} = shorthandMatch.groups; + return {user, repo}; + } + + let pathname; + try { + pathname = new URL(queryString).pathname; + } catch { + return null; + } + + const values = pathname.split('/').filter(s => s.length > 0); + if (values.length < 2) + return null; + const [user, repo] = values; + return {user, repo}; +} + + +function initiate_search() { /* Checking if search is allowed. */ if (searchNotAllowed()) return; // abort @@ -471,23 +495,23 @@ function initiate_search() { clear_old_data(); let queryString = getQueryOrDefault("payne911/PieMenu"); - let queryValues = queryString.split('/').filter(Boolean); + const queryValues = parse_query(queryString); - let len = queryValues.length; - if (len < 2) { - setMsg('Please enter a valid query: it should contain two strings separated by a "/"'); + if (!queryValues) { + setMsg('Please enter a valid query: it should contain two strings separated by a "/", or the full URL to a GitHub repo'); ga_faultyQuery(queryString); return; // abort } + const {user, repo} = queryValues; + setUpOctokitWithLatestToken(); + setQuery(`${user}/${repo}`); setQueryFieldsAsLoading(); hideFilterContainer(); setMsg(UF_MSG_SCANNING); - const user = queryValues[len - 2]; - const repo = queryValues[len - 1]; if (history.replaceState) { history.replaceState({}, document.title, `?repo=${user}/${repo}`); // replace current URL param }