2018-12-05 23:21:45 +05:30
|
|
|
import { visitUrl } from './url_utility';
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
/**
|
|
|
|
* Helper function that finds the href of the fiven selector and updates the location.
|
|
|
|
*
|
|
|
|
* @param {String} selector
|
|
|
|
*/
|
2018-10-15 14:42:47 +05:30
|
|
|
export default function findAndFollowLink(selector) {
|
|
|
|
const element = document.querySelector(selector);
|
|
|
|
const link = element && element.getAttribute('href');
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
if (link) {
|
2018-10-15 14:42:47 +05:30
|
|
|
visitUrl(link);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2018-10-15 14:42:47 +05:30
|
|
|
}
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
export function prefetchDocument(url) {
|
|
|
|
const newPrefetchLink = document.createElement('link');
|
|
|
|
newPrefetchLink.rel = 'prefetch';
|
|
|
|
newPrefetchLink.href = url;
|
|
|
|
newPrefetchLink.setAttribute('as', 'document');
|
|
|
|
document.head.appendChild(newPrefetchLink);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initPrefetchLinks(selector) {
|
|
|
|
document.querySelectorAll(selector).forEach((el) => {
|
|
|
|
let mouseOverTimer;
|
|
|
|
|
|
|
|
const mouseOutHandler = () => {
|
|
|
|
if (mouseOverTimer) {
|
|
|
|
clearTimeout(mouseOverTimer);
|
|
|
|
mouseOverTimer = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const mouseOverHandler = () => {
|
|
|
|
el.addEventListener('mouseout', mouseOutHandler, { once: true, passive: true });
|
|
|
|
|
|
|
|
mouseOverTimer = setTimeout(() => {
|
|
|
|
if (el.href) prefetchDocument(el.href);
|
|
|
|
|
|
|
|
// Only execute once
|
|
|
|
el.removeEventListener('mouseover', mouseOverHandler, true);
|
|
|
|
|
|
|
|
mouseOverTimer = undefined;
|
|
|
|
}, 100);
|
|
|
|
};
|
|
|
|
|
|
|
|
el.addEventListener('mouseover', mouseOverHandler, {
|
|
|
|
capture: true,
|
|
|
|
passive: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|