Skip to content
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

feat: get domain suggestions for url input #653

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions tools/oversight/elements/url-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ export default class URLSelector extends HTMLElement {
}
</style>
<label for="url"><img src="https://www.aem.live/favicon.ico"></label>
<input id="url" type="url">
<input id="url" type="url" list="rum-domain-suggestions">
<datalist id="rum-domain-suggestions"></datalist>
`;
}

connectedCallback() {
this.innerHTML = this.template;
const datalist = this.querySelector('datalist');
const input = this.querySelector('input');
input.value = new URL(window.location.href).searchParams.get('domain');
const img = this.querySelector('img');
img.src = `https://www.google.com/s2/favicons?domain=${input.value.split(':')[0]}&sz=64`;

if (!getPersistentToken()) {
const token = getPersistentToken();
if (!token) {
input.disabled = true;
datalist.remove();

// detect a click with shift key pressed
img.addEventListener('click', (event) => {
Expand All @@ -51,6 +55,26 @@ export default class URLSelector extends HTMLElement {
window.location.href = targetlocation.href;
}
});
} else {
fetch('https://rum.fastly-aem.page/domains?suggested=true', {
headers: {
accept: 'application/json',
authorization: `Bearer ${token}`,
},
}).then(async (resp) => {
if (!resp.ok) {
datalist.remove();
} else {
const { domains } = await resp.json();
domains.forEach((domain) => {
const option = document.createElement('option');
option.value = domain;
datalist.appendChild(option);
});
}
}).catch(() => {
datalist.remove();
});
}

input.addEventListener('focus', () => {
Expand Down
28 changes: 26 additions & 2 deletions tools/rum/elements/url-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,43 @@ export default class URLSelector extends HTMLElement {
}
</style>
<label for="url"><img src="https://www.aem.live/favicon.ico"></label>
<input id="url" type="url">
<input id="url" type="url" list="rum-domain-suggestions">
<datalist id="rum-domain-suggestions"></datalist>
`;
}

connectedCallback() {
this.innerHTML = this.template;
const datalist = this.querySelector('datalist');
const input = this.querySelector('input');
input.value = new URL(window.location.href).searchParams.get('domain');
const img = this.querySelector('img');
img.src = `https://www.google.com/s2/favicons?domain=${input.value.split(':')[0]}&sz=64`;

if (!getPersistentToken()) {
const token = getPersistentToken();
if (!token) {
input.disabled = true;
datalist.remove();
} else {
fetch('https://rum.fastly-aem.page/domains?suggested=true', {
headers: {
accept: 'application/json',
authorization: `Bearer ${token}`,
},
}).then(async (resp) => {
if (!resp.ok) {
datalist.remove();
} else {
const { domains } = await resp.json();
domains.forEach((domain) => {
const option = document.createElement('option');
option.value = domain;
datalist.appendChild(option);
});
}
}).catch(() => {
datalist.remove();
});
}

input.addEventListener('focus', () => {
Expand Down
Loading