Detecting your country...
Trending Topics
- Loading...
async function detectCountry() {
try {
const res = await fetch('https://ipapi.co/json/');
const data = await res.json();
const countryCode = data.country; // e.g., "IN"
document.getElementById('user-country').innerText = "You are visiting from: " + countryCode;
return countryCode;
} catch(err) {
document.getElementById('user-country').innerText = "Could not detect country";
console.error(err);
return "US"; // fallback country
}
}
async function fetchTrending(countryCode) {
const RSS_URL = `https://trends.google.com/trending/rss? ... ountryCode}`;
const list = document.getElementById("trending-list");
list.innerHTML = "";
try {
const response = await fetch(`https://api.rss2json.com/v1/api.json?rs ... t(RSS_URL)}`);
const data = await response.json();
const trendingTags = [];
data.items.forEach(item => {
const li = document.createElement("li");
li.innerHTML = `${item.title}`;
list.appendChild(li);
trendingTags.push(item.title); // collect titles for blog search
});
return trendingTags;
} catch (error) {
console.error("Error fetching trending topics:", error);
list.innerHTML = "[*]Failed to load trends.";
return [];
}
}
function fetchBlogPost(tags) {
const blogURL = "https://artisthindustani.blogspot.com"; // your blogspot URL
function fetchPostByKeyword(keyword, onSuccess, onFail) {
const callbackName = "cb_" + Date.now() + "_" + Math.floor(Math.random()*1000);
window[callbackName] = function(data) {
if (data.feed.entry && data.feed.entry.length > 0) {
const entry = data.feed.entry[0];
const title = entry.title.$t;
const link = entry.link.find(l => l.rel === "alternate").href;
let imgURL = "";
if (entry["media$thumbnail"]) {
imgURL = entry["media$thumbnail"].url;
} else {
const htmlContent = entry.content?.$t || entry.summary?.$t || "";
const imgMatch = htmlContent.match(/
` : "";
// Update DOM first
document.getElementById("matchedPost").innerHTML = `${title}` + imgHTML;
// Safely remove script after a tiny delay
setTimeout(() => {
delete window[callbackName];
document.body.removeChild(script);
}, 50);
onSuccess();
} else {
delete window[callbackName];
document.body.removeChild(script);
onFail();
}
};
const script = document.createElement("script");
script.src = `${blogURL}/feeds/posts/default?q=${encodeURIComponent(keyword)}&alt=json-in-script&max-results=1&callback=${callbackName}`;
document.body.appendChild(script);
}
function findFirstMatch(index) {
if (index >= tags.length) {
document.getElementById("matchedPost").innerHTML = "No matching blog found.";
return;
}
fetchPostByKeyword(tags[index], () => {}, () => findFirstMatch(index + 1));
}
findFirstMatch(0);
}
// Main integration
(async function() {
const countryCode = await detectCountry();
const trendingTags = await fetchTrending(countryCode);
if (trendingTags.length > 0) {
fetchBlogPost(trendingTags);
}
})();
< /code>
Пожалуйста, сделайте этот код статичным, чтобы мой конечный результат не исчезал сам. Это должно быть статично
Подробнее здесь: https://stackoverflow.com/questions/797 ... in-blogger