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

Senitize HTML In swapDropdown Method #294

Merged
merged 2 commits into from
Dec 10, 2024
Merged
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
26 changes: 15 additions & 11 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,14 @@ document.addEventListener("DOMContentLoaded", () => {
element.addEventListener('click', () => {
const engine = element.getAttribute('data-engine');
const radioButton = document.querySelector(`input[type="radio"][value="engine${engine}"]`);
const selector = `*[data-engine-name=${element.getAttribute('data-engine-name')}]`;

// console.log(element, selector);

radioButton.checked = true;

// Swap The dropdown. and sort them
swapDropdown(element);
swapDropdown(selector);
sortDropdown()

localStorage.setItem("selectedSearchEngine", radioButton.value);
Expand All @@ -732,10 +735,10 @@ document.addEventListener("DOMContentLoaded", () => {

const radioButtonValue = radioButton.value.charAt(radioButton.value.length - 1);

const element = document.querySelector(`[data-engine="${radioButtonValue}"]`);
const selector = `[data-engine="${radioButtonValue}"]`;

// Swap The dropdown.
swapDropdown(element);
swapDropdown(selector);
sortDropdown()

localStorage.setItem("selectedSearchEngine", radioButton.value);
Expand All @@ -749,15 +752,16 @@ document.addEventListener("DOMContentLoaded", () => {
*/
function swapDropdown(selectedElement) {
// Swap innerHTML
const element = document.querySelector(selectedElement);
const tempHTML = defaultEngine.innerHTML;
defaultEngine.innerHTML = selectedElement.innerHTML;
selectedElement.innerHTML = tempHTML;
defaultEngine.innerHTML = element.innerHTML;
element.innerHTML = tempHTML;

// Swap attributes
['data-engine', 'data-engine-name', 'id'].forEach(attr => {
const tempAttr = defaultEngine.getAttribute(attr);
defaultEngine.setAttribute(attr, selectedElement.getAttribute(attr));
selectedElement.setAttribute(attr, tempAttr);
defaultEngine.setAttribute(attr, element.getAttribute(attr));
element.setAttribute(attr, tempAttr);
});
}

Expand Down Expand Up @@ -799,9 +803,9 @@ document.addEventListener("DOMContentLoaded", () => {
// check if the default selected search engine is same as the stored one.
if (storedSearchEngineSN !== defaultDropdownSN) {
// The following line will find out the appropriate dropdown for the selected search engine.
const storedSearchEngineDropdown = document.querySelector(`*[data-engine="${storedSearchEngineSN}"]`);
const selector = `*[data-engine="${storedSearchEngineSN}"]`;

swapDropdown(storedSearchEngineDropdown);
swapDropdown(selector);
sortDropdown();
}

Expand Down Expand Up @@ -844,14 +848,14 @@ document.addEventListener("DOMContentLoaded", () => {
} else if (event.key === 'ArrowUp') {
selectedIndex = (selectedIndex - 1 + dropdownItems.length) % dropdownItems.length; // Move up, loop around
} else if (event.key === "Enter") {
const element = document.querySelector('.dropdown-content .selected');
const selector = '.dropdown-content .selected';
const engine = element.getAttribute('data-engine');
const radioButton = document.querySelector(`input[type="radio"][value="engine${engine}"]`);

radioButton.checked = true;

// Swap The dropdown. and sort them
swapDropdown(element);
swapDropdown(selector);
sortDropdown()
}
updateSelection();
Expand Down