Skip to content

Instantly share code, notes, and snippets.

@mi-lee
Created June 2, 2018 22:06
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 mi-lee/df5a1f7a5799cb3d18bd73bfc2d018f0 to your computer and use it in GitHub Desktop.
Save mi-lee/df5a1f7a5799cb3d18bd73bfc2d018f0 to your computer and use it in GitHub Desktop.
Popup of Wikipedia Summary based on highlighted term
var count = 0
$(document.body).keypress(function(event) {
if (event.which == 119) {
console.log("count =", count);
count++
if (count % 2 == 0) {
$(".pug").remove();
return;
}
prev = 119;
event.preventDefault();
if (window.getSelection) {
text = window.getSelection().toString();
if (!text) {
return;
}
$.get({
url: "https://en.wikipedia.org/api/rest_v1/page/summary/" + text,
complete: function(data) {
data = data.responseJSON
s = window.getSelection();
oRange = s.getRangeAt(0); // get the text range
oRect = oRange.getBoundingClientRect();
$(document.body).append(
'<div class="pug" role="tooltip" aria-hidden="" style="top: ' + oRect.top + 'px; left: ' + (oRect.left/2) + 'px; display: block; position: fixed; background: white; padding: 1%; width: 30%; border: 1px solid #ddd;">' +
'<div class="pug-container">' +
'<a dir="ltr" lang="en" class="pug-extract" href="' + data.content_urls.desktop.page + '"' +
'style="color:#555; text-decoration: none !important;">' +
(data.thumbnail ? '<img src="' + data.thumbnail.source + '" style="width: 70%;"/>' : '') +
data.extract_html + '</a>' +
'</div></div>')
}
})
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment