Skip to content

Instantly share code, notes, and snippets.

@nkgokul
Last active May 7, 2019 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkgokul/4f8e9f67eb18d3102d8c12e385d25c77 to your computer and use it in GitHub Desktop.
Save nkgokul/4f8e9f67eb18d3102d8c12e385d25c77 to your computer and use it in GitHub Desktop.
Intersection Observer API Sample Code
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