Skip to content

Commit

Permalink
modified: js/item.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Apr 17, 2024
1 parent d2fc28f commit dafc8e1
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions js/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,54 +185,61 @@ function generateTranslationDiv(translationLink, isMod, isVerified) {
$('#TranslationContainer').html(translationDiv);


// Function to update OG and Twitter meta tags
// Function to update Open Graph (og) meta tags
function updateMetaTags(selectedItem) {
// Remove existing meta tags for Open Graph and Twitter
document.querySelectorAll('meta[property^="og:"], meta[name^="twitter:"]').forEach(tag => tag.remove());
// Remove existing meta tags for Open Graph
document.querySelectorAll('meta[property^="og:"]').forEach(tag => tag.remove());

// Update meta description
const metaDescription = document.createElement('meta');
metaDescription.setAttribute('name', 'description');
metaDescription.setAttribute('content', selectedItem.description);
document.head.appendChild(metaDescription);
// Update og:title
const ogTitle = document.createElement('meta');
ogTitle.setAttribute('property', 'og:title');
ogTitle.setAttribute('content', "СУМ" + " - переклад " + selectedItem.title);
document.head.appendChild(ogTitle);

// Update og:description
const ogDescription = document.createElement('meta');
ogDescription.setAttribute('property', 'og:description');
ogDescription.setAttribute('content', truncateText(selectedItem.description, 120));
document.head.appendChild(ogDescription);

// Update meta og:title
const ogTitle = document.createElement('meta');
ogTitle.setAttribute('property', 'og:title');
ogTitle.setAttribute('content', "СУМ" + " - переклад " + selectedItem.title);
document.head.appendChild(ogTitle);

// Update meta og:image
// Update og:image
const ogImage = document.createElement('meta');
ogImage.setAttribute('property', 'og:image');
ogImage.setAttribute('content', selectedItem.image);
document.head.appendChild(ogImage);
}

// Function to check if the user agent is a known social network bot
function isSocialBot() {
const userAgent = navigator.userAgent.toLowerCase();
// Add checks here to detect social network bots based on user agent
// Example: return userAgent.includes('facebookexternalhit') || userAgent.includes('twitterbot');
return false; // Placeholder, update with actual checks
}

// Update meta twitter:description
const twitterDescription = document.createElement('meta');
twitterDescription.setAttribute('name', 'twitter:description');
twitterDescription.setAttribute('content', truncateText(selectedItem.description, 120));
document.head.appendChild(twitterDescription);

// Update meta twitter:title
const twitterTitle = document.createElement('meta');
twitterTitle.setAttribute('name', 'twitter:title');
twitterTitle.setAttribute('content', "СУМ" + " - переклад " + selectedItem.title);
document.head.appendChild(twitterTitle);

// Update meta twitter:image
const twitterImage = document.createElement('meta');
twitterImage.setAttribute('name', 'twitter:image');
twitterImage.setAttribute('content', selectedItem.image);
document.head.appendChild(twitterImage);
// Function to handle dynamic generation of meta tags
function handleDynamicMetaTags() {
if (isSocialBot()) {
// Bot detected, fetch data and update meta tags
const itemId = getItemId(); // Replace with your logic to get item ID
if (itemId) {
fetchItemData(itemId)
.then(({ selectedItem }) => {
if (selectedItem) {
// Call function to update meta tags
updateMetaTags(selectedItem);
}
})
.catch(error => {
console.error("Error fetching item data:", error);
});
}
}
}

// Event listener for when the page has finished loading
window.addEventListener('load', handleDynamicMetaTags);

window.addEventListener('load', updateItemPage);

// Function to update the item page with the fetched data
Expand Down

0 comments on commit dafc8e1

Please sign in to comment.