mutation-observers

Why does MutationObserver fire twice for childList but never for characterData?

喜夏-厌秋 提交于 2020-07-06 10:43:21
问题 I have a simple MutationObserver setup as a test. The HTML has a span whose text content gets updated once per second (and a div for messages): <span class="tester"></span> <div id="msg"></div> The MutationObserver is set to watch .tester and writes text to the #msg div when it observes a change. Meanwhile, a setInterval() runs once/second to change the text in .tester : var target = document.querySelector('.tester'); var observer = new MutationObserver(function(mutations) { mutations.forEach

Why does MutationObserver fire twice for childList but never for characterData?

大城市里の小女人 提交于 2020-07-06 10:43:09
问题 I have a simple MutationObserver setup as a test. The HTML has a span whose text content gets updated once per second (and a div for messages): <span class="tester"></span> <div id="msg"></div> The MutationObserver is set to watch .tester and writes text to the #msg div when it observes a change. Meanwhile, a setInterval() runs once/second to change the text in .tester : var target = document.querySelector('.tester'); var observer = new MutationObserver(function(mutations) { mutations.forEach

Why does MutationObserver fire twice for childList but never for characterData?

心不动则不痛 提交于 2020-07-06 10:42:05
问题 I have a simple MutationObserver setup as a test. The HTML has a span whose text content gets updated once per second (and a div for messages): <span class="tester"></span> <div id="msg"></div> The MutationObserver is set to watch .tester and writes text to the #msg div when it observes a change. Meanwhile, a setInterval() runs once/second to change the text in .tester : var target = document.querySelector('.tester'); var observer = new MutationObserver(function(mutations) { mutations.forEach

$(document).on() in plain JavaScript?

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-24 22:49:13
问题 In jQuery there is .on() which can be used as: $(document).on('click', '.foo', function() { /* ... */ }); This listens for click events on all DOM elements with the class .foo . However, this also listens for any eventual elements added to the DOM later, so it is not equal to: var elements = document.getElementsByClassName('foo'); for (var element in elements) { element.addEventListener('click', function() { /* ... */ }); } How do I do this in plain JavaScript? Am I supposed to use a

Audio won't be muted with JavaScript - removing audio tags with mutationobserver needed

耗尽温柔 提交于 2020-06-18 14:41:53
问题 I tried to use the following userscript to remove all audio from a certain website: // ==UserScript== // @name addicto // @namespace nms // @include http://* // @include https://* // @version 1 // @grant none // ==/UserScript== addEventListener('DOMContentLoaded', ()=>{ let sites = ['mako.co.il']; let href = window.location.href; for (let i = 0; i < sites.length; i++) { if (href.includes(sites[i])) { Array.prototype.slice.call(document.querySelectorAll('audio')).forEach((audio)=>{ audio.muted

Audio won't be muted with JavaScript - removing audio tags with mutationobserver needed

▼魔方 西西 提交于 2020-06-18 14:36:48
问题 I tried to use the following userscript to remove all audio from a certain website: // ==UserScript== // @name addicto // @namespace nms // @include http://* // @include https://* // @version 1 // @grant none // ==/UserScript== addEventListener('DOMContentLoaded', ()=>{ let sites = ['mako.co.il']; let href = window.location.href; for (let i = 0; i < sites.length; i++) { if (href.includes(sites[i])) { Array.prototype.slice.call(document.querySelectorAll('audio')).forEach((audio)=>{ audio.muted

Detect when a node is deleted (or removed from the DOM because a parent was)

北战南征 提交于 2020-06-12 04:52:26
问题 I want to detect when a node (nodeX, say) is no longer available, either because it was deleted or because its parent (or its parents parent) was deleted. So far, all I can think of is to use Mutation Observer to see any deletions on the page, and check if the deleted nodes was nodeX or had nodeX for a descendant. Is there an easier way? Please note: as far as I understand, the linked question (that this question "is a duplicate of") asks "how can I detect a [direct] deletion of a node". Mine

Why can't observe document.write on document.body using MutationObserver API?

泄露秘密 提交于 2020-01-24 09:46:25
问题 I failed to observe document.write on document.body . Here is code: <script> var observeDOM = (function(){ var MutationObserver = window.MutationObserver || window.WebKitMutationObserver, eventListenerSupported = window.addEventListener; return function(obj, callback){ if( MutationObserver ){ // define a new observer var obs = new MutationObserver(function(mutations, observer){ if( mutations[0].addedNodes.length || mutations[0].removedNodes.length ) callback(); }); // have the observer

How do I use mutationobserver instead of mutation events?

大兔子大兔子 提交于 2020-01-04 03:38:54
问题 I am trying to create my first streamgraph with D3.js. I am starting with a working example which incorporates a tooltip from code posted on-line, http://bl.ocks.org/WillTurman/4631136: When I copy the index.html and data.csv files to my PC (Windows 7), I am able to see the streamgraph in a browser (Firefox). However, I get a JS warning, " Use of Mutation Events is deprecated. Use MutationObserver instead. " What lines of code correspond to the Mutation Event? And how should I edit them to

How to use MutationObserver?

核能气质少年 提交于 2019-12-31 21:23:49
问题 I recently came across this awesome MutationObserver feature which sort of keep tracks of the changes on any dom element. I used the code that was shown on the mozilla developer network, but can't seem to make it run. This is the code I used (link): // create an observer instance var target = document.querySelector('#something'); console.log(target); var observer = new WebKitMutationObserver(function(mutations) { mutations.forEach(function(mutation) { console.log("Success"); //$('#log').text(