add addition to this code to post the first 300 words of each article posted when you click the headline, and the read full article here button after the 300 word summary to keep the reader on our site longer.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Great idea! This will definitely keep readers engaged on your site longer. I'll modify the code to show a preview of each article when someone clicks the headline, displaying the first 300 words before they click through to the full article.
Here's the updated code with the article preview feature:
xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html b:css='false' b:layoutsVersion='3' b:responsive='true' b:templateVersion='1.3.0' expr:dir='data:blog.languageDirection' lang='en'>
<head>
<meta charset='UTF-8'/>
<meta content='width=device-width, initial-scale=1.0' name='viewport'/>
<title><data:blog.pageTitle/></title>
<b:skin><![CDATA[
/* --- Section 8 Racing CSS --- */
/* You can edit colors and fonts here */
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: #1a1a1a; /* Dark background */
color: #f0f0f0;
margin: 0;
padding: 0;
}
.header {
background-color: #000;
padding: 20px;
text-align: center;
border-bottom: 4px solid #ff6600; /* Orange accent */
}
.header h1 {
margin: 0;
font-size: 2.5em;
color: #ffffff;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 2px;
}
.header h1 span {
color: #ff6600; /* Orange accent for part of the title */
}
.main-content {
max-width: 900px;
margin: 20px auto;
padding: 0 15px;
}
#news-feed {
display: grid;
grid-template-columns: 1fr;
gap: 25px;
}
.news-item {
background-color: #2b2b2b;
border: 1px solid #444;
border-radius: 8px;
overflow: hidden;
display: flex;
flex-direction: column;
transition: transform 0.2s ease-in-out;
}
.news-item:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0,0,0,0.5);
}
.news-item-image-container {
width: 100%;
height: 200px;
background-color: #333;
overflow: hidden;
}
.news-item-image {
width: 100%;
height: 100%;
object-fit: cover; /* This makes the image cover the container */
border-bottom: 1px solid #444;
}
.news-item-content {
padding: 20px;
display: flex;
flex-direction: column;
flex-grow: 1;
}
.news-item-title {
font-size: 1.4em;
margin: 0 0 10px 0;
line-height: 1.3;
}
.news-item-title a {
text-decoration: none;
color: #ffffff;
cursor: pointer;
}
.news-item-title a:hover {
color: #ff6600;
}
.news-item-meta {
font-size: 0.8em;
color: #aaa;
margin-bottom: 15px;
}
.read-more-link {
display: inline-block;
background-color: #ff6600;
color: #ffffff;
padding: 10px 15px;
border-radius: 5px;
text-decoration: none;
font-weight: bold;
text-transform: uppercase;
margin-top: auto; /* Pushes button to the bottom */
align-self: flex-start; /* Aligns button to the left */
cursor: pointer;
}
.read-more-link:hover {
background-color: #e65c00;
}
.loader {
text-align: center;
font-size: 1.2em;
padding: 40px;
color: #ff6600;
}
.footer {
text-align: center;
padding: 20px;
margin-top: 30px;
font-size: 0.9em;
color: #888;
border-top: 1px solid #444;
}
/* Modal Styles for Article Preview */
.modal-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.9);
z-index: 1000;
overflow-y: auto;
}
.modal-content {
background-color: #2b2b2b;
margin: 50px auto;
padding: 0;
border: 1px solid #444;
width: 90%;
max-width: 800px;
border-radius: 8px;
position: relative;
}
.modal-close {
color: #aaa;
position: absolute;
top: 20px;
right: 20px;
font-size: 28px;
font-weight: bold;
cursor: pointer;
z-index: 1001;
background-color: rgba(0, 0, 0, 0.7);
width: 40px;
height: 40px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
.modal-close:hover,
.modal-close:focus {
color: #ff6600;
}
.modal-article-image {
width: 100%;
height: 300px;
object-fit: cover;
}
.modal-article-content {
padding: 30px;
}
.modal-article-title {
font-size: 2em;
color: #ffffff;
margin: 0 0 15px 0;
}
.modal-article-meta {
font-size: 0.9em;
color: #aaa;
margin-bottom: 20px;
padding-bottom: 20px;
border-bottom: 1px solid #444;
}
.modal-article-text {
line-height: 1.6;
font-size: 1.1em;
color: #e0e0e0;
margin-bottom: 30px;
}
.modal-read-full {
display: inline-block;
background-color: #ff6600;
color: #ffffff;
padding: 15px 30px;
border-radius: 5px;
text-decoration: none;
font-weight: bold;
text-transform: uppercase;
font-size: 1.1em;
margin-top: 20px;
}
.modal-read-full:hover {
background-color: #e65c00;
}
/* Responsive Design for smaller screens */
@media (max-width: 600px) {
.header h1 {
font-size: 1.8em;
}
.news-item {
flex-direction: column;
}
.modal-content {
margin: 20px auto;
width: 95%;
}
.modal-article-content {
padding: 20px;
}
.modal-article-title {
font-size: 1.5em;
}
}
]]></b:skin>
</head>
<body>
<header class='header'>
<h1>SECTION <span>8</span> RACING</h1>
</header>
<main class='main-content'>
<div id='news-feed'>
<div class='loader'>Starting the gate... Loading latest news...</div>
</div>
<!-- This is where the blog posts would normally go, we leave it empty -->
<b:section class='main' id='main' showaddelement='no'>
<b:widget id='Blog1' locked='true' title='Blog Posts' type='Blog' version='1'/>
</b:section>
</main>
<!-- Article Preview Modal -->
<div id='articleModal' class='modal-overlay'>
<div class='modal-content'>
<span class='modal-close'>×</span>
<img id='modalImage' class='modal-article-image' src='' alt=''/>
<div class='modal-article-content'>
<h2 id='modalTitle' class='modal-article-title'></h2>
<div id='modalMeta' class='modal-article-meta'></div>
<div id='modalText' class='modal-article-text'></div>
<a id='modalFullLink' href='#' target='_blank' rel='noopener noreferrer' class='modal-read-full'>Read Full Article Here</a>
</div>
</div>
</div>
<footer class='footer'>
<p>Section 8 Racing | All news content linked is property of its respective owner.</p>
</footer>
<!-- All the magic happens here! -->
<script>
//<![CDATA[
document.addEventListener('DOMContentLoaded', function() {
// =========================================================================
// START: RSS FEED CONFIGURATION - EDIT THIS SECTION
// =========================================================================
// Add the RSS feed URLs you want to display here.
// To find a feed, search for "[website name] RSS feed".
// Make sure each line ends with a comma, except for the last one.
const feedUrls = [
'https://racerxonline.com/category/news/feed', // Racer X News
'https://www.vitalmx.com/community/main.rss', // Vital MX
'http://www.motoonline.com.au/feed/', // MotoOnline
'https://motocross.transworld.net/feed/', // Transworld Motocross (may be defunct, good example)
'https://www.cyclenews.com/category/off-road/feed/' // Cycle News Off-Road
];
// A placeholder image for articles that don't have one.
// You can change this URL to an image you prefer.
const placeholderImage = 'https://i.ibb.co/6yC1N1Y/motocross-placeholder.jpg';
// =========================================================================
// END: RSS FEED CONFIGURATION
// =========================================================================
const newsFeedContainer = document.getElementById('news-feed');
const loader = newsFeedContainer.querySelector('.loader');
// Modal elements
const modal = document.getElementById('articleModal');
const modalClose = modal.querySelector('.modal-close');
const modalImage = document.getElementById('modalImage');
const modalTitle = document.getElementById('modalTitle');
const modalMeta = document.getElementById('modalMeta');
const modalText = document.getElementById('modalText');
const modalFullLink = document.getElementById('modalFullLink');
// This function fetches and parses a single RSS feed
async function fetchFeed(url) {
// We use a CORS proxy to bypass browser security restrictions.
// This is necessary because we're fetching data from other websites.
const proxyUrl = 'https://api.allorigins.win/raw?url=';
try {
const response = await fetch(proxyUrl + encodeURIComponent(url));
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const text = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, "application/xml");
const items = xmlDoc.querySelectorAll("item");
const articles = [];
items.forEach(item => {
const title = item.querySelector("title")?.textContent || 'No Title';
const link = item.querySelector("link")?.textContent || '#';
const pubDate = item.querySelector("pubDate")?.textContent || new Date().toISOString();
const description = item.querySelector("description")?.textContent || '';
const creator = item.querySelector("dc\\:creator")?.textContent || getDomainName(link);
// Try to get full content from content:encoded or use description
const contentEncoded = item.querySelector("content\\:encoded")?.textContent || '';
const fullContent = contentEncoded || description;
articles.push({
title: title,
link: link,
pubDate: new Date(pubDate),
description: description,
fullContent: fullContent,
source: creator,
image: extractImage(item, description)
});
});
return articles;
} catch (error) {
console.error(`Failed to fetch or parse feed: ${url}`, error);
return []; // Return empty array on failure
}
}
// Function to extract the image from an article item
function extractImage(item, description) {
// 1. Look for a 'media:content' tag (common in modern feeds)
const mediaContent = item.querySelector('media\\:content, content');
if (mediaContent && mediaContent.getAttribute('url')) {
return mediaContent.getAttribute('url');
}
// 2. Look for an 'enclosure' tag
const enclosure = item.querySelector('enclosure');
if (enclosure && enclosure.getAttribute('url') && enclosure.getAttribute('type').startsWith('image/')) {
return enclosure.getAttribute('url');
}
// 3. Search for the first <img> tag in the description HTML
const descDoc = new DOMParser().parseFromString(description, 'text/html');
const firstImg = descDoc.querySelector('img');
if (firstImg && firstImg.src) {
return firstImg.src;
}
// 4. If all else fails, use the placeholder
return placeholderImage;
}
// Function to get the website's name from its URL
function getDomainName(url) {
try {
const hostname = new URL(url).hostname;
let parts = hostname.split('.');
if (parts.length > 2) {
return parts.slice(-2, -1)[0]; // e.g., 'racerxonline' from 'www.racerxonline.com'
}
return parts[0];
} catch (e) {
return 'Unknown Source';
}
}
// Function to format the date
function formatDate(date) {
return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
}
// Function to extract plain text from HTML and limit to approximately 300 words
function extractPreviewText(htmlContent, wordLimit = 300) {
// Create a temporary element to parse the HTML
const tempDiv = document.createElement('div');
tempDiv.innerHTML = htmlContent;
// Get just the text content
let text = tempDiv.textContent || tempDiv.innerText || '';
// Clean up extra whitespace
text = text.replace(/\s+/g, ' ').trim();
// Split into words and take first 300
const words = text.split(' ');
if (words.length > wordLimit) {
return words.slice(0, wordLimit).join(' ') + '...';
}
return text;
}
// Function to show the article modal
function showArticleModal(article) {
modalImage.src = article.image;
modalImage.alt = article.title;
modalTitle.textContent = article.title;
modalMeta.innerHTML = `By <strong>${article.source}</strong> on ${formatDate(article.pubDate)}`;
modalText.textContent = extractPreviewText(article.fullContent);
modalFullLink.href = article.link;
modal.style.display = 'block';
document.body.style.overflow = 'hidden'; // Prevent background scrolling
}
// Function to close the modal
function closeModal() {
modal.style.display = 'none';
document.body.style.overflow = 'auto'; // Re-enable scrolling
}
// Modal event listeners
modalClose.onclick = closeModal;
// Close modal when clicking outside of it
window.onclick = function(event) {
if (event.target == modal) {
closeModal();
}
}
// Close modal with Escape key
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape' && modal.style.display === 'block') {
closeModal();
}
});
// Main function to load all feeds and display them
async function loadAllFeeds() {
const allPromises = feedUrls.map(url => fetchFeed(url));
const allArticleArrays = await Promise.all(allPromises);
// Combine all articles into one big array
let combinedArticles = [].concat(...allArticleArrays);
// Sort all articles by publication date, newest first
combinedArticles.sort((a, b) => b.pubDate - a.pubDate);
// Clear the loader
newsFeedContainer.innerHTML = '';
if (combinedArticles.length === 0) {
newsFeedContainer.innerHTML = '<p>Could not load any news feeds. Please check the feed URLs or try again later.</p>';
return;
}
// Create and append the HTML for each article
combinedArticles.forEach((article, index) => {
const articleElement = document.createElement('div');
articleElement.className = 'news-item';
articleElement.innerHTML = `
<div class="news-item-image-container">
<img src="${article.image}" alt="${article.title}" class="news-item-image" onerror="this.onerror=null;this.src='${placeholderImage}';"/>
</div>
<div class="news-item-content">
<h2 class="news-item-title">
<a href="#" data-article-index="${index}">${article.title}</a>
</h2>
<p class="news-item-meta">
By <strong>${article.source}</strong> on ${formatDate(article.pubDate)}
</p>
<button class="read-more-link" data-article-index="${index}">Read Article</button>
</div>
`;
newsFeedContainer.appendChild(articleElement);
// Add click event listeners to the title and button
const titleLink = articleElement.querySelector('.news-item-title a');
const readButton = articleElement.querySelector('.read-more-link');
titleLink.addEventListener('click', function(e) {
e.preventDefault();
showArticleModal(combinedArticles[index]);
});
readButton.addEventListener('click', function(e) {
e.preventDefault();
showArticleModal(combinedArticles[index]);
});
});
}
// Start the process
loadAllFeeds();
});
//]]>
</script>
</body>
</html>
What's New in This Version:
Article Preview Modal: When you click any headline or the "Read Article" button, a beautiful overlay appears showing:
The article's featured image
The title and publication info
The first 300 words of the article
A prominent "Read Full Article Here" button
Enhanced User Experience:
Clicking the headline no longer immediately takes you away from your site
Readers get a taste of the content before leaving
The modal can be closed by clicking the X, pressing Escape, or clicking outside the content area
Improved Engagement: Visitors will spend more time on your site reading previews before deciding which full articles to read
Installation Instructions:
The installation process is exactly the same as before! Just follow the same steps from my previous answer:
Go to your Blogger Dashboard
Click Theme in the left menu
Back up your current theme (click the dropdown next to CUSTOMIZE > Backup > Download)
Click the dropdown again and select Edit HTML
Select all the existing code (Ctrl+A or Cmd+A) and delete it
Copy the entire new code block above and paste it in
Click the Save icon
View your blog to see the new preview feature in action!
How the New Feature Works:
Click any headline: Opens a preview with the first 300 words
Click "Read Article" button: Also opens the preview
Click "Read Full Article Here": Takes you to the original website
Close the preview: Click the X, press Escape, or click the dark background
The code automatically extracts article content from RSS feeds and displays it beautifully. This keeps visitors engaged with your site while still respecting the original publishers by linking to their full articles.