检测外部网站的参数更改

Detect a parameter change in a foreign website

本文关键字:参数 外部 网站 检测      更新时间:2023-09-26

假设我们有一个只读网站,包含一个按钮

<div class="with-a-class">
    <div property="value">
        <table>
            <tbody><tr>
                <td>
                    <button>
                        <img src="static/first.gif">
                    </button>
                </td>
                <!-- Some more table cells-->
            </tr>
        </tbody></table>
    </div>
</div>

我想检测任何变化,如果按钮的src属性和打印它改变时:(伪代码)

onSrcChanged: console.log(src)

我用的是Firefox

你可以使用MutationObserver来检测src中的变化

// This is your image
var target = document.querySelector('#myImage');
// This is the observer
var observer = new MutationObserver(function(mutations)
{
    // Loop all changes
    mutations.forEach(function(mutation)
    {
        // Only if src was changed
        if(mutation.attributeName=='src')
        {
            // Print the new value
            console.log(mutation.target.src);
        }
    });    
});
// Read only changes in the attributes
var config = { attributes: true, childList: false, characterData: false };
// Initialize the Observer
observer.observe(target, config);