Dup Ver Goto 📝

WatchElementInTabTitle

PT2/lang/js/user-scripts/examples does not exist
To
63 lines, 268 words, 1952 chars Page 'WatchElementInTabTitle' does not exist.

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