Search code examples
javascriptuserscripts

Replace specific colours in a webpage with tampermonkey


I am trying to replace all colours on a webpage with my own pre-defined colours. I want to replace all mentions of "rgba(29,161,242,1.00)" with "rgb(255 0 0)" using tampermonkey but I am unsure of how to go about doing it.


Solution

  • If it's a multi-page app with limited JavaScript functionality, this should work:

    const oldColorNormalized = 'rgb(29, 161, 242)'
    const newColor = 'rgb(255 0 0)'
    
    document.querySelectorAll('*').forEach(el => {
        if (getComputedStyle(el).color === oldColorNormalized) {
            el.style.color = newColor
        }
    })
    

    If it's a single-page app or has lots of JS-rendered content, you may need something more complex with MutationObservers.