Dup Goto 📝

WatchElementInTabTitle

PT2/lang/js/user-scripts/examples 08-25 20:03:35
To Pop
150 lines, 550 words, 4855 chars Tuesday 2026-08-25 20:03:35

I often use these for things like football or cricket scores. Provided the score is contained in a single element, this will duplicate its text content to the tab title. If necessary, splice in some text manipulation.

One useful trick, with selectors, is to add an id='bobbins' attribute to what you want to watch, so you can use the #bobbins selector.

window.q = (x,y=document) => y.querySelector(x)
window.qq = (x,y=document) => Array.from(y.querySelectorAll(x))

function watch(x,dt=1000) {
    if( window.titleInterval ) clearInterval(window.titleInterval)
    window.titleInterval = setInterval(_ => {
        let a = q(x)
        if( a ) {
            let t = a.textContent
            document.title = t
        }
    },dt)
}
window.watch = watch

v2

This allows you to transform the text before putting it in the title bar.

window.q = (x,y=document) => y.querySelector(x)
window.qq = (x,y=document) => Array.from(y.querySelectorAll(x))

function watch(x, dt = 1000, callback = (x) => x) {
    if( window.titleInterval ) clearInterval(window.titleInterval)
    window.titleInterval = setInterval(_ => {
        let a = q(x)
        if( a ) {
            let t = a.textContent
            document.title = callback(t)
        }
    },dt)
}
window.watch = watch

v3

This also allows you to give an actual element object or a selector. It assumes that anything that isn't a string is an element to be watched.

window.q = (x,y=document) => y.querySelector(x)
window.qq = (x,y=document) => Array.from(y.querySelectorAll(x))

function watch(x, dt = 1000, callback = (x) => x) {
    if( window.titleInterval ) clearInterval(window.titleInterval)
    window.titleInterval = setInterval(_ => {
        let a = x
        if( typeof a === "string" ) {
            a = q(a)
        }
        if( a ) {
            let t = a.textContent
            document.title = callback(t)
        }
    },dt)
}
window.watch = watch

v4 by Gemini

Click, press Ctrl-B. Assign to window.watchFunc if you want to process the content of an element.

// ==UserScript==
// @name         Element Watcher & Title Updater
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Save clicked element and update window title periodically
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let lastClickedElement = null;

    // Track the last element clicked on the page
    document.addEventListener('click', (event) => {
        lastClickedElement = event.target;
    }, true);

    // Watch function definition
    window.watch = function(p = 1000) {
        // Clear existing interval loop if running
        if (window.watchLoop) {
            clearInterval(window.watchLoop);
            window.watchLoop = null;
        }

        if (!window.watchTarget) {
            console.warn('window.watchTarget is not set.');
            return;
        }

        // Start the update loop
        window.watchLoop = setInterval(() => {
            if (!window.watchTarget) return;

            let textResult = '';
            if (typeof window.watchFunc === 'function') {
                textResult = window.watchFunc(window.watchTarget);
            } else {
                textResult = window.watchTarget.textContent || '';
            }

            document.title = textResult;
        }, p);
    };

    // Keyboard Shortcuts Listener
    document.addEventListener('keydown', (event) => {
        // Ignore key combinations inside text inputs
        const activeTag = document.activeElement ? document.activeElement.tagName.toLowerCase() : '';
        if (['input', 'textarea'].includes(activeTag) || document.activeElement.isContentEditable) {
            return;
        }

        // Ctrl + Alt + B -> Shift target to parent node
        if (event.ctrlKey && event.altKey && event.key.toLowerCase() === 'b') {
            event.preventDefault();
            if (window.watchTarget && window.watchTarget.parentElement) {
                window.watchTarget = window.watchTarget.parentElement;
                console.log('watchTarget shifted to parent:', window.watchTarget);
            }
            return;
        }

        // Ctrl + B -> Set clicked element as target and call watch(1000)
        if (event.ctrlKey && !event.altKey && event.key.toLowerCase() === 'b') {
            event.preventDefault();
            if (lastClickedElement) {
                window.watchTarget = lastClickedElement;
                console.log('watchTarget set to:', window.watchTarget);

                // Prompt or default interval period in ms
                const period = 1000; 
                window.watch(period);
            } else {
                console.warn('No element clicked yet.');
            }
        }
    });
})();