Skip to content

Commit a5e613f

Browse files
author
Dara Javaherian
committed
Get working a lot better
1 parent f13a477 commit a5e613f

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

inject.js

+65-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var wikiResponse = false;
2+
var shadow = null;
23

34
$(document).ready( function() {
45
peekElem =
@@ -27,7 +28,7 @@ function setupPeek(elem) {
2728

2829
var timeout = setTimeout(function() {
2930
showPeek(elem);
30-
}, 1000);
31+
}, 500);
3132

3233
$('a').mouseout(function() {
3334
clearTimeout(timeout);
@@ -39,18 +40,73 @@ function setupPeek(elem) {
3940
function showPeek(elem) {
4041
if (wikiResponse === false)
4142
return false;
42-
elem.addClass('wikitip');
43-
elem.append('<span class="wikiinfo"><div class="wikititle"></div><div class="wikitext"></div></span>');
4443

45-
46-
47-
$('.wikiinfo .wikititle').text(wikiResponse[0]);
48-
$('.wikiinfo .wikitext').text(wikiResponse[1]);
44+
elem.append('<span class="wikitip"></span>')
45+
// Insert node after the body so that it will always be on top
46+
$('body').after('<div id="wikipeek-host"></div>');
47+
48+
// Using DOM shadowing cause I'm just fucking 1337 like that...
49+
// ...also this is the way Google made their dictionary extension
50+
var shadow = $('#wikipeek-host')[0].createShadowRoot();
51+
shadow.innerHTML = `
52+
<style>
53+
.wikiinfo{
54+
background: rgba(245, 245, 255, 0.96);
55+
border-radius: 5px;
56+
border: 1px solid #ccc;
57+
bottom: 26px;
58+
color: black;
59+
content: attr(title);
60+
left: 20%;
61+
padding: 18px;
62+
position: absolute;
63+
width: 480px;
64+
}
65+
66+
.wikiinfo .wikititle{
67+
font-family: "Linux Libertine",Georgia,Times,serif;
68+
line-height: 1.3;
69+
margin-bottom: 14px;
70+
padding: 0;
71+
padding-bottom: 7px;
72+
width: 100%;
73+
border-bottom: 1px solid #aaa;
74+
font-size: 22px;
75+
}
76+
77+
.wikiinfo .wikitext{
78+
max-height: 140px;
79+
font-size: 12px;
80+
overflow: hidden;
81+
}
82+
</style>
83+
<span class="wikiinfo">
84+
<div class="wikititle">${wikiResponse[0]}</div>
85+
<div class="wikitext">${wikiResponse[1]}</div>
86+
</span>`;
87+
88+
$('#wikipeek-host').css({
89+
position: "absolute",
90+
marginLeft: 0, marginTop: 0,
91+
top: elem.offset().top + 20, left: elem.offset().left
92+
});
93+
94+
// Sucks, but it seems to be difficult to find the width of shadow elements
95+
// This is the width of the tooltip + the padding
96+
var tipWidth = 518;
97+
var offscreenRight = $('#wikipeek-host').offset().left + tipWidth;
98+
var screenWidth = $(window).width();
99+
console.log($('#wikipeek-host').outerWidth());
100+
console.log(offscreenRight);
101+
console.log(screenWidth);
102+
if (offscreenRight > screenWidth) {
103+
$('#wikipeek-host').css('left', $('#wikipeek-host').offset().left - (offscreenRight - screenWidth));
104+
}
49105
}
50106

51-
function hidePeek(elem) {return
107+
function hidePeek(elem) {
52108
elem.removeClass('wikitip');
53-
elem.find('.wikiinfo').remove();
109+
$('#wikipeek-host').remove();
54110
}
55111

56112
function fetchPeek(link) {

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"version": "0.1",
77
"author": "Dara Javaherian",
88

9-
"permissions": ["tabs", "*://*.wikipedia.org/wiki/*"],
9+
"permissions": ["*://*.wikipedia.org/wiki/*"],
1010

1111
"icons": {
1212
"128": "wiki-128.png"

0 commit comments

Comments
 (0)