【问题标题】:Detect a parameter change in a foreign website检测国外网站的参数变化
【发布时间】:2023-04-02 12:20:01
【问题描述】:

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

<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。

【问题讨论】:

标签: javascript html firefox monitoring


【解决方案1】:

你可以使用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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 2021-04-17
    • 2015-03-12
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多