Personalize the Snapdeal webpage by changing the overall theme color according to the following criteria:
Blue for male customers
Pink for female customers
Purple for kids
Technologies to Use:
jQuery
Chrome DevTools
CSS
Cookies
Steps/Guidelines:
Inject or Run a Custom Script on the Snapdeal Domain:
The script should be executed on every page, either automatically or manually using Chrome DevTools.
Set Cookies Based on URL Content:
On pages containing /men/ or /boy/, set a cookie as male.
Example: Boys Clothing
Example: Men's Footwear
On pages containing /women/ or /girl/, set a cookie as female.
Example: Women's Apparel
Example: Girls Clothing
On pages containing /kids/, set a cookie as kids.
Example: Kids Clothing
Apply Theme Based on Cookie Values:
Modify the website's theme color based on the value of the cookie set by the script.
For example, if the script identifies a male customer, the site's theme should be updated to blue.
Assistant
To personalize the Snapdeal webpage by changing the theme color based on the customer's gender preference, follow the steps below. This guide uses jQuery, Chrome DevTools, CSS, and cookies to detect the relevant pages and apply the appropriate theme.
Overview
Detect the User's Gender Based on the URL Content:
Set a cookie (gender) based on keywords in the URL (/men/, /women/, /kids/, etc.).
Apply the Theme Based on the Cookie Value:
Use jQuery to modify the website's CSS and change the theme color accordingly.
Right-click anywhere on the page and select Inspect to open Chrome DevTools.
Alternatively, you can press Ctrl+Shift+I (Windows/Linux) or Cmd+Option+I (macOS).
2. Switch to the Console Tab
In Chrome DevTools, click on the Console tab. This is where you'll run your custom script.
3. Copy and Paste the Custom Script
Copy the following script and paste it into the Console:
javascript
(function() {
// Function to set a cookie
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
// Function to get a cookie by name
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
// Get the current URL
var url = window.location.href.toLowerCase();
// Initialize variable to store gender
var gender = '';
// Check URL and set gender accordingly
if (url.indexOf('/men/') !== -1 || url.indexOf('/boy/') !== -1) {
gender = 'male';
} else if (url.indexOf('/women/') !== -1 || url.indexOf('/girl/') !== -1) {
gender = 'female';
} else if (url.indexOf('/kids/') !== -1) {
gender = 'kids';
}
// If gender is set, set or update the cookie
if (gender) {
// Set cookie to expire in 30 days
setCookie('gender', gender, 30);
}
// Get the value of the 'gender' cookie
var genderCookie = getCookie('gender');
// Apply theme based on the cookie value
if (genderCookie == 'male') {
// Apply blue theme
applyTheme('#0000FF'); // Blue color code
} else if (genderCookie == 'female') {
// Apply pink theme
applyTheme('#FFC0CB'); // Pink color code
} else if (genderCookie == 'kids') {
// Apply purple theme
applyTheme('#800080'); // Purple color code
}
// Function to apply theme
function applyTheme(color) {
// Change the background color of the header
$('header').css('background-color', color);
// Change the text color of navigation links
$('header a').css('color', '#FFFFFF');
// Change the background color of the footer
$('footer').css('background-color', color);
// Change the color of buttons
$('.btn, .button').css('background-color', color);
// Modify other elements as needed
// For example, change link colors
$('a').css('color', color);
// You can add more selectors to customize the theme further
}
})();
4. Press Enter to Run the Script
After pasting the script, press Enter to execute it. You should see the theme change immediately based on the page you're on or based on the previously set cookie.
5. Navigate Through Different Categories
Men's Section: Visit any page that includes /men/ or /boy/ in the URL, such as Men's Footwear.
Women's Section: Visit any page that includes /women/ or /girl/ in the URL, such as Women's Apparel.
Kids' Section: Visit any page that includes /kids/, such as Kids Clothing.
Each time you navigate to a new page, rerun the script to update the theme based on the current URL.
6. Automate the Script Execution (Optional)
To have the script run automatically on every page load, you can use a browser extension like Tampermonkey:
Click on the Tampermonkey icon in your browser toolbar and select Create a new script.
Configure the Script:
Replace the default code with the script provided above.
Update the metadata to run on Snapdeal's domain:
javascript
// ==UserScript==
// @name Snapdeal Theme Personalizer
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Personalize Snapdeal theme based on gender
// @author Your Name
// @match https://www.snapdeal.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// [Place the script here]
})();
Save the Script:
Click File > Save or press Ctrl+S.
Now, the script will automatically run on every page you visit on Snapdeal, applying the theme without manual intervention.
Explanation of the Script
Setting and Getting Cookies:
Functions setCookie and getCookie manage the creation and retrieval of cookies.
The gender cookie stores the user's gender preference based on the URL.
URL Detection:
The script converts the current URL to lowercase for consistent matching.
It checks for specific keywords (/men/, /women/, /kids/) to determine the gender.
Applying the Theme:
The applyTheme function uses jQuery to select elements and change their CSS properties.
It changes colors of the header, footer, buttons, links, and more.
Color codes are used to represent blue, pink, and purple.
Using jQuery:
The script assumes that jQuery is already loaded on the page (which is typically the case for modern websites).
If jQuery is not available, you may need to inject it manually or modify the script to use vanilla JavaScript.
Customization
Adjusting Selectors:
You can modify the selectors in the applyTheme function to target different elements.
Use Chrome DevTools to inspect elements and find the appropriate selectors.
Changing Color Codes:
Replace the hex color codes with different ones if you prefer other shades.
For example, use #1E90FF for a lighter blue or #FF69B4 for hot pink.
Extending Functionality:
Add more conditions to detect other categories or preferences.
Customize the theme further by changing fonts, borders, or adding animations.
Troubleshooting
Script Not Working:
Ensure that the script is correctly pasted into the Console.
Check for any syntax errors or typos.
Elements Not Changing:
The website's structure may have different class or ID names.
Use the Elements tab in Chrome DevTools to find the correct selectors.
jQuery Not Defined:
If you encounter a jQuery is not defined error, manually include jQuery by pasting the following in the Console:
javascript
var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-3.6.0.min.js';
document.head.appendChild(script);
Cookies Not Setting Properly:
Ensure that your browser allows cookies for Snapdeal.
Confirm that the cookie domain and path are correctly set.
Security and Permissions
Permissions:
The script runs in your browser and only modifies the page locally.
It does not send any data to external servers.
Respecting Website Terms:
This script is for personal use and educational purposes.
Be mindful of the website's terms of service and avoid any actions that may be prohibited.
Conclusion
By following the steps above, you can personalize the Snapdeal website experience by changing the theme color based on the customer's gender. This enhances the user experience and demonstrates how JavaScript, jQuery, cookies, and CSS can be combined to modify web pages dynamically.
Feel free to explore and expand upon this script to create more personalized and feature-rich customizations!