Valerii Vasin (valerii) wrote in changelog,
Valerii Vasin
valerii
changelog

[livejournal] r23246: LJSUP-14136: [Friends Feed] Optimize lik...

Committer: vvasin
LJSUP-14136: [Friends Feed] Optimize like buttons parsing. Check if button is on the page before parsing it to prevent faults.
U   trunk/htdocs/js/livejournal.js
Modified: trunk/htdocs/js/livejournal.js
===================================================================
--- trunk/htdocs/js/livejournal.js	2012-11-01 15:22:35 UTC (rev 23245)
+++ trunk/htdocs/js/livejournal.js	2012-11-02 07:22:31 UTC (rev 23246)
@@ -410,6 +410,15 @@
 LiveJournal.parseLikeButtons = (function ($) {
 	'use strict';
 
+	var selectors = {
+		facebook: '.lj-like-item-facebook',
+		google: '.lj-like-item-google',
+		twitter: '.lj-like-item-twitter',
+		tumblr: '.lj-like-item-tumblr',
+		surfingbird: '.lj-like-item-surfinbird',
+		repost: '.lj-like-item-repost'
+	};
+
 	/**
 	 * Parse lj-like buttons
 	 * @param  {Object} $node jQuery .lj-like node
@@ -457,8 +466,14 @@
 	 * @param  {jQuery} $node jQuery collection
 	 */
 	function parseFacebook($node) {
+		var item = $node.find( selectors.facebook );
+
+		if (item.length === 0) {
+			return;
+		}
+
 		try {
-			window.FB.XFBML.parse( $node.find('.lj-like-item-facebook').get(0) );
+			window.FB.XFBML.parse( item.get(0) );
 		} catch (e) {
 			console.warn(e.message);
 		}
@@ -470,9 +485,15 @@
 	 * @param  {jQuery} $node jQuery node with likes in which we will search for google +1 button for parsing
 	 */
 	function parseGoogle($node) {
+		var item = $node.find( selectors.google );
+
+		if (item.length === 0) {
+			return;
+		}
+
 		// gapi could
 		try {
-			window.gapi.plusone.go( $node.find('.lj-like-item-google').get(0) );
+			window.gapi.plusone.go( item.get(0) );
 		} catch (e) {
 			console.warn(e.message);
 		}
@@ -485,10 +506,16 @@
 	function parseTwitter($node) {
 		var params = null,
 			iframe = null,
-			link = $node.children('.lj-like-item-twitter')
-						.children()
-						.eq(0);
+			// link to replace with iframe
+			link = null,
+			item = $node.find( selectors.twitter );
 
+		if (item.length === 0) {
+			return;
+		}
+
+		link = item.children().eq(0);
+
 		params = {
 			url: link.data('url'),
 			text: link.data('text'),
@@ -508,10 +535,16 @@
 	 * @param  {jQuery} $node jQuery .lj-like node
 	 */
 	function parseSurfingbird($node) {
-		var link = $node.find('.surfinbird__like_button'),
+		var item = $node.find( selectors.surfingbird ),
+			link = null,
 			iframe = null,
 			params = null;
 
+		if (item.length === 0) {
+			return;
+		}
+
+		link = item.find('.surfinbird__like_button');
 		params = {
 			url: link.data('url'),
 			caption: link.data('text'),
@@ -530,13 +563,22 @@
 	 * @param  {jQuery} $node jQuery .lj-like node
 	 */
 	function parseTumblr($node) {
-		var link = $node.find('.tumblr-share-button'),
-			href = link.attr('href'),
-			params = {
-				url: link.data('url'),
-				name: link.data('title')
-			};
+		var item = $node.find( selectors.tumblr ),
+			link = null,
+			params = null,
+			href;
 
+		if (item.length === 0) {
+			return;
+		}
+
+		link = item.find('.tumblr-share-button'),
+		href = link.attr('href'),
+		params = {
+			url: link.data('url'),
+			name: link.data('title')
+		};
+
 		link.attr('href', LiveJournal.constructUrl(href, params));
 	}
 
@@ -545,9 +587,17 @@
 	 * @param  {jQuery} $node jQuery .lj-like node
 	 */
 	function parseRepost($node) {
-		var link = $node.find('.lj-like-item-repost').find('a'),
-			url = link.data('url');
+		var item = $node.find( selectors.repost ),
+			link = null,
+			url;
 
+		if (item.length === 0) {
+			return;
+		}
+
+		link = $node.find('.lj-like-item-repost').find('a'),
+		url = link.data('url');
+
 		LJ.Api.call('repost.get_status', { url: url }, function (data) {
 			link.replaceWith(LiveJournal.renderRepostButton(url, data));
 		});

Tags: invis89, js, livejournal, vvasin
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments