-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathUtil.js
159 lines (129 loc) · 4.6 KB
/
Util.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
"use strict";
/* jshint multistr: true */
/* jshint newcap: false */
/* Fonctions Utiles */
SK.Util = {
/* Effectue une requête sur l'api de JVC */
jvc: function(url, callback) {
GM_xmlhttpRequest({
url: "http://ws.jeuxvideo.com/" + url,
method: "GET",
headers: {
"Authorization": "Basic YXBwYW5kcjplMzIhY2Rm"
},
onload: function(response) {
callback($($.parseXML(SK.Util.sanitizeXML(response.responseText))));
}
});
},
/* Montre une fenêtre modale passée en paramètre */
showModal: function($modal) {
var $background = $("#modal-background");
$background.after($modal);
SK.Util.fetchStyle($modal);
$background.fadeIn();
$modal.addClass("active");
},
/* Cache une fenêtre modale si elle est ouverte */
hideModal: function() {
$("#modal-background").fadeOut();
$(".modal-box").on("transitionend webkitTransitionEnd", function() {
$(".modal-box").remove();
});
$(".modal-box").removeClass("active");
},
/* Ajoute un bouton au post à l'emplacement indiqué en paramètre dans les options (location: "top" (defaut), "right", ou "bottom") */
addButton: function($msg, buttonOptions) {
var location = buttonOptions.location || "top";
delete buttonOptions.location;
//On récupère ou on crée le conteneur des boutons
var $buttons = $msg.find(".buttons." + location);
if($buttons.length === 0) {
$buttons = $("<div>", {
class: "buttons " + location
});
//On place la box .buttons en fonction de l'emplacement
switch(location) {
case "top":
$msg.find(".pseudo > strong").first().after($buttons);
break;
case "bottom":
//Si le li .ancre n'existe pas, on la crée
var $ancre = $msg.find(".ancre").first();
if($ancre.length === 0) {
$ancre = $("<li>", {
class: "ancre"
});
$msg.find(".post").after($ancre);
}
$ancre.append($buttons);
break;
case "right":
$msg.find(".date").first().append($buttons);
break;
}
}
//On crée le bouton avec les options
var $button = new SK.Button(buttonOptions);
$button.hide();
//TODO: Faire un append avec un positionnement
$buttons.append($button.fadeIn());
},
addCss: function(css) {
if(typeof GM_addStyle === "function") {
GM_addStyle(css);
}
else {
$("head").append("<style type='text/css' >" + css + "</style>");
}
},
/* permet de supprimer les caractères spéciaux pour éviter les erreurs de parsing */
sanitizeXML: function(xmlString) {
var NOT_SAFE_IN_XML_1_0 = /[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm;
return xmlString.replace(NOT_SAFE_IN_XML_1_0, "");
},
/* Permet de précharger des images */
preload: function($img) {
var $preload = $("#preloaded-images");
if($preload.length === 0) {
$preload = $("<div>", {
id: "preloaded-images",
css: {
display: "none"
}
});
$("body").prepend($preload);
}
$preload.append($img);
},
//Force le navigateur à recalculer le CSS pour les animations
fetchStyle: function(element) {
if(typeof(window.getComputedStyle) == "function") {
if(element instanceof jQuery) {
element = element.get(0);
}
/* jshint -W030 */
window.getComputedStyle(element).left;
}
},
setValue: function(key, value) {
key = "SK." + key;
localStorage.setItem(key, JSON.stringify(value));
},
getValue: function(key) {
key = "SK." + key;
return JSON.parse(localStorage.getItem(key));
},
deleteValue: function(key) {
key = "SK." + key;
localStorage.removeItem(key);
},
/* Retourne nbspCount espaces insecables */
_: function(nbspCount) {
var nbspString = "";
for(var i = 0; i < nbspCount; i++) {
nbspString += String.fromCharCode(160);
}
return nbspString;
}
};