I am working on Salesforce marketing cloud.
I have created a chrome extension using JavaScript. What it has to do is , when user enters username and hits “Next” button , it has to copy the value of Username and create a cookie
The url is https://mc.exacttarget.com/cloud . when you enter it , it navigates to “https://mc.login.exacttarget.com/hub-cas/login%5C*
I have 2 files
Manifest.json :
{
"manifest_version": 3,
"name": "SFMC Login Tracker",
"version": "1.0",
"description": "Stores the username in a cookie when a user logs into SFMC.",
"permissions": ["cookies", "scripting", "activeTab"],
"host_permissions": ["https://mc.login.exacttarget.com/hub-cas/login*"],
"content_scripts": [
{
"matches": ["https://mc.login.exacttarget.com/hub-cas/login*"],
"js": ["content.js"]
}
]
}
Second is the JavaScript:
// Function to set a cookie with a specified name, value, and expiration in days
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
const expires = "expires=" + date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
// Function to extract the username when the form is submitted
function handleLogin() {
// Select the username input field; adjust the selector to match the SFMC login page
const usernameField = document.querySelector("input[name='username']");
console.log("The username is :"+usernameField);
if (usernameField) {
const username = usernameField.value;
// Set a cookie with the username, valid for 7 days
setCookie("ActiveUser", username, 7);
console.log(`Cookie set: ActiveUser=${username}`);
}
}
// Attach the handler to the login form submit event
document.addEventListener("DOMContentLoaded", () => {
const loginForm = document.querySelector("LoginForm"); // Adjust if needed
if (loginForm) {
loginForm.addEventListener("submit-btn", handleLogin);
}
});
Somehow its not creating cookie as expected. i am not sure what i am doing wrong
When i go to inspect in chrome , i was expecting a cookie with name as ActiveUser be there