neon_arch commited on
Commit
81a21d0
1 Parent(s): 1b352ec

✨ feat: add code to store the safe search as a cookie & fix code formatting (#210)

Browse files
Files changed (1) hide show
  1. public/static/settings.js +28 -22
public/static/settings.js CHANGED
@@ -1,5 +1,5 @@
1
  /**
2
- * This function handles the toggling of selections of all upstream search engines
3
  * options in the settings page under the tab engines.
4
  */
5
  function toggleAllSelection() {
@@ -8,12 +8,12 @@ function toggleAllSelection() {
8
  .forEach(
9
  (engine_checkbox) =>
10
  (engine_checkbox.checked =
11
- document.querySelector('.select_all').checked)
12
  )
13
  }
14
 
15
  /**
16
- * This function adds the functionality to sidebar buttons to only show settings
17
  * related to that tab.
18
  * @param {HTMLElement} current_tab - The current tab that was clicked.
19
  */
@@ -43,20 +43,28 @@ function setClientSettings() {
43
 
44
  // Loop through all select tags and add their values to the cookie dictionary
45
  document.querySelectorAll('select').forEach((select_tag) => {
46
- if (select_tag.name === 'themes') {
47
- cookie_dictionary['theme'] = select_tag.value
48
- } else if (select_tag.name === 'colorschemes') {
49
- cookie_dictionary['colorscheme'] = select_tag.value
 
 
 
 
 
 
50
  }
51
  })
52
 
53
  // Loop through all engine checkboxes and add their values to the cookie dictionary
54
  let engines = []
 
55
  document.querySelectorAll('.engine').forEach((engine_checkbox) => {
56
- if (engine_checkbox.checked === true) {
57
  engines.push(engine_checkbox.parentNode.parentNode.innerText.trim())
58
  }
59
  })
 
60
  cookie_dictionary['engines'] = engines
61
 
62
  // Set the expiration date for the cookie to 1 year from the current date
@@ -65,7 +73,7 @@ function setClientSettings() {
65
 
66
  // Save the cookie to the user's machine
67
  document.cookie = `appCookie=${JSON.stringify(
68
- cookie_dictionary
69
  )}; expires=${expiration_date.toUTCString()}`
70
 
71
  // Display a success message to the user
@@ -79,9 +87,9 @@ function setClientSettings() {
79
  }
80
 
81
  /**
82
- * This functions gets the saved cookies if it is present on the user's machine If it
83
- * is available then it is parsed and converted to an object which is then used to
84
- * retrieve the preferences that the user had selected previously and is then loaded in the
85
  * website otherwise the function does nothing and the default server side settings are loaded.
86
  */
87
  function getClientSettings() {
@@ -89,21 +97,19 @@ function getClientSettings() {
89
  let cookie = decodeURIComponent(document.cookie)
90
 
91
  // If the cookie is not empty, parse it and use it to set the user's preferences
92
- if (cookie !== '') {
93
- let cookie_value = decodeURIComponent(document.cookie)
94
  .split(';')
95
  .map((item) => item.split('='))
96
  .reduce((acc, [_, v]) => (acc = JSON.parse(v)) && acc, {})
97
 
98
  // Loop through all link tags and update their href values to match the user's preferences
99
- let links = Array.from(document.querySelectorAll('link')).forEach(
100
- (item) => {
101
- if (item.href.includes('static/themes')) {
102
- item.href = `static/themes/${cookie_value['theme']}.css`
103
- } else if (item.href.includes('static/colorschemes')) {
104
- item.href = `static/colorschemes/${cookie_value['colorscheme']}.css`
105
- }
106
  }
107
- )
108
  }
109
  }
 
1
  /**
2
+ * This function handles the toggling of selections of all upstream search engines
3
  * options in the settings page under the tab engines.
4
  */
5
  function toggleAllSelection() {
 
8
  .forEach(
9
  (engine_checkbox) =>
10
  (engine_checkbox.checked =
11
+ document.querySelector('.select_all').checked),
12
  )
13
  }
14
 
15
  /**
16
+ * This function adds the functionality to sidebar buttons to only show settings
17
  * related to that tab.
18
  * @param {HTMLElement} current_tab - The current tab that was clicked.
19
  */
 
43
 
44
  // Loop through all select tags and add their values to the cookie dictionary
45
  document.querySelectorAll('select').forEach((select_tag) => {
46
+ switch (select_tag.name) {
47
+ case 'themes':
48
+ cookie_dictionary['theme'] = select_tag.value
49
+ break
50
+ case 'colorschemes':
51
+ cookie_dictionary['colorscheme'] = select_tag.value
52
+ break
53
+ case 'safe_search_levels':
54
+ cookie_dictionary['safe_search_level'] = Number(select_tag.value)
55
+ break
56
  }
57
  })
58
 
59
  // Loop through all engine checkboxes and add their values to the cookie dictionary
60
  let engines = []
61
+
62
  document.querySelectorAll('.engine').forEach((engine_checkbox) => {
63
+ if (engine_checkbox.checked) {
64
  engines.push(engine_checkbox.parentNode.parentNode.innerText.trim())
65
  }
66
  })
67
+
68
  cookie_dictionary['engines'] = engines
69
 
70
  // Set the expiration date for the cookie to 1 year from the current date
 
73
 
74
  // Save the cookie to the user's machine
75
  document.cookie = `appCookie=${JSON.stringify(
76
+ cookie_dictionary,
77
  )}; expires=${expiration_date.toUTCString()}`
78
 
79
  // Display a success message to the user
 
87
  }
88
 
89
  /**
90
+ * This functions gets the saved cookies if it is present on the user's machine If it
91
+ * is available then it is parsed and converted to an object which is then used to
92
+ * retrieve the preferences that the user had selected previously and is then loaded in the
93
  * website otherwise the function does nothing and the default server side settings are loaded.
94
  */
95
  function getClientSettings() {
 
97
  let cookie = decodeURIComponent(document.cookie)
98
 
99
  // If the cookie is not empty, parse it and use it to set the user's preferences
100
+ if (cookie.length) {
101
+ let cookie_value = cookie
102
  .split(';')
103
  .map((item) => item.split('='))
104
  .reduce((acc, [_, v]) => (acc = JSON.parse(v)) && acc, {})
105
 
106
  // Loop through all link tags and update their href values to match the user's preferences
107
+ Array.from(document.querySelectorAll('link')).forEach((item) => {
108
+ if (item.href.includes('static/themes')) {
109
+ item.href = `static/themes/${cookie_value['theme']}.css`
110
+ } else if (item.href.includes('static/colorschemes')) {
111
+ item.href = `static/colorschemes/${cookie_value['colorscheme']}.css`
 
 
112
  }
113
+ })
114
  }
115
  }