");
$("html,body").animate({
scrollTop: 100
}, 1000);
}
},
failure: function(errMsg) {
// console.log(errMsg);
}
});
}
}
window.fbAsyncInit = function () {
FB.init({
appId: '2016819718806904',
xfbml: true,
version: 'v18.0'
});
FB.AppEvents.logPageView();
};
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) { return; }
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function fb_login() {
FB.login(function(response) {
if( response.authResponse ) {
access_token = response.authResponse.accessToken;
user_id = response.authResponse.userID;
FB.api('/me', {
locale: 'en_US',
fields: 'name, email'
}, function(response) {
if( response != false ) {
var post_data = {
'full_name': response.name,
'username': response.email,
'email': response.email,
'fb_id': user_id,
'task': 'signup'
};
register( post_data );
}
});
} else {
$("#message").html("
Error! User cancelled login or did not fully authorize.
");
$("html,body").animate({
scrollTop: 100
}, 1000);
}
}, {
scope: 'email'
});
}
// Microsoft Login Function
function microsoft_login() {
const loginRequest = {
scopes: ["user.read", "mail.read", "openid", "profile", "email"]
};
if (!window.msalInstance) {
console.error('MSAL instance not initialized yet.');
return;
}
// Trigger Microsoft login via Popup
window.msalInstance.loginPopup(loginRequest)
.then(function (loginResponse) {
console.log('Microsoft login successful', loginResponse);
const accessToken = loginResponse.accessToken;
fetchUserProfile(accessToken);
})
.catch(function (error) {
console.error('Microsoft login failed', error);
//alert('Login failed');
$("#message").html("
Error! Microsoft login failed.
");
$("html,body").animate({
scrollTop: 100
}, 1000);
});
}
// Fetch User Profile from Microsoft Graph API
function fetchUserProfile(accessToken) {
fetch('https://graph.microsoft.com/v1.0/me', {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + accessToken
}
})
.then(response => response.json())
.then(data => {
console.log('User Profile:', data);
var post_data = {
'full_name': data.displayName,
'username': data.userPrincipalName,
'email': data.mail || data.userPrincipalName, // Fallback if mail is null
'ms_id': data.id, // Microsoft's unique user ID
'task': 'signup' // Indicate whether it's a new signup or login
};
register(post_data);
})
.catch(err => {
console.error('Error fetching user profile', err);
$("#message").html("
Error! Error fetching user profile.'
");
$("html,body").animate({
scrollTop: 100
}, 1000);
//alert('Error fetching profile');
});
}
// Initialize Apple Sign-In
AppleID.auth.init({
clientId: "com.search.login",
redirectURI: 'https://search.com',
//state: "random_string", // CSRF protection
usePopup: true
});
function apple_login() {
AppleID.auth.signIn().then(function (response) {
let id_token = response.authorization.id_token;
// Decode JWT Token in frontend (Base64)
let user_data = parseJwt(id_token);
var post_data = {
'email': user_data.email,
'app_id': user_data.sub, // Apple's unique user ID
'task': 'signup' // Indicate whether it's a new signup or login
};
let fullName = response.user ? response.user.name : null;
if (fullName) {
post_data["full_name"] = fullName.firstName + " " + fullName.lastName;
}
register(post_data);
}).catch(function (error) {
console.error("Apple Sign-In Error:", error);
$("#message").html("
Error! Error fetching user profile.
");
$("html,body").animate({
scrollTop: 100
}, 1000);
});
}
// Function to Decode Apple ID Token (JWT)
function parseJwt(token) {
try {
let base64Url = token.split('.')[1];
let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
return JSON.parse(atob(base64));
} catch (e) {
return null;
}
}
`;
notifList.innerHTML += html;
});
// Add click listeners for immediate UI update
const links = notifList.querySelectorAll('.notification-item-link');
links.forEach(link => {
link.addEventListener('click', function() {
const id = this.getAttribute('data-notification-id');
const badgeBtn = this.querySelector('.btn-new-badge');
// Remove "New" styling and badge immediately
this.classList.remove('notification-section-new');
if(badgeBtn) badgeBtn.remove();
// Handle guest read status
if (!userId || userId === "") {
if(id) saveGuestReadId(parseInt(id));
}
});
});
notifModal.dataset.unreadIds = unreadIds.join(',');
}
if(notifModal) {
notifModal.addEventListener('show.bs.modal', function () {
// Clear badge immediately for UX
updateBadge(0);
const unreadIdsStr = notifModal.dataset.unreadIds;
if(unreadIdsStr && unreadIdsStr.length > 0) {
// Handle guest read status on open
if (!userId || userId === "") {
const ids = unreadIdsStr.split(',');
ids.forEach(id => {
if(id) saveGuestReadId(id);
});
// We do NOT clear the dataset immediately or remove visual classes here
// because the user wants them to remain "New" until click or refresh.
// However, for logical consistency, we can clear the dataset so we don't re-save unnecessarily.
notifModal.dataset.unreadIds = '';
} else {
// Mark as read in background for logged-in users only
const formData = new FormData();
formData.append('user_id', userId);
formData.append('notification_ids', unreadIdsStr);
fetch(markReadUrl, {
method: 'POST',
body: formData
})
.then(res => res.json())
.then(data => {
// Successfully marked as read.
notifModal.dataset.unreadIds = '';
})
.catch(err => console.error(err));
}
}
});
}
// Always fetch notifications
fetchNotifications();
// Polling every 60 seconds
setInterval(fetchNotifications, 60000);
});
Sign Up for Free
Unlock Pro Search and History