From 6bc06a34f5292d20440ad34eacc83f800a6c10f8 Mon Sep 17 00:00:00 2001 From: alamin655 <129589283+alamin655@users.noreply.github.com> Date: Fri, 7 Jul 2023 20:05:55 +0530 Subject: [PATCH] Add comments and docstrings to the code --- public/static/cookies.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/public/static/cookies.js b/public/static/cookies.js index 7c27d33..677eff7 100644 --- a/public/static/cookies.js +++ b/public/static/cookies.js @@ -1,15 +1,26 @@ -// This function is executed when any page on the website finishes loading and -// this function retrieves the cookies if it is present on the user's machine. -// If it is available then the saved cookies is display in the cookies tab -// otherwise an appropriate message is displayed if it is not available. +/** + * This function is executed when any page on the website finishes loading and + * this function retrieves the cookies if it is present on the user's machine. + * If it is available then the saved cookies is display in the cookies tab + * otherwise an appropriate message is displayed if it is not available. + * + * @function + * @listens DOMContentLoaded + * @returns {void} + */ document.addEventListener( 'DOMContentLoaded', () => { try { + // Decode the cookie value let cookie = decodeURIComponent(document.cookie) + // Set the value of the input field to the decoded cookie value if it is not empty + // Otherwise, display a message indicating that no cookies have been saved on the user's system document.querySelector('.cookies input').value = cookie !== '' ? cookie : 'No cookies have been saved on your system' } catch (error) { + // If there is an error decoding the cookie, log the error to the console + // and display an error message in the input field console.error('Error decoding cookie:', error) document.querySelector('.cookies input').value = 'Error decoding cookie' }