Skip to content

Instantly share code, notes, and snippets.

@jdillard
Last active April 13, 2022 10:36
Show Gist options
  • Save jdillard/87491a7ccea66e6c486fdd4144525464 to your computer and use it in GitHub Desktop.
Save jdillard/87491a7ccea66e6c486fdd4144525464 to your computer and use it in GitHub Desktop.
Wikipedia article preview on link hover
<html>
<head>
<script src="https://unpkg.com/tippy.js@2.5.3/dist/tippy.all.min.js"></script>
</head>
<body>
<div id="wiki-template" style="display: none;">
Loading wikipedia summary...
</div>
</body>
</html>
const template = document.querySelector('#wiki-template')
const initialText = template.textContent
const tip = tippy('a[href*="wikipedia.org"]', {
animation: 'shift-toward',
arrow: true,
maxWidth: '300px',
theme: 'light',
html: '#wiki-template',
onShow(instance) {
const wikiPage = instance.reference.href.split("wikipedia.org/wiki")[1].substring(1);
// `this` inside callbacks refers to the popper element
const content = this.querySelector('.tippy-content')
if (tip.loading || content.innerHTML !== initialText) return
tip.loading = true
fetch('https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&origin=*&redirects=true&exintro=&titles='+wikiPage).then((resp) => resp.json()).then(data => {
const pageKey = Object.keys(data.query.pages)[0];
const firstPara = $.parseHTML(data.query.pages[pageKey].extract)[0].innerHTML
if(firstPara.split(" ").length > 48) {
content.innerHTML = '<div style="padding:5px">' + firstPara.split(" ").splice(0,48).join(" ").replace(/,\s*$/, "") + '...</div>'
} else {
content.innerHTML = '<div style="padding:5px">' + firstPara + '</div>'
}
tip.loading = false
}).catch(e => {
content.innerHTML = 'Loading failed'
tip.loading = false
})
},
onHidden() {
const content = this.querySelector('.tippy-content')
content.innerHTML = initialText
},
// prevent tooltip from displaying over button
popperOptions: {
modifiers: {
preventOverflow: {
enabled: false
},
hide: {
enabled: false
}
}
}
})
@AKiumarsi
Copy link

Hi, why dos not work for me?
a page html without anythings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment