0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-22 05:58:21 -05:00

Add comments and docstrings to the code

This commit is contained in:
alamin655 2023-07-07 20:09:43 +05:30 committed by GitHub
parent 09d9573c55
commit a615fffdf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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