Javascript 通知/挂起文档/窗口更改
我想写一个浏览器扩展,当某些事件发生时,它会做一些事情,我想知道是否已经有这样的API(Firefox或Chrome) 我主要对DOM更改和窗口更改感兴趣Javascript 通知/挂起文档/窗口更改,javascript,google-chrome-extension,firefox-addon,google-chrome-devtools,firefox-addon-sdk,Javascript,Google Chrome Extension,Firefox Addon,Google Chrome Devtools,Firefox Addon Sdk,我想写一个浏览器扩展,当某些事件发生时,它会做一些事情,我想知道是否已经有这样的API(Firefox或Chrome) 我主要对DOM更改和窗口更改感兴趣 让我们考虑这个例子: <html> <head> <script type="text/javascript"> function addContentToDocument(content){ document.cookie = "debug=true"; //<--- Not
让我们考虑这个例子:
<html>
<head>
<script type="text/javascript">
function addContentToDocument(content){
document.cookie = "debug=true"; //<--- Notify
if(content != null){
document.write(content); //<--- Notify
};
}
</script>
</head>
<body onload="addContentToDocument('Say cheese')">
<h3>My Content</h3>
</body>
</html>
函数addContentToDocument(内容){
document.cookie=“debug=true”;//当前的Firefox(和带有webkit
前缀的Chrome)支持。我认为您不能用它捕获cookie更改,但您肯定可以捕获对DOM所做的更改(无论是否使用document.write
)
Mozilla文档中的示例:
// select the target node
var target = document.querySelector('#some-id');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation.type);
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true }
// pass in the target node, as well as the observer options
observer.observe(target, config);
// later, you can stop observing
observer.disconnect();
如果要监视在选项卡/窗口中打开的文档的更改,则可以使用MutationObserver。如果要监视纯JS中调用的方法,则需要查看Spidermonkey的JSEngine API,特别是分析和跟踪方面的API:
如何调用修改localStorage?或者调用eval()方法?我实际上从未使用过这些方法,我只知道它们存在。但我相信它们只适用于DOM突变。对于eval
,您可以使用类似var originalEval=eval;eval=function(){}的内容覆盖它
。您也可以使用localStorage方法执行类似的操作。对于localStorage,您可以只使用向window对象发出的事件。类似于:window.addEventListener(“存储”,function(){console.log('storage');},false);
的操作即可。