mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-21 21:48:21 -05:00
✨ feat: add code to store the safe search as a cookie & fix code formatting (#210)
This commit is contained in:
parent
1b352ecda0
commit
81a21d0b26
@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* This function handles the toggling of selections of all upstream search engines
|
* This function handles the toggling of selections of all upstream search engines
|
||||||
* options in the settings page under the tab engines.
|
* options in the settings page under the tab engines.
|
||||||
*/
|
*/
|
||||||
function toggleAllSelection() {
|
function toggleAllSelection() {
|
||||||
@ -8,12 +8,12 @@ function toggleAllSelection() {
|
|||||||
.forEach(
|
.forEach(
|
||||||
(engine_checkbox) =>
|
(engine_checkbox) =>
|
||||||
(engine_checkbox.checked =
|
(engine_checkbox.checked =
|
||||||
document.querySelector('.select_all').checked)
|
document.querySelector('.select_all').checked),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function adds the functionality to sidebar buttons to only show settings
|
* This function adds the functionality to sidebar buttons to only show settings
|
||||||
* related to that tab.
|
* related to that tab.
|
||||||
* @param {HTMLElement} current_tab - The current tab that was clicked.
|
* @param {HTMLElement} current_tab - The current tab that was clicked.
|
||||||
*/
|
*/
|
||||||
@ -43,20 +43,28 @@ function setClientSettings() {
|
|||||||
|
|
||||||
// Loop through all select tags and add their values to the cookie dictionary
|
// 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') {
|
switch (select_tag.name) {
|
||||||
cookie_dictionary['theme'] = select_tag.value
|
case 'themes':
|
||||||
} else if (select_tag.name === 'colorschemes') {
|
cookie_dictionary['theme'] = select_tag.value
|
||||||
cookie_dictionary['colorscheme'] = 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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Loop through all engine checkboxes and add their values to the cookie dictionary
|
// 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) {
|
||||||
engines.push(engine_checkbox.parentNode.parentNode.innerText.trim())
|
engines.push(engine_checkbox.parentNode.parentNode.innerText.trim())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
cookie_dictionary['engines'] = engines
|
cookie_dictionary['engines'] = engines
|
||||||
|
|
||||||
// Set the expiration date for the cookie to 1 year from the current date
|
// Set the expiration date for the cookie to 1 year from the current date
|
||||||
@ -65,7 +73,7 @@ function setClientSettings() {
|
|||||||
|
|
||||||
// Save the cookie to the user's machine
|
// 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
|
// Display a success message to the user
|
||||||
@ -79,9 +87,9 @@ function setClientSettings() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This functions gets the saved cookies if it is present on the user's machine If it
|
* 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
|
* 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
|
* 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.
|
* website otherwise the function does nothing and the default server side settings are loaded.
|
||||||
*/
|
*/
|
||||||
function getClientSettings() {
|
function getClientSettings() {
|
||||||
@ -89,21 +97,19 @@ function getClientSettings() {
|
|||||||
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 the cookie is not empty, parse it and use it to set the user's preferences
|
||||||
if (cookie !== '') {
|
if (cookie.length) {
|
||||||
let cookie_value = decodeURIComponent(document.cookie)
|
let cookie_value = 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
|
// Loop through all link tags and update their href values to match the user's preferences
|
||||||
let links = Array.from(document.querySelectorAll('link')).forEach(
|
Array.from(document.querySelectorAll('link')).forEach((item) => {
|
||||||
(item) => {
|
if (item.href.includes('static/themes')) {
|
||||||
if (item.href.includes('static/themes')) {
|
item.href = `static/themes/${cookie_value['theme']}.css`
|
||||||
item.href = `static/themes/${cookie_value['theme']}.css`
|
} else if (item.href.includes('static/colorschemes')) {
|
||||||
} else if (item.href.includes('static/colorschemes')) {
|
item.href = `static/colorschemes/${cookie_value['colorscheme']}.css`
|
||||||
item.href = `static/colorschemes/${cookie_value['colorscheme']}.css`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user