-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMutationObserver.java
More file actions
54 lines (44 loc) · 1.64 KB
/
MutationObserver.java
File metadata and controls
54 lines (44 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package snap.webapi;
/**
* This class is a wrapper for Web API MutationObserver (https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
*/
public class MutationObserver extends JSProxy {
// The callback
private Callback _callback;
// The mutation types that can be observed
public enum Option { attributes, childList, subtree, attributeFilter, attributeOldValue, characterData, characterDataOldValue }
/**
* Constructor.
*/
public MutationObserver(Callback aCallback)
{
super(WebEnv.get().newMutationObserver(aCallback));
_callback = aCallback;
}
/**
* Returns the callback.
*/
public Callback getCallback() { return _callback; }
/**
* Configures the MutationObserver to begin receiving notifications through its callback function when DOM changes matching the given options occur.
*/
public void observe(Node targetNode, Option... theOptions)
{
// Convert options to dictionary object
Object optionsJS = WebEnv.get().newObject();
for (Option option : theOptions)
WebEnv.get().setMemberBoolean(optionsJS, option.name(), true);
WebEnv.get().addMutationObserver(this, targetNode, optionsJS);
}
/**
* Stops the MutationObserver instance from receiving further notifications until and unless observe() is called again.
*/
public void disconnect() { call("disconnect"); }
/**
* An interface for a MutationObserver callback.
*/
public interface Callback {
// Called when mutation is observed
void handleMutations(MutationRecord[] mutationRecords);
}
}