-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wiki[a]Peek.user.js
159 lines (131 loc) · 3.09 KB
/
Wiki[a]Peek.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// ==UserScript==
// @name Wiki[a]Peek
// @namespace http://your.homepage/
// @version 0.1
// @description (test) Preview Wikia articles as a tooltip
// @author C.Cajas
// @match http://*.wikia.com/wiki/*
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.js
// ==/UserScript==
$(document).ready(function()
{
$('#WikiaArticle a').hover(
function(e) {
hoverFunc(e, this);
},
function() {
hoverOut();
}
);
console.info("Wiki[a]Peek loaded");
});
///
/// Hover function, to load the article
///
function hoverFunc(e, link)
{
var content = loadArticle(link.href);
// Check if it's a redirect
if (link.className == "mw-redirect")
{
var redirContent = $.parseHTML(content);
var redirLink = $(redirContent).find('a');
content = loadArticle(redirLink[0].href);
}
// Show toolTip with article preview
if (content)
{
showToolTip(e, content);
}
}
///
/// Load article object from the article's name
///
function loadArticle(article)
{
// Article URL slug
article = article.substr(article.lastIndexOf("/") + 1);
// Check if this is an anchor link or empty
if (article.indexOf("#") != -1 || article.indexOf("?") != -1 || article === "")
return null;
// URL for MediaWiki API
var queryURL = ajaxFunc("/api/v1/Articles/Details/?titles="+ article +"&abstract=500&width=200&height=200");
var jsonObj = queryURL.items;
var basePath = queryURL.basepath;
var pageID;
// Get property key name of page ID
for(var key in jsonObj) {
if(jsonObj.hasOwnProperty(key)) {
pageID = jsonObj[key];
break;
}
}
console.dir(pageID.abstract);
return pageID;
}
///
/// Remove target element from string
///
(function($) {
$.strRemove = function(theTarget, theString) {
return $("<div/>").append(
$(theTarget, theString).remove().end()
).html();
};
})(jQuery);
///
/// AJAX shorthand function
///
function ajaxFunc(url)
{
var result;
$.ajax({
datatype: "json",
url: url,
async: false,
success: function(data){
result = data;
}
});
return result;
}
///
/// Show the ToolTip
///
function showToolTip(event, content)
{
console.log(content);
var bgcolor = $('.WikiaPage .WikiaPageBackground').css('background-color');
var thumbnail = (content.thumbnail) ? '<img align="right" src="'+ content.thumbnail +'"/>' : '';
$('body')
.prepend('<div class="WikiaPageBackground wpk-tooltip">'+
thumbnail + content.abstract +'</div>');
$('.wpk-tooltip')
.hide()
.fadeIn(500)
.css({
'position' : 'absolute',
'z-index' : '10',
'font-size' : '12px',
'line-height' : '1.4em',
'top' : (event.pageY + 10) + 'px',
'left' : '20%',
'width' : '400px',
'padding' : '5px',
'background-color': bgcolor,
'border' : '1px solid #aaa',
'-webkit-box-shadow' : '0px 3px 3px 2px rgba(0,0,0,0.25)',
'-moz-box-shadow' : '0px 3px 3px 2px rgba(0,0,0,0.25)',
'box-shadow' : '0px 3px 3px 2px rgba(0,0,0,0.25)'
});
$('.wpk-tooltip img').css({
'margin': '0px 4px', 'width': '96px', 'height': '96px'});
}
///
/// Hide the ToolTip
///
function hoverOut()
{
$('.wpk-tooltip').remove();
}