0

特定のイベントが発生したときに何かを実行するブラウザー拡張機能を作成したいのですが、そのような API (Firefox または Chrome) が既に存在するかどうか疑問に思っています。

私は主に DOM の変更とウィンドウの変更に興味があります。

この例を考えてみましょう:

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

したがって、この単純な例では、document.cookie の変更と document.write メソッドの呼び出しという 2 つのイベントに関心があります。これらのことが発生したときに、拡張機能で通知を受け取りたいです。これらのステートメントが利用可能な JavaScript コンテキストに存在するかどうかではなく、実際に実行されているかどうか。

Firefox 拡張機能と Chrome 拡張機能で API を検索しようとしましたが、役に立つものは見つかりませんでした。

ありがとうございました。

UPDATE : 私が興味を持っている他のメソッドは、eval()メソッドの呼び出しとlocalStorageの変更です

4

2 に答える 2

1

現在の Firefox (およびwebkitプレフィックス付きの Chrome) は をサポートしていますMutation Observers。それを使用して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();
于 2013-01-10T20:09:32.743 に答える
0

タブ/ウィンドウで開いているドキュメントへの変更を監視する場合は、MutationObservers がその方法です。純粋な JS で呼び出されたメソッドを監視する場合は、Spidermonkey の JSEngine API、特にプロファイリングとトレースに関する API を調べる必要があります。

https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_User_Guide#Tracing_and_Profiling

于 2013-01-11T03:33:12.427 に答える