let title = document.querySelector('title');
let page_state = JSON.parse(title.innerHTML);
window.scroll(0, page_state.scroll_top);
window.addEventListener('scroll', function() {
page_state.scroll_top = window.pageYOffset;
title.innerHTML = JSON.stringify(page_state);
});
document.querySelectorAll('img').forEach(function(img) {
const width = page_state.image_widths[img.src];
const height = page_state.image_heights[img.src];
let style = "";
if (width) { style = `${style} width: ${width}px;`; }
if (height) { style = `${style} height: ${height}px;`; }
img.style = style;
img.onload = function() {
img.style = "";
page_state.image_heights[this.src] = this.height;
page_state.image_widths[this.src] = this.width;
title.innerHTML = JSON.stringify(page_state);
};
});
document.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(function(heading) {
if (!heading.id) {
let content = heading.innerHTML.trim().toLowerCase();
let slug = content.replace(/[^\w\d]+/g, '-');
heading.id = slug;
}
});
let linkPreview = document.querySelector('#link-preview');
let rootUrl = window.location.href;
document.querySelectorAll('a').forEach(function(link) {
let url = link.href;
let description;
if (url.startsWith(rootUrl)) {
description = `<strong>Jump</strong>: ${url.replace(rootUrl, '')}`
} else {
description = `<strong>Copy</strong>: ${url}`
link.addEventListener('click', function(e) {
e.preventDefault();
let tempInput = document.createElement('input');
tempInput.setAttribute('type', 'text');
tempInput.setAttribute('value', url);
document.body.insertBefore(tempInput, linkPreview);
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
linkPreview.innerHTML = "<strong>Copied to clipboard!</strong>";
});
}
link.addEventListener('mouseenter', function() {
linkPreview.innerHTML = description;
linkPreview.classList.remove('hidden');
linkPreview.classList.add('visible');
});
link.addEventListener('mouseleave', function() {
linkPreview.classList.remove('visible');
linkPreview.classList.add('hidden');
});
});