generated from cloud-gov/pages-uswds-11ty
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#313: Trigger the rdap lookup on load #365
Open
rachidatecs
wants to merge
3
commits into
main
Choose a base branch
from
313-whois-onload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+69
−29
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
{ | ||
"admin.js": "/assets/js/admin-77FHK54G.js", | ||
"admin.map": "/assets/js/admin-77FHK54G.js.map", | ||
"app.js": "/assets/js/app-XWR645AF.js", | ||
"app.map": "/assets/js/app-XWR645AF.js.map", | ||
"styles.css": "/assets/styles/styles-A2RYK5HG.css", | ||
"styles.map": "/assets/styles/styles-A2RYK5HG.css.map" | ||
} | ||
"app.js": "/assets/js/app-T4AK7MTZ.js", | ||
"app.map": "/assets/js/app-T4AK7MTZ.js.map", | ||
"uswds.js": "/assets/js/uswds-init.js", | ||
"styles.css": "/assets/styles/styles-M7MBCPQG.css", | ||
"styles.map": "/assets/styles/styles-M7MBCPQG.css.map" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,61 @@ | ||
<script> | ||
document.addEventListener("DOMContentLoaded", function () { | ||
// Autofill domain from URL if present | ||
let domain = getQueryParam("domain"); | ||
if (domain) { | ||
const inputField = document.getElementById("whois-lookup-input"); | ||
const submitButton = document.querySelector(".usa-search--domain__submit"); | ||
|
||
if (inputField && submitButton) { | ||
const validation = validateDomain(domain, true); | ||
|
||
if (validation.isValid) { | ||
inputField.value = validation.domain; | ||
submitButton.click(); | ||
} else { | ||
console.warn("Invalid domain:", validation.errorMessage); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// Function to get query parameters from URL | ||
function getQueryParam(param) { | ||
const urlParams = new URLSearchParams(window.location.search); | ||
return urlParams.get(param); | ||
} | ||
|
||
// Function to validate domain name | ||
function validateDomain(domain, mustIncludeTopLevelDomain=false) { | ||
if (!domain) return { isValid: false, errorMessage: "Domain is required" }; | ||
|
||
// A domain name is alphanumeric or hyphen, up to 63 characters, doesn't | ||
// begin or end with a hyphen, followed by a TLD of 2-6 alphabetic characters | ||
const DOMAIN_REGEX = /^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.gov)?$/; | ||
if (!DOMAIN_REGEX.test(domain)) { | ||
return { isValid: false, errorMessage: "Enter a domain using only letters, numbers, or hyphens." }; | ||
} | ||
|
||
// Number of parts to domain separated by . | ||
const num_domain_parts = domain.split('.').length | ||
|
||
// Domain has more than one part above the TLD which RDAP does not support | ||
if (num_domain_parts > 2) { | ||
errorMessageText = "Domain name must have exactly one part above the TLD." | ||
} | ||
else if (num_domain_parts == 2 && !domain.endsWith('.gov')) { | ||
errorMessageText = "You can only search domain names that end in .gov" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch. Fixed, I think. |
||
} | ||
|
||
// On page load, we want this extra check. On form interaction intead, we want to | ||
// give the user the option to enter or omit the TLD | ||
if (mustIncludeTopLevelDomain && !domain.endsWith(".gov")) { | ||
return { isValid: false, errorMessage: "You can only search domain names that end in .gov" }; | ||
} | ||
|
||
return { isValid: true, domain }; | ||
} | ||
|
||
// RDAP data that is excluded in search results | ||
const excludedFields = { | ||
"contact": ["version"], | ||
|
@@ -130,44 +187,28 @@ <h4>${contact.charAt(0).toUpperCase() + contact.slice(1)}</h4> | |
|
||
const rdapBaseUrl = 'https://manage.get.gov/api/v1/rdap/?domain='; | ||
|
||
let domain = document.getElementById(searchId).value | ||
let domain = document.getElementById(searchId).value.trim(); | ||
|
||
if (domain === null || domain.length === 0) { | ||
render_result(searchResultsId, ``); | ||
return; | ||
} | ||
|
||
/* Validate domain name is valid. | ||
A domain name is alphanumeric or hyphen, up to 63 characters, doesn't | ||
begin or end with a hyphen, followed by a TLD of 2-6 alphabetic characters */ | ||
DOMAIN_REGEX = /^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.gov)?$/; | ||
|
||
if (!DOMAIN_REGEX.test(domain)) { | ||
let errorMessageText = "Enter a domain using only letters, numbers, or hyphens." | ||
// Number of parts to domain separated by . | ||
const num_domain_parts = domain.split('.').length | ||
|
||
// Domain has more than one part above the TLD which RDAP does not support | ||
if (num_domain_parts > 2) { | ||
errorMessageText = "Domain name must have exactly one part above the TLD." | ||
} | ||
else if (num_domain_parts == 2 && !domain.endsWith('.gov')) { | ||
errorMessageText = "You can only search domain names that end in .gov" | ||
} | ||
|
||
let errorMessage = ` | ||
// Validate domain using the new function | ||
const validation = validateDomain(domain); | ||
if (!validation.isValid) { | ||
render_result(searchResultsId, ` | ||
<div class="usa-alert usa-alert--error usa-alert--slim" role="alert"> | ||
<div class="usa-alert__body display-flex flex-column flex-align-center"> | ||
<p class="usa-alert__text"> | ||
${errorMessageText} | ||
</p> | ||
<p class="usa-alert__text">${validation.errorMessage}</p> | ||
</div> | ||
</div> | ||
` | ||
render_result(searchResultsId, errorMessage); | ||
`); | ||
return; | ||
} | ||
|
||
domain = validation.domain; | ||
|
||
if (!domain.endsWith('.gov')) { | ||
domain = `${domain}.gov`; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be returned as an error message like below? If not, what is this being used for?