| 1 |
/** |
| 2 |
* |
| 3 |
* Credits |
| 4 |
* |
| 5 |
* Plugin-Name: Akismet Anti-Spam |
| 6 |
* Plugin-URI: https://akismet.com/ |
| 7 |
* Author: Automattic |
| 8 |
* Author URI: https://automattic.com/wordpress-plugins/ |
| 9 |
* License: GPLv2 or later |
| 10 |
* Text Domain: akismet |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
jQuery(document).ready(function ($) { |
| 15 |
|
| 16 |
/** |
| 17 |
* Init site preview function. |
| 18 |
*/ |
| 19 |
mainwp_preview_init_event = function() { |
| 20 |
var mshotRemovalTimer = null; |
| 21 |
var mshotSecondTryTimer = null |
| 22 |
var mshotThirdTryTimer = null |
| 23 |
|
| 24 |
var mshotEnabledLinkSelector = 'td span.mainwp-preview-item'; |
| 25 |
|
| 26 |
// Show a preview image of the hovered URL. |
| 27 |
$( '.mainwp-with-preview-table' ).on( 'click', mshotEnabledLinkSelector, function () { |
| 28 |
clearTimeout( mshotRemovalTimer ); |
| 29 |
|
| 30 |
if ( $( '.mainwp-preview-mshot' ).length > 0 ) { |
| 31 |
if ( $( '.mainwp-preview-mshot:first' ).data( 'link' ) == this ) { |
| 32 |
// The preview is already showing for this link. |
| 33 |
return; |
| 34 |
} |
| 35 |
else { |
| 36 |
// A new link is being hovered, so remove the old preview. |
| 37 |
$( '.mainwp-preview-mshot' ).remove(); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
clearTimeout( mshotSecondTryTimer ); |
| 42 |
clearTimeout( mshotThirdTryTimer ); |
| 43 |
|
| 44 |
var thisHref = $( this ).attr( 'preview-site-url' ); |
| 45 |
|
| 46 |
var mShot = $( '<div class="mainwp-preview-mshot mshot-container"><div class="mshot-arrow"></div><img src="' + mainwp_preview_mshot_url( thisHref ) + '" width="450" height="338" class="mshot-image" /></div>' ); |
| 47 |
mShot.data( 'link', this ); |
| 48 |
|
| 49 |
var offset = $( this ).offset(); |
| 50 |
|
| 51 |
mShot.offset( { |
| 52 |
left : Math.min( $( window ).width() - 475, offset.left + $( this ).width() + 10 ), // Keep it on the screen if the link is near the edge of the window. |
| 53 |
top: offset.top + ( $( this ).height() / 2 ) - 101 // 101 = top offset of the arrow plus the top border thickness |
| 54 |
} ); |
| 55 |
|
| 56 |
// These retries appear to be superfluous if .mshot-image has already loaded, but it's because mShots |
| 57 |
// can return a "Generating thumbnail..." image if it doesn't have a thumbnail ready, so we need |
| 58 |
// to retry to see if we can get the newly generated thumbnail. |
| 59 |
mshotSecondTryTimer = setTimeout( function () { |
| 60 |
mShot.find( '.mshot-image' ).attr( 'src', mainwp_preview_mshot_url( thisHref, 2 ) ); |
| 61 |
}, 6000 ); |
| 62 |
|
| 63 |
mshotThirdTryTimer = setTimeout( function () { |
| 64 |
mShot.find( '.mshot-image' ).attr( 'src', mainwp_preview_mshot_url( thisHref, 3 ) ); |
| 65 |
}, 12000 ); |
| 66 |
|
| 67 |
$( 'body' ).append( mShot ); |
| 68 |
} ).on( 'mouseover', 'tr', function () { |
| 69 |
// When the mouse hovers over a row, begin preloading mshots for links. |
| 70 |
var linksToPreloadMshotsFor = $( this ).find( mshotEnabledLinkSelector ); |
| 71 |
|
| 72 |
linksToPreloadMshotsFor.each( function () { |
| 73 |
// Don't attempt to preload an mshot for a single link twice. Browser caching should cover this, but in case of |
| 74 |
// race conditions, save a flag locally when we've begun trying to preload one. |
| 75 |
if ( ! $( this ).data( 'mainwp-preview-mshot-preloaded' ) ) { |
| 76 |
mainwp_preview_preload_mshot( $( this ).attr( 'preview-site-url' ) ); |
| 77 |
$( this ).data( 'mainwp-preview-mshot-preloaded', true ); |
| 78 |
} |
| 79 |
} ); |
| 80 |
} ); |
| 81 |
|
| 82 |
$( document ).on( 'mouseup', function () { |
| 83 |
if ( $( '.mainwp-preview-mshot' ).length > 0 ) { |
| 84 |
mshotRemovalTimer = setTimeout( function () { |
| 85 |
clearTimeout( mshotSecondTryTimer ); |
| 86 |
clearTimeout( mshotThirdTryTimer ); |
| 87 |
$( '.mainwp-preview-mshot' ).remove(); |
| 88 |
}, 100 ); |
| 89 |
} |
| 90 |
} ); |
| 91 |
}; |
| 92 |
|
| 93 |
// init preview. |
| 94 |
mainwp_preview_init_event(); |
| 95 |
|
| 96 |
/** |
| 97 |
* Generate an mShot URL if given a link URL. |
| 98 |
* |
| 99 |
* @param string linkUrl |
| 100 |
* @param int retry If retrying a request, the number of the retry. |
| 101 |
* @return string The mShot URL; |
| 102 |
*/ |
| 103 |
function mainwp_preview_mshot_url( linkUrl, retry ) { |
| 104 |
var mshotUrl = '//s0.wordpress.com/mshots/v1/' + encodeURIComponent( linkUrl ) + '?w=900'; |
| 105 |
|
| 106 |
if ( retry ) { |
| 107 |
mshotUrl += '&r=' + encodeURIComponent( retry ); |
| 108 |
} |
| 109 |
|
| 110 |
return mshotUrl; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Begin loading an mShot preview of a link. |
| 115 |
* |
| 116 |
* @param string linkUrl |
| 117 |
*/ |
| 118 |
function mainwp_preview_preload_mshot( linkUrl ) { |
| 119 |
var img = new Image(); |
| 120 |
img.src = mainwp_preview_mshot_url( linkUrl ); |
| 121 |
} |
| 122 |
}); |
| 123 |
|