Display news on Google homepage
当前为
// ==UserScript==
// @name Google News Display
// @description Display news on Google homepage
// @version 1.0
// @match https://www.google.com/
// @grant GM_xmlhttpRequest
// @namespace https://greasyfork.org/users/916589
// ==/UserScript==
(function() {
// Get the user's country code (you might need to implement this logic)
const userCountryCode = getUserCountryCode(); // Implement this function accordingly
// Fetch news data based on the user's country code
const apiUrl = `https://personal-toolkit.genarunchisacoa.repl.co/news/alt?country=${userCountryCode}`;
GM_xmlhttpRequest({
method: 'GET',
url: apiUrl,
onload: function(response) {
if (response.status === 200) {
const newsData = JSON.parse(response.responseText);
displayNews(newsData);
} else {
console.error('Error fetching news:', response.statusText);
}
},
onerror: function(error) {
console.error('Error fetching news:', error);
}
});
// Function to display news titles in separate DIVs
function displayNews(newsData) {
const newsContainer = document.createElement('div');
newsContainer.style.marginTop = '20px'; // Adjust styling as needed
// Iterate through each news item and create a DIV for the title
newsData.results.forEach(item => {
const newsTitle = document.createElement('div');
newsTitle.textContent = item.title;
// You might want to add additional styling or processing for each news title
newsContainer.appendChild(newsTitle);
});
// Append the news container to the Google homepage
document.body.appendChild(newsContainer);
}
// Function to get the user's country code (you may need to implement this)
function getUserCountryCode() {
// Implement logic to get the user's country code
// For example, you can use a geolocation API or any other method
return 'us'; // Default to 'US' for demonstration purposes
}
})();