summaryrefslogtreecommitdiff
path: root/js/show-own-posts.js
blob: 8b83f4cdaed86c9fdc247f0cffae24fcecb90b40 (plain)
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
/*
 * show-own-posts.js
 * https://github.com/savetheinternet/Tinyboard/blob/master/js/show-op.js
 *
 * Adds "(You)" to a name field when the post is yours. Update references as well.
 *
 * Released under the MIT license
 * Copyright (c) 2014 Marcin Łabanowski <marcin@6irc.net>
 *
 * Usage:
 *   $config['additional_javascript'][] = 'js/jquery.min.js';
 *   $config['additional_javascript'][] = 'js/ajax.js';
 *   $config['additional_javascript'][] = 'js/show-own-posts.js';
 *
 */


+function(){


var update_own = function() {
  if ($(this).is('.you')) return;

  var thread = $(this).parents('[id^="thread_"]').first();
  if (!thread.length) {
    thread = $(this);
  }

  var board = thread.attr('data-board');
  var posts = JSON.parse(localStorage.own_posts || '{}');

  var id = $(this).attr('id').split('_')[1];

  if (posts[board] && posts[board].indexOf(id) !== -1) { // Own post!
    $(this).addClass('you');
    $(this).find('span.name').first().append(' '+_('(You)'));
  }

  // Update references
  $(this).find('div.body:first a:not([rel="nofollow"])').each(function() {
    var postID;

    if(postID = $(this).text().match(/^>>(\d+)$/))
      postID = postID[1];
    else
      return;

    if (posts[board] && posts[board].indexOf(postID) !== -1) {
      $(this).after(' <small>'+_('(You)')+'</small>');
    }
  });
};

var update_all = function() {
  $('div[id^="thread_"], div.post.reply').each(update_own);
};

var board = null;

$(function() {
  board = $('input[name="board"]').first().val();

  update_all();
});

$(document).on('ajax_after_post', function(e, r) {
  var posts = JSON.parse(localStorage.own_posts || '{}');
  posts[board] = posts[board] || [];
  posts[board].push(r.id);
  localStorage.own_posts = JSON.stringify(posts);
});

$(document).on('new_post', function(e,post) {
  var $post = $(post);
  if ($post.is('div.post.reply')) { // it's a reply
    $post.each(update_own);
  }
  else {
    $post.each(update_own); // first OP
    $post.find('div.post.reply').each(update_own); // then replies
  }
});



}();