Skip to content

Instantly share code, notes, and snippets.

@kerminz
Last active May 8, 2019 09:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerminz/c1148b01eea66f2fe7540ece05fca5a5 to your computer and use it in GitHub Desktop.
Save kerminz/c1148b01eea66f2fe7540ece05fca5a5 to your computer and use it in GitHub Desktop.
Testen auf Single-Page-Application mit DOM Observer (YouTube)
var event = new Event('abtest');
// Listen for the event.
window.addEventListener('abtest', function (e) {
console.log("AB Test running...");
console.log("Sign In Page");
}, false);
function waitForElement(selector, target, callback) {
// observer target node
var target = document.querySelector(target);
// create new observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var addedNodes = mutation.addedNodes;
for (var i=0; i < addedNodes.length; i++) {
var node = addedNodes[i];
if (node.matches && node.matches(selector)) {
callback();
//observer.disconnect();
}
}
});
});
// observer config
var config = { childList: true, subtree: true };
// start with observervation
if (target) {
observer.observe(target, config);
}
}
waitForElement("main.is-narrow", "body", function() {
window.dispatchEvent(event);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment