Minds UI Improvements

Shows comments on the right hand side of the page.

目前为 2018-05-10 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Minds UI Improvements
  3. // @namespace http://www.minds.com/
  4. // @version 0.5
  5. // @description Shows comments on the right hand side of the page.
  6. // @author You
  7. // @match https://www.minds.com/newsfeed/*
  8. // @grant none
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
  10. // ==/UserScript==
  11.  
  12. function addGlobalStyle(css) {
  13. var head, style;
  14. head = document.getElementsByTagName('head')[0];
  15. if (!head) { return; }
  16. style = document.createElement('style');
  17. style.type = 'text/css';
  18. style.innerHTML = css;
  19. head.appendChild(style);
  20. }
  21.  
  22. addGlobalStyle(`
  23. .myp {
  24. padding: 5px;
  25. background: #FFFFFF;
  26. border-radius: 5px;
  27. width: 100%;
  28. display: flex;
  29. margin-bottom: 10px;
  30. word-break: break-word;
  31. border: 1px solid #e8e8e8;
  32. }
  33. .tagged {
  34. border: 1px solid #e8e8e8;
  35. width: 100%;
  36. padding: 5px;
  37. border-radius: 10px;
  38. background: #EEEEFF;
  39. }
  40. .mytime {
  41. margin-right: 5px;
  42. color: #4690df;
  43. font-style: normal;
  44. font-size: .8em;
  45. }
  46. .myimg {
  47. max-width: 40%;
  48. max-height: 100px;
  49. border: 1px solid black;
  50. margin: 0px;
  51. margin-right: 15px;
  52. border-radius: 3px;
  53. }
  54. .mya {
  55. text-decoration: none;
  56. }
  57. .m-newsfeed--boost-sidebar {
  58. right: 50px !important;
  59. }
  60. .m-newsfeed--feed {
  61. margin-left: 20px !important;
  62. }
  63. `);
  64.  
  65. function formatComment(n) {
  66. let title = n.entity.title;
  67. if (title === false) {
  68. title = n.entity.message;
  69. }
  70. if (title === false && n.entity.remind_object !== undefined) {
  71. title = n.entity.remind_object.message;
  72. }
  73. if (title === "") {
  74. title = "[empty]";
  75. }
  76.  
  77. let img = n.entity.thumbnail_src;
  78. if (img == false && n.entity.custom_data !== false) {
  79. img = n.entity.custom_data[0].src;
  80. }
  81. if (img == false && n.entity.remind_object !== undefined) {
  82. img = n.entity.remind_object.thumbnail_src;
  83. }
  84.  
  85. let ret = '<i class="mytime">' + getTime(n) + '</i> ' +
  86. '<a class="mya" target="_blank" href="https://www.minds.com/' + n.from.username + '">' + n.from.username + '</a>' +
  87. ' on ' +
  88. '<a class="mya" target="_blank" href="https://www.minds.com/newsfeed/' + n.entity.guid + '">' + title + '</a>' +
  89. '<p class="myp">';
  90.  
  91. if (img !== false && img !== undefined) {
  92. ret += '<img class="myimg" src="' + img + '">';
  93. }
  94. if (n.description !== false) {
  95. ret += n.description;
  96. }
  97. ret += '</p>';
  98.  
  99. return ret;
  100. }
  101.  
  102. function formatGroupActivity(n) {
  103. let img = n.entity.thumbnail_src;
  104. if (img == false && n.entity.custom_data[0] !== undefined) {
  105. img = n.entity.custom_data[0].src;
  106. }
  107.  
  108. let ret = '<i class="mytime">' + getTime(n) + '</i> ' +
  109. '<a class="mya" target="_blank" href="https://www.minds.com/' + n.from.username + '">' + n.from.username + '</a>' +
  110. ' in group ' +
  111. '<a class="mya" target="_blank" href="https://www.minds.com/newsfeed/' + n.entity.guid + '">' + n.params.group.name + '</a>' +
  112. '<p class="myp">';
  113.  
  114. if (img !== false) {
  115. ret += '<img class="myimg" src="' + img + '">';
  116. }
  117. if (n.description !== false) {
  118. ret += n.description;
  119. }
  120. ret += '</p>';
  121.  
  122. return ret;
  123. }
  124.  
  125. function formatTag(n) {
  126. console.log(n);
  127. let ret = '<i class="mytime">' + getTime(n) + '</i> ' +
  128. '<a class="mya" target="_blank" href="https://www.minds.com/' + n.from.username + '">' + n.from.username + '</a> ' +
  129. '<a class="mya" target="_blank" href="https://www.minds.com/newsfeed/' + n.entity.guid + '">tagged you</a>' +
  130. '<p class="tagged">' +
  131. n.description +
  132. '</p>';
  133. return ret;
  134. }
  135.  
  136. function getTime(n) {
  137. let time = new Date(n.time_created * 1000);
  138. let options = {
  139. year: 'numeric',
  140. month: 'numeric',
  141. day: 'numeric',
  142. hour: 'numeric',
  143. minute: 'numeric',
  144. second: 'numeric'
  145. };
  146. return time.toLocaleDateString("en-US", options);
  147. }
  148.  
  149. function showNotifications() {
  150. getNotifications(function(response) {
  151. let target = $('.m-newsfeed--boost-sidebar')[0];
  152. target.innerHTML = "";
  153. let nn = response.notifications;
  154. for (var i = 0; i < nn.length; i++) {
  155. var n = nn[i];
  156.  
  157. if (n.notification_view === "group_activity") {
  158. target.innerHTML += formatGroupActivity(n);
  159. } else
  160. if (n.notification_view === "comment") {
  161. target.innerHTML += formatComment(n);
  162. } else
  163. if (n.notification_view === "tag") {
  164. target.innerHTML += formatTag(n);
  165. } else {
  166. //console.log(n);
  167. }
  168. }
  169. });
  170. }
  171.  
  172. function getNotifications(callback) {
  173. return http('GET', 'api/v1/notifications/all?limit=100', null, callback);
  174. }
  175.  
  176. function getCookie(name) {
  177. var value = "; " + document.cookie;
  178. var parts = value.split("; " + name + "=");
  179. if (parts.length == 2) {
  180. return parts.pop().split(";").shift();
  181. }
  182. }
  183.  
  184. function http(method, url, payload, callback) {
  185. $.ajax({
  186. method: method,
  187. url: url,
  188. headers: {
  189. 'x-xsrf-token': getCookie('XSRF-TOKEN')
  190. }
  191. })
  192. .done(function(ret) {
  193. callback(ret);
  194. });
  195. }
  196.  
  197. showNotifications();
  198. setInterval(showNotifications, 5 * 1000);