当标题被另一个脚本更改时,更改文档标题

时间:2014-10-03 12:56:48

标签: javascript-events

奇怪的问题是,我有一个Chrome扩展程序,可以重命名文档标题。但它不适用于实时更新标题的网站,如Facebook,AngularJS SPA ......

所以我需要检测此更改以覆盖它。问题,因为你期望它无限循环......

var target = document.querySelector('head > title');

var observer = new window.MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
        console.log(mutation.target.textContent);

        // here change the title with a new value
        document.title = 'something';
    });
});

observer.observe(target, {
    subtree: true,
    characterData: true,
    childList: true
});

然后,我怎么能告诉我的事件“如果我是那个解雇你的人,什么都不做”?

糟糕的方法是每隔X秒更新标题......

1 个答案:

答案 0 :(得分:1)

添加自己的属性以在更改标题时进行标记:

if (typeof(document.titleChanged) == "undefined") {
    console.log(mutation.target.textContent);

    // here change the title with a new value
    document.titleChanged = true;
    document.title = 'something';
} else {
    delete document.titleChanged;
}