Brave - Discussions Reddit Title Replacer

Enhances the Brave Search results page by adding click functionality to navigate to source links and automatically extending these features to newly loaded content.

// ==UserScript==
// @name         Brave - Discussions Reddit Title Replacer
// @version      2.0
// @description  Enhances the Brave Search results page by adding click functionality to navigate to source links and automatically extending these features to newly loaded content.
// @author       Misspent & OpenAI
// @namespace    https://chat.openai.com
// @icon         https://i.imgur.com/HBBSMUb.png
// @match        https://search.brave.com/*
// @license      MIT
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    // Function to enhance a title element
    function enhanceTitleElement(titleElement) {
        // Find the parent container of the clicked title element
        const parentContainer = titleElement.closest('details.fq-item');

        if (parentContainer) {
            // Find the href link in the .source element
            const sourceLink = parentContainer.querySelector('a.source');

            if (sourceLink) {
                // Replace the title text with a link to the href
                titleElement.innerHTML = `<a href="${sourceLink.href}">${titleElement.textContent}</a>`;
            }
        }
    }

    // Function to enhance all existing title elements
    function enhanceExistingTitleElements() {
        document.querySelectorAll('#discussions summary p.title').forEach(titleElement => {
            if (!titleElement.hasAttribute('data-enhanced')) {
                enhanceTitleElement(titleElement);
                titleElement.setAttribute('data-enhanced', 'true');
            }
        });
    }

    // Observe changes in the body (or specify a more specific element if needed)
    const observer = new MutationObserver(mutationsList => {
        for (const mutation of mutationsList) {
            if (mutation.type === 'childList') {
                // DOM elements have changed, enhance newly added links
                enhanceExistingTitleElements();
            }
        }
    });

    // Add the CSS styles dynamically using a <style> element
    const styleElement = document.createElement('style');
    styleElement.textContent = `
        #discussions summary div p a {color: #fff;}
        #discussions summary div p a:hover {
            color: var(--link-secondary);
            text-decoration: underline !important;
        }
    `;
    document.head.appendChild(styleElement);

    // Initial enhancement for existing elements
    enhanceExistingTitleElements();

    // Start observing changes
    observer.observe(document.body, { childList: true, subtree: true });
})();




/*

Write a Brave Search Engine TamperMonkey script that does the following:
1. Make the @namespace https://chat.openai.com
2. Make the @author Misspent & OpenAI
3. Give each section/function a brief description of what it is like you normally do
4. Can you make it so when I click "#discussions summary p.title" it takes me to the "a.source" href link that's in the "details.fq-item" of the container where I'm clicking (that's the parent of all of them).
+ Can you make it change the color of the "#discussions summary p.title" to deepskyblue when I hover over it, please. Remove it when I stop hovering over it too.
+ Can you make it so this one picks up newly loaded ones too
+ Can you give it a better TamperMonkey @description
+ Instead of redirecting the href link, turn the title into the link

I removed the fullstop it put at the start of "details.fq-item"

I then added this to it:

    // Add the CSS styles dynamically using a <style> element
    const styleElement = document.createElement('style');
    styleElement.textContent = `
        #discussions summary div p a {color: #fff;}
        #discussions summary div p a:hover {
            color: var(--link-secondary);
            text-decoration: underline !important;
        }
    `;
    document.head.appendChild(styleElement);


*/