Exemplo n.º 1
0
        public SpecificChildListChangedObserver()
        {
            _observer = new MutationObserver((changes, _) => {
                Logger.Debug(GetType(), "[SpecificChildListChangedObserver] dom nodes mutation detected - got {0} items", changes.Length);
                var triggered = 0;
                changes.ForEach(change =>
                                change.AddedNodes
                                .Where(x => x.NodeType == NodeType.Element)
                                .Select(x => x.AsElementOrNull())
                                .ForEach(added =>
                                         triggered += added.TraverseAll(el => {
                    if (!_onAddedListeners.ContainsKey(el))
                    {
                        return(false);
                    }
                    _onAddedListeners[el].ForEach(x => x());
                    return(true);
                }))
                                );
                Logger.Debug(GetType(), "triggered {0} listeners", triggered);
            });

            _observer.Observe(Document.Body, new MutationObserverInit {
                Subtree   = true,
                ChildList = true
            });
        }
Exemplo n.º 2
0
        public void TestNewlyAttachedElements()
        {
            Records = null;

            var done = Assert.Async();

            var root = HtmlHelper.FixtureElement;

            //setup observer
            var observer = new MutationObserver((changes, _) =>
            {
                if (changes.Length > 0)
                {
                    Records = changes;
                }
            });

            observer.Observe(root, new MutationObserverInit
            {
                Subtree   = true,
                ChildList = true
            });

            Task task = new Task(() =>
            {
                // mutate DOM
                // observer will be invoked asynchronously
                root.AppendChild(new HTMLSpanElement());
            });

            var task1 = task.ContinueWith(x =>
            {
                Task.Delay(10);
            });

            task1.ContinueWith(x =>
            {
                try
                {
                    AssertRecords(this.Records);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

                observer.Disconnect();

                done();
            });

            task.Start();
        }
        public static void Run()
        {
            // ExStart:MutationObserver
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_Data();

            // The MutationObserver interface provides the ability to watch for changes being made to the DOM tree.

            // Create an empty document
            using (var document = new HTMLDocument())
            {
                // Create a WaitHandle for purpose described below
                var @event = new ManualResetEvent(false);

                // Create an observer instance
                var observer = new MutationObserver((mutations, mutationObserver) =>
                {
                    var mutation = mutations[0];
                    Console.WriteLine(mutation.AddedNodes[0]);
                    @event.Set();
                });

                // Options for the observer (which mutations to observe)
                var config = new MutationObserverInit
                {
                    ChildList = true,
                    Subtree   = true
                };

                // Start observing the target node
                observer.Observe(document.DocumentElement, config);

                // An example of user modifications
                var p = document.CreateElement("p");
                document.DocumentElement.AppendChild(p);

                // Since, mutations are working in the async mode you should wait a bit. We use WaitHandle for this purpose.
                @event.WaitOne();


                // Later, you can stop observing
                observer.Disconnect();
            }
            // ExEnd:MutationObserver
        }
        public GeneralChildListChangedObserver()
        {
            _observer = new MutationObserver((changes, _) => {
                Logger.Debug(GetType(), "[GeneralChildListChangedObserver] dom nodes mutation detected - got {0} items", changes.Length);
                var triggered = 0;
                changes.ForEach(change =>
                                change.AddedNodes
                                .Where(x => x.NodeType == NodeType.Element)
                                .Select(x => x.AsElementOrNull())
                                .ForEach(added =>
                                         triggered += added.TraverseAll(el => {
                    _onAddedListeners.ForEach(listener => listener(el));
                    return(true);
                }))
                                );
                Logger.Debug(GetType(), "triggered {0} listeners {1} times total", _onAddedListeners.Count, triggered);
            });

            _observer.Observe(Document.Body, new MutationObserverInit {
                Subtree   = true,
                ChildList = true
            });
        }
Exemplo n.º 5
0
        public GeneralAttrChangedObserver()
        {
            _observer = new MutationObserver((changes, _) => {
                Logger.Debug(GetType(), "[GeneralAttrChangedObserver] dom attr mutation detected - got {0} items", changes.Length);
                var triggered = 0;
                changes.ForEach(change => {
                    var attrName = change.AttributeName.ToLower();
                    var el       = change.Target.AsElementOrNull();
                    Logger.Debug(GetType(), "changed {0} to '{1}' on {2}", attrName, el?.GetAttribute(attrName), el);
                    if (!_listeners.ContainsKey(attrName) || el == null)
                    {
                        return;
                    }
                    triggered++;
                    _listeners[attrName](el);
                });
                Logger.Debug(GetType(), "triggered {0} listeners", triggered);
            });

            _observer.Observe(Document.Body, new MutationObserverInit {
                Attributes = true,
                Subtree    = true
            });
        }