InstaSynchP Timestamp

Adds timestamps to the chat

目前为 2015-01-03 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name InstaSynchP Timestamp
  3. // @namespace InstaSynchP
  4. // @description Adds timestamps to the chat
  5.  
  6. // @version 1
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-Timestamp
  9. // @license MIT
  10.  
  11. // @include http://*.instasynch.com/*
  12. // @include http://instasynch.com/*
  13. // @include http://*.instasync.com/*
  14. // @include http://instasync.com/*
  15. // @grant none
  16. // @run-at document-start
  17.  
  18. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js?version=30464
  19. // @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js
  20. // ==/UserScript==
  21.  
  22. function Timestamp(version) {
  23. "use strict";
  24. this.version = version;
  25. this.name = 'InstaSynchP Timestamp';
  26. this.settings = [{
  27. 'label': 'Timestamp',
  28. 'id': 'chat-timestamp',
  29. 'type': 'checkbox',
  30. 'default': true,
  31. 'section': ['Chat', 'Timestamp']
  32. }, {
  33. 'label': 'Timestamp Format',
  34. 'id': 'chat-timestamp-format',
  35. 'type': 'text',
  36. 'default': 'hh:mm:ss',
  37. 'size': 10,
  38. 'section': ['Chat', 'Timestamp']
  39. }];
  40. }
  41.  
  42. Timestamp.prototype.executeOnce = function() {
  43. "use strict";
  44. var th = this;
  45.  
  46. function getFormattedText(data, isEmote) {
  47. var timestamp;
  48. timestamp = moment.unix(data.unix).format(gmc.get('chat-timestamp-format'));
  49. if (isEmote) {
  50. if (gmc.get('chat-timestamp')) {
  51. return '{0} - {1} {2}'.format(timestamp, data.username, data.message);
  52. } else {
  53. return '{0} {1}'.format(data.username, data.message);
  54. }
  55. } else {
  56. if (gmc.get('chat-timestamp')) {
  57. return '{0} - {1}: '.format(timestamp, data.username);
  58. } else {
  59. return '{0}: '.format(data.username);
  60. }
  61. }
  62. }
  63.  
  64. //add/remove timestamps when changing the setting
  65. events.on(th, 'SettingChange[chat-timestamp],SettingChange[chat-timestamp-format]', function() {
  66. $('#chat-messages').children().each(function() {
  67. var data, newText;
  68. data = JSON.parse($(this).attr('data'));
  69. // /me message
  70. if ($(this).find('.emote').length > 0) {
  71. $(this).find('.emote').text(getFormattedText(data, true));
  72. } else {
  73. $(this).find('.username').text(getFormattedText(data, false));
  74. }
  75.  
  76. });
  77. scrollDown();
  78. });
  79.  
  80. events.on(th, 'AddMessage', function(user, message) {
  81. var now, timestamp, lastMessage, data;
  82. now = new moment();
  83. lastMessage = $('#chat-messages > :last-child');
  84. data = {
  85. 'unix': now.unix(),
  86. 'username': user.username,
  87. 'message': message.replace(/^\/me /,'')
  88. };
  89.  
  90. lastMessage.attr('data', JSON.stringify(data));
  91.  
  92. if (lastMessage.find('.emote').length > 0) {
  93. lastMessage.find('.emote').text(getFormattedText(data, true));
  94. } else {
  95. lastMessage.find('.username').text(getFormattedText(data, false));
  96. }
  97.  
  98. if (window.autoscroll) {
  99. scrollDown();
  100. }
  101. });
  102. }
  103.  
  104. window.plugins = window.plugins || {};
  105. window.plugins.timestamp = new Timestamp('1');