/**
*
* Credits
*
* Plugin-Name: Akismet Anti-Spam
* Plugin-URI: https://akismet.com/
* Author: Automattic
* Author URI: https://automattic.com/wordpress-plugins/
* License: GPLv2 or later
* Text Domain: akismet
*
*/
jQuery(function($) {
const MSHOT_REFRESH_DELAY = 6000;
const MSHOT_REFRESH_QUERY_ARG = 'mainwp_mshot_refresh';
function mainwp_preview_append_cache_bust(url, token) {
if (!url) {
return url;
}
let separator = url.indexOf('?') === -1 ? '?' : '&';
return url + separator + MSHOT_REFRESH_QUERY_ARG + '=' + encodeURIComponent(token + '-' + Date.now());
}
function mainwp_preview_get_button_mshot_url(element, requeue) {
let attrName = requeue ? 'data-mainwp-mshot-requeue-src' : 'data-mainwp-mshot-src';
let mshotUrl = $(element).attr(attrName);
if (mshotUrl) {
return mshotUrl;
}
let linkUrl = $(element).attr('preview-site-url');
if (!linkUrl) {
return '';
}
mshotUrl = '//s0.wp.com/mshots/v1/' + encodeURIComponent(linkUrl) + '?w=900';
if (requeue) {
mshotUrl += '&requeue=true';
}
return mshotUrl;
}
function mainwp_preview_clear_image_recovery(img) {
if (img && img.mainwpMshotRecoveryTimer) {
clearTimeout(img.mainwpMshotRecoveryTimer);
img.mainwpMshotRecoveryTimer = null;
}
}
function mainwp_preview_queue_image_recovery(img) {
if (!img || img.dataset.mainwpMshotRecoveryRequested === '1') {
return;
}
let primarySrc = img.getAttribute('data-mainwp-mshot-src') || img.getAttribute('data-src') || img.getAttribute('src');
let requeueSrc = img.getAttribute('data-mainwp-mshot-requeue-src');
if (!primarySrc || !requeueSrc) {
return;
}
img.dataset.mainwpMshotRecoveryRequested = '1';
mainwp_preview_clear_image_recovery(img);
// Ask mShots to regenerate the thumbnail once, then retry the canonical URL.
img.setAttribute('src', mainwp_preview_append_cache_bust(requeueSrc, 'requeue'));
img.mainwpMshotRecoveryTimer = setTimeout(function () {
img.setAttribute('src', mainwp_preview_append_cache_bust(primarySrc, 'reload'));
}, MSHOT_REFRESH_DELAY);
}
if (!window.mainwpPreviewImageRecoveryBound) {
document.addEventListener('error', function (event) {
let target = event.target;
if (target && target.tagName === 'IMG') {
mainwp_preview_queue_image_recovery(target);
}
}, true);
window.mainwpPreviewImageRecoveryBound = true;
}
/**
* Init site preview function.
*/
window.mainwp_preview_init_event = function () {
let mshotRemovalTimer = null;
let mshotSecondTryTimer = null;
let mshotThirdTryTimer = null;
let previewTableSelector = '.mainwp-with-preview-table';
let mshotEnabledLinkSelector = 'td span.mainwp-preview-item';
// Show a preview image of the hovered URL.
$(previewTableSelector).off('.mainwpPreview').on('click.mainwpPreview', mshotEnabledLinkSelector, function () {
clearTimeout(mshotRemovalTimer);
if ($('.mainwp-preview-mshot').length > 0) {
if ($('.mainwp-preview-mshot:first').data('link') == this) {
// The preview is already showing for this link.
return;
}
else {
// A new link is being hovered, so remove the old preview.
let existingImage = $('.mainwp-preview-mshot .mshot-image').get(0);
mainwp_preview_clear_image_recovery(existingImage);
$('.mainwp-preview-mshot').remove();
}
}
clearTimeout(mshotSecondTryTimer);
clearTimeout(mshotThirdTryTimer);
let primarySrc = mainwp_preview_get_button_mshot_url(this, false);
let requeueSrc = mainwp_preview_get_button_mshot_url(this, true);
let $image = $('', {
src: primarySrc,
width: 450,
height: 338,
class: 'mshot-image'
});
let mShot = $('