From 6ee91fbb258c5156f337a31c44c5bfaede5302ab Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 10 May 2016 01:22:43 +0300 Subject: [PATCH] #78 fixed. Null recomendations comments fixed. WS close messages in console. --- chrome_point_plus/js/point-plus.js | 116 +++++++++++++++-------------- 1 file changed, 60 insertions(+), 56 deletions(-) diff --git a/chrome_point_plus/js/point-plus.js b/chrome_point_plus/js/point-plus.js index 6655630..bef6df5 100644 --- a/chrome_point_plus/js/point-plus.js +++ b/chrome_point_plus/js/point-plus.js @@ -24,6 +24,9 @@ function PointPlus(ppVersion) { // Getting username var point_username = $('#name h1').text(); + // Getting post id + var postId = $('#top-post').attr('data-id'); + console.debug('Current post id detected as #%s', postId); // Проверяем, загрузились ли мы var point_plus_debug = $('#point-plus-debug'); @@ -387,9 +390,6 @@ function PointPlus(ppVersion) { ws = new WebSocket(((location.protocol == 'https:') ? 'wss' : 'ws') + '://point.im/ws'); console.log('WebSocket created: %O', ws); - // Detecting post id if presented - var postId = $('#top-post').attr('data-id'); - console.debug('Current post id detected as #%s', postId); // Detecting view mode treeSwitch = $('#tree-switch a.active').attr('href'); console.debug('Comments view mode: %s', treeSwitch); @@ -399,6 +399,12 @@ function PointPlus(ppVersion) { console.error('WebSocket error: %O', err); }; + // Close handler + ws.onclose = function(evt) { + console.error('WebSocket closed: %O', evt); + }; + + // todo: refactor to background service // Message handler ws.onmessage = function(evt) { try { @@ -427,17 +433,13 @@ function PointPlus(ppVersion) { // Recommendation comment case 'ok': // Do not break here. Using next case for this message - + // Comments case 'comment': - if (wsMessage.a === 'comment') { - console.groupCollapsed('WS comment #%s/%s', wsMessage.post_id, wsMessage.comment_id); - } else if (wsMessage.a === 'ok') { - console.groupCollapsed('WS comment rec #%s/%s', wsMessage.post_id, wsMessage.comment_id); - } + console.groupCollapsed('WS \'%s\' #%s/%s', wsMessage.a, wsMessage.post_id, wsMessage.comment_id); // Check option - if ( ! options.is('option_ws_comments')) { + if (!options.is('option_ws_comments')) { console.log('Comments processing disabled'); console.groupEnd(); break; @@ -457,56 +459,58 @@ function PointPlus(ppVersion) { break; } - // Generating comment from websocket message - create_comment_elements({ - id: (wsMessage.a === 'ok') ? wsMessage.rcid : wsMessage.comment_id, - toId: wsMessage.to_comment_id, - postId: wsMessage.post_id, - author: wsMessage.author, - html: wsMessage.html, - fadeOut: options.is('option_ws_comments_color_fadeout'), - isRec: (wsMessage.a === 'ok') ? true : false, - unread: (point_username && point_username !== wsMessage.author) ? true : false, - }, function($comment) { - // It's time to DOM - console.info('Inserting comment'); - - // Search for parent comment - $parentComment = (wsMessage.to_comment_id) ? ($('div.post[data-comment-id="' + wsMessage.to_comment_id + '"]')) : []; - console.log('Parent comment: %O', $parentComment || null); - - // If list mode or not addressed to other comment - if ($('#comments #tree-switch a').eq(0).hasClass('active') || (wsMessage.to_comment_id === null) || (!$parentComment.length)) { - // Adding to the end of the list - $('.content-wrap #comments #post-reply').before($comment); - } else { - // Check for children - $parentCommentChildren = $parentComment.next('.comments'); - // If child comment already exist - if ($parentCommentChildren.length > 0) { - console.log('Child comments found. Appending...'); - $parentCommentChildren.append($comment); + // If comment has text + if (wsMessage.hasOwnProperty('html') && typeof wsMessage.html === 'string') { + // Generating comment from websocket message + create_comment_elements({ + id: (wsMessage.a === 'ok') ? wsMessage.rcid : wsMessage.comment_id, + toId: wsMessage.to_comment_id, + postId: wsMessage.post_id, + author: wsMessage.author, + html: wsMessage.html, + fadeOut: options.is('option_ws_comments_color_fadeout'), + isRec: (wsMessage.a === 'ok') ? true : false, + unread: (point_username && point_username !== wsMessage.author) ? true : false, + }, function($comment) { + // It's time to DOM + console.info('Inserting comment'); + + // Search for parent comment + $parentComment = (wsMessage.to_comment_id) ? ($('div.post[data-comment-id="' + wsMessage.to_comment_id + '"]')) : []; + console.log('Parent comment: %O', $parentComment || null); + + // If list mode or not addressed to other comment + if ($('#comments #tree-switch a').eq(0).hasClass('active') || (wsMessage.to_comment_id === null) || (!$parentComment.length)) { + // Adding to the end of the list + $('.content-wrap #comments #post-reply').before($comment); } else { - console.log('No child comments found. Creating...'); - $parentComment.after($('
').addClass('comments').append($comment)); + // Check for children + $parentCommentChildren = $parentComment.next('.comments'); + // If child comment already exist + if ($parentCommentChildren.length > 0) { + console.log('Child comments found. Appending...'); + $parentCommentChildren.append($comment); + } else { + console.log('No child comments found. Creating...'); + $parentComment.after($('
').addClass('comments').append($comment)); + } } - } - // Desktop notifications - if (options.is('option_ws_comments_notifications')) { - console.log('Showing desktop notification'); - messenger.sendMessage({ - type: 'showNotification', - notificationId: 'comment_' + wsMessage.post_id + '#' + wsMessage.comment_id, - avatarUrl: getProtocol() + '//point.im/avatar/' + wsMessage.author + '/80', - title: '@' + wsMessage.author + ' #' + wsMessage.post_id + '/' + wsMessage.comment_id, - text: wsMessage.text - }, function(response) {}); - } - - console.groupEnd(); - }); + // Desktop notifications + if (options.is('option_ws_comments_notifications')) { + console.log('Showing desktop notification'); + messenger.sendMessage({ + type: 'showNotification', + notificationId: 'comment_' + wsMessage.post_id + '#' + wsMessage.comment_id, + avatarUrl: getProtocol() + '//point.im/avatar/' + wsMessage.author + '/80', + title: '@' + wsMessage.author + ' #' + wsMessage.post_id + '/' + wsMessage.comment_id, + text: wsMessage.text + }, function(response) {}); + } + console.groupEnd(); + }); + } break;