有没有办法通过Javascript检测document.title
/head > title
的更改?我希望通过Google Chrome扩展内容脚本来检测这一点,因此我无法在目标页面的JS中连接代码,其中实际的标题更改是执行的。
我发现WebKitMutationObserver理论上应该能够检测head > title
的更改,但它并不适用于所有情况:
// set up an observer for the title element
var target = document.querySelector('title');
var observer = new WebKitMutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation);
});
});
var config = { attributes: true, childList: true, characterData: true };
observer.observe(target, config);
// this jQuery statement fires the observer as expected ...
$('head > title').text('foo');
// ... but this doesn't:
document.querySelector('title').innerText = 'cheezburger';
// ... and neither does this:
document.title = 'lorem ipsum';
有什么想法吗?