Last active
May 7, 2019 14:36
-
-
Save nkgokul/4f8e9f67eb18d3102d8c12e385d25c77 to your computer and use it in GitHub Desktop.
Intersection Observer API Sample Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chrome.extension.sendMessage({}, function (response) { | |
var readyStateCheckInterval = setInterval(function () { | |
if (document.readyState === "complete") { | |
clearInterval(readyStateCheckInterval); | |
var options = { | |
root: document.querySelector('null'), | |
rootMargin: '0% 0% 75% 0%', | |
threshold: 1.0 | |
} | |
var callback = function (entries, observer) { | |
entries.forEach(entry => { | |
//console.log(entry.target.id); | |
if (entry.target.id) { | |
window.history.replaceState({}, "", "#" + entry.target.id); | |
} | |
}); | |
}; | |
var observer = new IntersectionObserver(callback, options); | |
var targets = document.querySelectorAll('p, div, h1, h2, h3, h4, h5, h6, span'); | |
targets.forEach(function (target) { | |
observer.observe(target); | |
}); | |
} | |
}, 10); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment