Refine UI and bug fix challenge solving
This commit is contained in:
@@ -1,52 +1,44 @@
|
||||
(function() {
|
||||
if (window.location.pathname !== '/challenges') return;
|
||||
|
||||
function injectSubmissionBoxes(config) {
|
||||
function injectBoxes(config) {
|
||||
const enabledCategories = config.categories || [];
|
||||
const headers = document.querySelectorAll('.category-header');
|
||||
|
||||
headers.forEach(header => {
|
||||
document.querySelectorAll('.category-header').forEach(header => {
|
||||
const catName = header.textContent.trim();
|
||||
const alreadyInjected = header.nextElementSibling &&
|
||||
header.nextElementSibling.classList.contains('cat-sub-row');
|
||||
|
||||
if (enabledCategories.includes(catName) && !alreadyInjected) {
|
||||
// Create a row container to match CTFd's grid layout
|
||||
if (enabledCategories.includes(catName) && !header.nextElementSibling.classList.contains('cat-sub-row')) {
|
||||
const row = document.createElement('div');
|
||||
row.className = "cat-sub-row row mb-4 justify-content-center";
|
||||
|
||||
// Use col-md-8 or 10 to keep the box from being too wide
|
||||
row.innerHTML = `
|
||||
<div class="col-md-10">
|
||||
<div class="input-group custom-sub-group">
|
||||
<input type="text"
|
||||
class="form-control form-control-lg border-secondary bg-dark text-light"
|
||||
placeholder="Found a flag for ${catName}?"
|
||||
id="in-${catName.replace(/\s+/g, '-')}"
|
||||
autocomplete="off"
|
||||
style="border-color: rgba(255,255,255,0.1) !important;">
|
||||
<div class="input-group-append">
|
||||
<button class="btn btn-success btn-lg px-4 btn-sub" data-cat="${catName}">Submit</button>
|
||||
<div class="form-row">
|
||||
<div class="col-9">
|
||||
<input type="text"
|
||||
class="form-control form-control-lg bg-dark text-light border-secondary"
|
||||
placeholder="Submit a flag for ${catName}..."
|
||||
id="in-${catName.replace(/\s+/g, '-')}"
|
||||
autocomplete="off">
|
||||
</div>
|
||||
<div class="col-3">
|
||||
<button class="btn btn-success btn-lg btn-block" data-cat="${catName}">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-msg mt-2" style="min-height: 24px; font-weight: bold;"></div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
header.after(row);
|
||||
|
||||
row.querySelector('button').onclick = function() {
|
||||
const btn = this;
|
||||
const input = document.getElementById(`in-${catName.replace(/\s+/g, '-')}`);
|
||||
const container = row.querySelector('.col-md-10');
|
||||
const input = container.querySelector('input');
|
||||
const msg = container.querySelector('.status-msg');
|
||||
const val = input.value.trim();
|
||||
if (!val) return;
|
||||
|
||||
btn.disabled = true;
|
||||
msg.innerHTML = '<span class="text-info">Checking...</span>';
|
||||
|
||||
const params = new URLSearchParams({
|
||||
submission: val,
|
||||
category: catName,
|
||||
nonce: init.csrfNonce
|
||||
});
|
||||
const params = new URLSearchParams({ submission: val, category: catName, nonce: init.csrfNonce });
|
||||
|
||||
fetch('/category_submit', {
|
||||
method: 'POST',
|
||||
@@ -55,33 +47,28 @@
|
||||
})
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
alert(data.message);
|
||||
if (data.success) location.reload();
|
||||
btn.disabled = false;
|
||||
})
|
||||
.catch(() => {
|
||||
alert("Error submitting flag.");
|
||||
btn.disabled = false;
|
||||
if (data.success) {
|
||||
msg.innerHTML = `<span class="text-success">✔ ${data.message}</span>`;
|
||||
input.value = '';
|
||||
// Delay reload slightly so the user sees the success message
|
||||
setTimeout(() => { window.location.reload(); }, 1200);
|
||||
} else {
|
||||
msg.innerHTML = `<span class="text-danger">✘ ${data.message}</span>`;
|
||||
btn.disabled = false;
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fetch('/category_submit/config')
|
||||
.then(r => r.json())
|
||||
.then(config => {
|
||||
injectSubmissionBoxes(config);
|
||||
let timeout;
|
||||
const observer = new MutationObserver(() => {
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(() => {
|
||||
observer.disconnect();
|
||||
injectSubmissionBoxes(config);
|
||||
observer.observe(document.body, { childList: true, subtree: true });
|
||||
}, 200);
|
||||
});
|
||||
fetch('/category_submit/config').then(r => r.json()).then(config => {
|
||||
injectBoxes(config);
|
||||
const observer = new MutationObserver(() => {
|
||||
observer.disconnect();
|
||||
injectBoxes(config);
|
||||
observer.observe(document.body, { childList: true, subtree: true });
|
||||
})
|
||||
.catch(err => console.error("Could not load category submission config", err));
|
||||
});
|
||||
observer.observe(document.body, { childList: true, subtree: true });
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user