Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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