2023-07-07 10:39:43 -04:00
|
|
|
/**
|
2023-09-22 12:39:50 -04:00
|
|
|
* This function handles the toggling of selections of all upstream search engines
|
2023-07-07 10:39:43 -04:00
|
|
|
* options in the settings page under the tab engines.
|
|
|
|
*/
|
2023-06-18 12:50:32 -04:00
|
|
|
function toggleAllSelection() {
|
|
|
|
document
|
|
|
|
.querySelectorAll('.engine')
|
|
|
|
.forEach(
|
|
|
|
(engine_checkbox) =>
|
|
|
|
(engine_checkbox.checked =
|
2023-09-22 12:39:50 -04:00
|
|
|
document.querySelector('.select_all').checked),
|
2023-06-18 12:50:32 -04:00
|
|
|
)
|
2023-05-31 09:05:02 -04:00
|
|
|
}
|
|
|
|
|
2023-07-07 10:39:43 -04:00
|
|
|
/**
|
2023-09-22 12:39:50 -04:00
|
|
|
* This function adds the functionality to sidebar buttons to only show settings
|
2023-07-07 10:39:43 -04:00
|
|
|
* related to that tab.
|
|
|
|
* @param {HTMLElement} current_tab - The current tab that was clicked.
|
|
|
|
*/
|
2023-06-18 12:50:32 -04:00
|
|
|
function setActiveTab(current_tab) {
|
2023-07-07 10:39:43 -04:00
|
|
|
// Remove the active class from all tabs and buttons
|
2023-06-18 12:50:32 -04:00
|
|
|
document
|
|
|
|
.querySelectorAll('.tab')
|
|
|
|
.forEach((tab) => tab.classList.remove('active'))
|
|
|
|
document
|
|
|
|
.querySelectorAll('.btn')
|
|
|
|
.forEach((tab) => tab.classList.remove('active'))
|
2023-07-07 10:39:43 -04:00
|
|
|
|
|
|
|
// Add the active class to the current tab and its corresponding settings
|
2023-06-18 12:50:32 -04:00
|
|
|
current_tab.classList.add('active')
|
|
|
|
document
|
|
|
|
.querySelector(`.${current_tab.innerText.toLowerCase().replace(' ', '_')}`)
|
|
|
|
.classList.add('active')
|
|
|
|
}
|
2023-05-31 09:05:02 -04:00
|
|
|
|
2023-07-07 10:39:43 -04:00
|
|
|
/**
|
|
|
|
* This function adds the functionality to save all the user selected preferences
|
|
|
|
* to be saved in a cookie on the users machine.
|
|
|
|
*/
|
2023-06-18 12:50:32 -04:00
|
|
|
function setClientSettings() {
|
2023-07-07 10:39:43 -04:00
|
|
|
// Create an object to store the user's preferences
|
2023-06-18 12:50:32 -04:00
|
|
|
let cookie_dictionary = new Object()
|
2023-07-07 10:39:43 -04:00
|
|
|
|
|
|
|
// Loop through all select tags and add their values to the cookie dictionary
|
2023-06-18 12:50:32 -04:00
|
|
|
document.querySelectorAll('select').forEach((select_tag) => {
|
2023-09-22 12:39:50 -04:00
|
|
|
switch (select_tag.name) {
|
|
|
|
case 'themes':
|
|
|
|
cookie_dictionary['theme'] = select_tag.value
|
|
|
|
break
|
|
|
|
case 'colorschemes':
|
|
|
|
cookie_dictionary['colorscheme'] = select_tag.value
|
|
|
|
break
|
|
|
|
case 'safe_search_levels':
|
|
|
|
cookie_dictionary['safe_search_level'] = Number(select_tag.value)
|
|
|
|
break
|
2023-06-04 10:31:02 -04:00
|
|
|
}
|
2023-06-18 12:50:32 -04:00
|
|
|
})
|
2023-07-07 10:39:43 -04:00
|
|
|
|
|
|
|
// Loop through all engine checkboxes and add their values to the cookie dictionary
|
2023-06-18 12:50:32 -04:00
|
|
|
let engines = []
|
2023-09-22 12:39:50 -04:00
|
|
|
|
2023-06-18 12:50:32 -04:00
|
|
|
document.querySelectorAll('.engine').forEach((engine_checkbox) => {
|
2023-09-22 12:39:50 -04:00
|
|
|
if (engine_checkbox.checked) {
|
2023-06-18 12:50:32 -04:00
|
|
|
engines.push(engine_checkbox.parentNode.parentNode.innerText.trim())
|
|
|
|
}
|
|
|
|
})
|
2023-09-22 12:39:50 -04:00
|
|
|
|
2023-06-18 12:50:32 -04:00
|
|
|
cookie_dictionary['engines'] = engines
|
2023-07-07 10:39:43 -04:00
|
|
|
|
|
|
|
// Set the expiration date for the cookie to 1 year from the current date
|
2023-06-18 12:50:32 -04:00
|
|
|
let expiration_date = new Date()
|
|
|
|
expiration_date.setFullYear(expiration_date.getFullYear() + 1)
|
2023-07-07 10:39:43 -04:00
|
|
|
|
|
|
|
// Save the cookie to the user's machine
|
2023-06-18 12:50:32 -04:00
|
|
|
document.cookie = `appCookie=${JSON.stringify(
|
2023-09-22 12:39:50 -04:00
|
|
|
cookie_dictionary,
|
2023-06-18 12:50:32 -04:00
|
|
|
)}; expires=${expiration_date.toUTCString()}`
|
2023-05-31 09:05:02 -04:00
|
|
|
|
2023-07-07 10:39:43 -04:00
|
|
|
// Display a success message to the user
|
2023-06-18 12:50:32 -04:00
|
|
|
document.querySelector('.message').innerText =
|
|
|
|
'✅ The settings have been saved sucessfully!!'
|
2023-06-03 03:26:42 -04:00
|
|
|
|
2023-07-07 10:39:43 -04:00
|
|
|
// Clear the success message after 10 seconds
|
2023-06-18 12:50:32 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
document.querySelector('.message').innerText = ''
|
|
|
|
}, 10000)
|
2023-05-31 09:05:02 -04:00
|
|
|
}
|
|
|
|
|
2023-07-07 10:39:43 -04:00
|
|
|
/**
|
2023-09-22 12:39:50 -04:00
|
|
|
* This functions gets the saved cookies if it is present on the user's machine If it
|
|
|
|
* is available then it is parsed and converted to an object which is then used to
|
|
|
|
* retrieve the preferences that the user had selected previously and is then loaded in the
|
2023-07-07 10:39:43 -04:00
|
|
|
* website otherwise the function does nothing and the default server side settings are loaded.
|
|
|
|
*/
|
2023-06-18 12:50:32 -04:00
|
|
|
function getClientSettings() {
|
2023-07-07 10:39:43 -04:00
|
|
|
// Get the appCookie from the user's machine
|
2023-06-18 12:50:32 -04:00
|
|
|
let cookie = decodeURIComponent(document.cookie)
|
2023-06-04 10:31:02 -04:00
|
|
|
|
2023-07-07 10:39:43 -04:00
|
|
|
// If the cookie is not empty, parse it and use it to set the user's preferences
|
2023-09-22 12:39:50 -04:00
|
|
|
if (cookie.length) {
|
|
|
|
let cookie_value = cookie
|
2023-06-18 12:50:32 -04:00
|
|
|
.split(';')
|
|
|
|
.map((item) => item.split('='))
|
|
|
|
.reduce((acc, [_, v]) => (acc = JSON.parse(v)) && acc, {})
|
2023-06-04 10:31:02 -04:00
|
|
|
|
2023-07-07 10:39:43 -04:00
|
|
|
// Loop through all link tags and update their href values to match the user's preferences
|
2023-09-22 12:39:50 -04:00
|
|
|
Array.from(document.querySelectorAll('link')).forEach((item) => {
|
|
|
|
if (item.href.includes('static/themes')) {
|
|
|
|
item.href = `static/themes/${cookie_value['theme']}.css`
|
|
|
|
} else if (item.href.includes('static/colorschemes')) {
|
|
|
|
item.href = `static/colorschemes/${cookie_value['colorscheme']}.css`
|
2023-06-18 12:50:32 -04:00
|
|
|
}
|
2023-09-22 12:39:50 -04:00
|
|
|
})
|
2023-06-04 10:31:02 -04:00
|
|
|
}
|
2023-05-31 09:05:02 -04:00
|
|
|
}
|