- 05aug2024
- Google Trends changed their page, breaking the script. Added @match https://trends.google.com/trending*.
- Added additional checks, but they're probably unnecessary after including a wider match.
March 1, 2024 at 13:02
I sub to Google Trends RSS to see what the most popular searches are for the day. The problem is that Google Trends sucks for finding out more. With this script, clicking on a Google Trend will bring you to the Kagi.com news search to find out more.
Just load this up in your browser’s TamperMonkey extension and you’re set!
// ==UserScript==
// @name Google Trends to Kagi News Redirector
// @namespace http://tampermonkey.net/
// @version 0.91
// @description Redirect Google Trends to today's Kagi News
// @author G-Man
// @match https://trends.google.com/trends/trendingsearches/daily*
// @match https://trends.google.com/trending*
// @match https://rss.mcwain.net/tt-rss/* # you'll need to change this obviously
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// Clear the console for easier debugging
console.clear();
// Function to handle the redirection
function redirectToKagiNews() {
console.log("Tampermonkey Script: Redirect function called.");
// Check if the current URL contains a query parameter
const urlParams = new URLSearchParams(window.location.search);
const geoParam = urlParams.get('geo');
console.log("Tampermonkey Script: Geo parameter:", geoParam);
// Check if the required geo parameter is as expected
if (geoParam === 'US') {
// Extract the hash part which seems to represent the search term
const searchTerm = window.location.hash.substring(1); // Remove the leading '#'
console.log("Tampermonkey Script: Search term from hash:", searchTerm);
if (searchTerm) {
// Decode the search term to remove URL encoding like %20
const decodedSearchTerm = decodeURIComponent(searchTerm);
console.log("Tampermonkey Script: Decoded search term:", decodedSearchTerm);
// Re-encode the search term properly for the URL
const kagiUrl = `https://kagi.com/news?q=${encodeURIComponent(decodedSearchTerm)}&freshness=1`;
console.log("Tampermonkey Script: Redirecting to:", kagiUrl);
window.location.href = kagiUrl; // Use href for redirection
} else {
console.log("Tampermonkey Script: No search term found in the URL hash.");
}
} else {
console.log("Tampermonkey Script: Geo parameter is not 'US', no redirection.");
}
}
// Run the redirection function immediately
redirectToKagiNews();
})();
Questions or comments?