-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
344 lines (283 loc) · 9.92 KB
/
script.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
jQuery(document).ready(function($) {
var currentA = null;
var mobile = () => {return $(window).width() > 600 ? false : true};
function _f(s) {
return blpwp_params['labels'][s]
}
jQuery("body").append( "<style>" +
".mblpwp .blpwp_spot b {background:" + blpwp_params["options"]["color"] +"}" +
".mblpwp .blpwp_spot b i {color:" + blpwp_params["options"]["fgcolor"] +"}" +
".mblpwp .blpwp_wrap {background:" + blpwp_params["options"]["color0"] +"}" +
".mblpwp .blpwp_wrap {color:" + blpwp_params["options"]["fgcolor0"] +"}</style>");
function checkPic() {
span = $("#blpwp .blpwp_pics");
if (typeof(span.data("images"))!== "undefined" && span.data("images")!="") {
arPics = span.data("images").split("|");
var img = new Image();
img.onload = function(){
if(img.width * img.height > 2500) {
span.replaceWith("<img src=\"" + arPics[0] + "\" onerror=\"jQuery(this).hide();\">");
if (img.width > img.height) {
$("#blpwp").addClass("horizontal").removeClass("vertical");
} else {
$("#blpwp").addClass("vertical").removeClass("horizontal");
}
} else {
let images = span.data("images").replace(arPics[0],"");
images = images.replace(/^\|/, '');
span.data("images", images);
checkPic(); // recursive call
}
}
img.src = arPics[0];
} else {
$("#blpwp>div").addClass("nopic");
}
}
var mTimer = false;
var triggerClick = (a) => {
$.ajax({
url: blpwp_params['url'],
type :'POST',
data: {
"url": convertToAbsoluteURL( a.attr("href") ),
"idpost": blpwp_params['idpost'],
"action" : 'blpwpclick',
"rel" : a.attr("rel")
},
success: function(html){
if( a.data("preventClick") == 0) {
// normal links
if(blpwp_params['options'].blank == 'on') {
window.open( a.attr("href") );
} else {
document.location = a.attr("href");
}
} else {
closePopup();
}
},
});
}
function addOverlayer(){
$("body").data("css-overflow", $("body").css("overflow"));
$('body').append("<div id='underblpwp' class='" + blpwp_params['options'].style + "'></div>");
$('body,html').css("overflow","hidden");
mTimer = setInterval(function(){
if($('#blpwp').css("opacity") <= 0.05 && $('#blpwp').hasClass("fading")) {
$('#underblpwp').remove();$('#blpwp').remove();
clearInterval(mTimer);
}
// click on dark layer close pop up
$('#underblpwp').on("click",function(e){
e.preventDefault();
e.stopPropagation();
if($('#blpwp').css("opacity") >=.95 && !$('#blpwp').hasClass("fading")) {
$('#blpwp').addClass("fading");
$('body,html').css("overflow",$("body").data("css-overflow"));
}
});
},500);
};
function closePopup(){
$('#blpwp').addClass("fading");
if(mobile()) $('body,html').css("overflow",$("body").data("css-overflow"));
}
function showThisPop(a) {
$('#blpwp').html(a.data("html")).attr("class","mblpwp loaded " + blpwp_params['options'].style + " " + (mobile() ? "mob" :"") );
checkPic();
if(mobile()){
$('#underblpwp').addClass("loaded");
$('#blpwp').on("click",function(e){
e.preventDefault();
e.stopPropagation();
if(!$('#blpwp').hasClass("fading")) {
triggerClick(a)
}
});
}
}
function convertToAbsoluteURL(relativeURL) {
// Check if a <base> tag already exists
if(relativeURL) {
const existingBase = document.querySelector('base');
let base;
if (existingBase) {
base = existingBase;
} else {
base = document.createElement('base');
document.head.appendChild(base);
}
base.href = window.location.href;
const absoluteURL = new URL(relativeURL, base.href);
return absoluteURL.href;
} else {
return "";
}
}
function setupPopUp(a, e) {
currentA = convertToAbsoluteURL( e.target.getAttribute("href") );
let h = convertToAbsoluteURL( a.attr("href") );
if(a.data("html")==""){
// search same link already retrieved
let s = 'a[href^="' + h+'"]';
$(s).each(function(){
if($(this).data("html")!="" && typeof($(this).data("html"))!=="undefined") {
a.data("html", $(this).data("html")); a.data("status",2); return;
}
});
}
// link has status 0 and no html
if(a.data("html")=="" && a.data("status")==0){
// add div container for popup and fetch content
$('#blpwp').html(_f("loading-preview")).attr("class","mblpwp loading a " + blpwp_params['options'].style + " " + (mobile() ? "mob" :""));
a.data("status",1);
$.ajax({
url: blpwp_params['url'],
type :'POST',
data: {
"geturl" : h,
"idpost" : blpwp_params['idpost'],
"action" : 'blpwpgetinfo',
"rel" : a.attr("rel")
},
success: function(html){
a.data("html",html);
a.data("status",2); // status = 2 ==> OK
if(currentA == h || !currentA) showThisPop(a);
},
});
} else {
// there is html or status != 0
if(a.data("status")==1) { // link is loading
$('#blpwp').html(_f("loading-preview")).attr("class","mblpwp loading c " + blpwp_params['options'].style + " " + (mobile() ? "mob" :""));
} else {
// status is 2 ==> OK
if(a.data("status") == 2 ) {
if(currentA == h || !currentA) showThisPop(a);
} else {
// other
$('#blpwp').remove();
}
}
}
}
function prepareLayers(){
// black layer on mobile
if($('#underblpwp').length == 0 && mobile()) {
addOverlayer();
}
if($('#blpwp').length == 0) {
// add div container for popup if not exists
$("body").append("<div id='blpwp' class='mblpwp loading b " + blpwp_params['options'].style + " " + (mobile() ? "mob" :"") +"' style='font-size:" + blpwp_params['options'].size + "px'>" + _f("loading-preview") +"</div>");
}
}
function followMouse(e){
currentA = convertToAbsoluteURL( e.target.getAttribute("href") );
$('#blpwp').removeClass("fading");
let posX = e.pageX - $('#blpwp').width()/2;
if($('#blpwp').width() +20 + posX > $(window).width()) {
posX = $(window).width() - $('#blpwp').width() - 20;
} else if(posX < 0) {
posX = 0;
}
let posY = e.pageY +50;
if($('#blpwp').height() +50 + posY > $(document).height()) {
posY = $(document).height() - $('#blpwp').height() -50;
}
$('#blpwp').css({
left: posX,
top: posY
});
}
let $selector = "";
if (blpwp_params['options'].contentlinks == 'on') $selector +="a.blpwp";
if (blpwp_params['options'].commentlinks == 'on') $selector += ($selector!="" ? ", " : "" ) + "a.blpwp_comment";
if (blpwp_params['options'].selectorchk == 'on' && blpwp_params['options'].selector!="")
$selector += ($selector!="" ? ", " : "" ) + blpwp_params['options'].selector;
// alert($selector)
$( $selector ).each(function(index){
var a = $(this);
let h = convertToAbsoluteURL( a.attr("href") );
if( blpwp_params['options'].counter == 'yes' ) {
setTimeout(function(){
$.ajax({
url: blpwp_params['url'],
type :'POST',
data: {
"url" : h,
"idpost" : blpwp_params['idpost'],
"action" : 'blpwpgetclick'
},
success: function(html){
if(html>"0") {
// there was an error
a.after("<sup class='blpwp_counter'>" + html+"<span> click</span></sup>");
}
},
});
}, index * 500);
}
if( blpwp_params['options'].blank == 'on' && h.indexOf("http") == 0 && h.indexOf(blpwp_params["internals"]) == -1) {
a.attr("target","_blank");
}
let check = blpwp_params['options'].extint == '' || h.indexOf("mailto:") == 0;
if (!check && blpwp_params['options'].extint == 'internal' && (h.indexOf("/") == 0 || h.indexOf(blpwp_params["internals"]) == 0 )) {
check = true;
}
if (!check && blpwp_params['options'].extint == 'external' && h.indexOf("http") == 0 && h.indexOf(blpwp_params["internals"]) == -1 ) {
check = true;
}
let preventClick = 0;
if (check && blpwp_params['options'].imagelinks == '' && h.match(/(\.(jpe?g|gif|png|webp))$/, 'i') && $(this).find("img").length == 1 && $(this).children().length == 1) {
check = false;
}
if (check) {
// console.log("check")
if ($(this).attr("href").indexOf("wp-admin")==-1 && $(this).attr("href").trim()!="") {
a.data("html","");
a.data("status",0);
a.data("preventClick",preventClick);
a.on("click",function(e){
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
if(mobile()) {
if(blpwp_params['options'].disable_mobile == 'on') {
triggerClick(a);
} else {
// on mobile the click open the pop up
prepareLayers();
setupPopUp(a, e);
}
} else {
// on desktop the click is a click
triggerClick(a);
}
});
a.on("mouseenter",function(e){
if(!mobile()) {
prepareLayers();
setupPopUp(a, e);
// on desktop popup follows cursor
a.on("mousemove",function(e){
if(!mobile()) followMouse(e);
});
}
});
// on desktop remove pop up on exit
a.on("mouseleave",function(e){
currentA = null;
if(!mobile()) $('#blpwp').addClass("fading");
});
}
}
});
// on desktop when scroll remove pop up
$(window).on("scroll",function(){
closePopup();
});
$(window).on("resize",function(){
closePopup();
});
});