From f872e0dc9bb77c7e513bad5e31b805b1dbd2e68a Mon Sep 17 00:00:00 2001 From: hqweay Date: Mon, 27 Jun 2022 21:33:31 +0800 Subject: [PATCH] caching link content for link preview --- _includes/link-previews.html | 24 ++++++++++++++++++------ _notes/your-first-note.md | 2 ++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/_includes/link-previews.html b/_includes/link-previews.html index 49801cc..b9eb370 100644 --- a/_includes/link-previews.html +++ b/_includes/link-previews.html @@ -50,6 +50,8 @@ var tooltipWrapper = document.getElementById('tooltip-wrapper') var tooltipContent = document.getElementById('tooltip-content') + var linkHistories = {}; + function hideTooltip() { opacityTimeout = setTimeout(function() { tooltipWrapper.style.opacity = 0; @@ -66,20 +68,30 @@ var top = window.pageYOffset || document.documentElement.scrollTop if (event.target.host === window.location.host) { - iframe.src = event.target.href - iframe.onload = function() { - tooltipContentHtml = '' - tooltipContentHtml += '
' + iframe.contentWindow.document.querySelector('h1').innerHTML + '
' - tooltipContentHtml += iframe.contentWindow.document.querySelector('content').innerHTML + if (!linkHistories[event.target.href]) { + iframe.src = event.target.href + iframe.onload = function() { + tooltipContentHtml = '' + tooltipContentHtml += '
' + iframe.contentWindow.document.querySelector('h1').innerHTML + '
' + tooltipContentHtml += iframe.contentWindow.document.querySelector('content').innerHTML - tooltipContent.innerHTML = tooltipContentHtml + tooltipContent.innerHTML = tooltipContentHtml + linkHistories[event.target.href] = tooltipContentHtml + tooltipWrapper.style.display = 'block'; + setTimeout(function() { + tooltipWrapper.style.opacity = 1; + }, 1) + } + } else { + tooltipContent.innerHTML = linkHistories[event.target.href] tooltipWrapper.style.display = 'block'; setTimeout(function() { tooltipWrapper.style.opacity = 1; }, 1) } + tooltipWrapper.style.left = elem_props.left - (tooltipWrapper.offsetWidth / 2) + (elem_props.width / 2) + "px"; if ((window.innerHeight - elem_props.top) < (tooltipWrapper.offsetHeight)) { tooltipWrapper.style.top = elem_props.top + top - tooltipWrapper.offsetHeight - 10 + "px"; diff --git a/_notes/your-first-note.md b/_notes/your-first-note.md index e7ca5cb..44d89aa 100644 --- a/_notes/your-first-note.md +++ b/_notes/your-first-note.md @@ -58,6 +58,8 @@ Notice in the "Notes mentioning this note" section that there is another note li If you're on a device with mouse support, try hovering your mouse on internal links to preview the notes: [[a note about cats]]. +Links that have been previewed will be cached to avoid redundant requests. + ### Images and other Markdown goodies Finally, because you have the full power of Markdown in this template, you can use regular Markdown syntax for various formatting options.