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.6
  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. max-width: initial !important;
  59. right: 50px !important;
  60. width: 45%;
  61. }
  62. .m-newsfeed--feed {
  63. margin-left: 20px !important;
  64. width: 50% !important;
  65. }
  66. .m-newsfeed--sidebar,
  67. .m-boost-rotator-item,
  68. .m-boost-console-link
  69. {
  70. display: none !important;
  71. }
  72. `);
  73.  
  74. function formatComment(n) {
  75. let title = n.entity.title;
  76. if (title === false) {
  77. title = n.entity.message;
  78. }
  79. if (title === false && n.entity.remind_object !== undefined) {
  80. title = n.entity.remind_object.message;
  81. }
  82. if (title === "") {
  83. title = "[empty]";
  84. }
  85.  
  86. let img = n.entity.thumbnail_src;
  87. if (img == false && n.entity.custom_data !== false) {
  88. img = n.entity.custom_data[0].src;
  89. }
  90. if (img == false && n.entity.remind_object !== undefined) {
  91. img = n.entity.remind_object.thumbnail_src;
  92. }
  93.  
  94. let ret = '<i class="mytime">' + getTime(n) + '</i> ' +
  95. '<a class="mya" target="_blank" href="https://www.minds.com/' + n.from.username + '">' + n.from.username + '</a>' +
  96. ' on ' +
  97. '<a class="mya" target="_blank" href="https://www.minds.com/newsfeed/' + n.entity.guid + '">' + title + '</a>' +
  98. '<p class="myp">';
  99.  
  100. if (img !== false && img !== undefined) {
  101. ret += '<img class="myimg" src="' + img + '">';
  102. }
  103. if (n.description !== false) {
  104. ret += n.description;
  105. }
  106. ret += '</p>';
  107.  
  108. return ret;
  109. }
  110.  
  111. function formatGroupActivity(n) {
  112. let img = n.entity.thumbnail_src;
  113. if (img == false && n.entity.custom_data[0] !== undefined) {
  114. img = n.entity.custom_data[0].src;
  115. }
  116.  
  117. let ret = '<i class="mytime">' + getTime(n) + '</i> ' +
  118. '<a class="mya" target="_blank" href="https://www.minds.com/' + n.from.username + '">' + n.from.username + '</a>' +
  119. ' in group ' +
  120. '<a class="mya" target="_blank" href="https://www.minds.com/newsfeed/' + n.entity.guid + '">' + n.params.group.name + '</a>' +
  121. '<p class="myp">';
  122.  
  123. if (img !== false) {
  124. ret += '<img class="myimg" src="' + img + '">';
  125. }
  126. if (n.description !== false) {
  127. ret += n.description;
  128. }
  129. ret += '</p>';
  130.  
  131. return ret;
  132. }
  133.  
  134. function formatTag(n) {
  135. console.log(n);
  136. let ret = '<i class="mytime">' + getTime(n) + '</i> ' +
  137. '<a class="mya" target="_blank" href="https://www.minds.com/' + n.from.username + '">' + n.from.username + '</a> ' +
  138. '<a class="mya" target="_blank" href="https://www.minds.com/newsfeed/' + n.entity.guid + '">tagged you</a>' +
  139. '<p class="tagged">' +
  140. n.description +
  141. '</p>';
  142. return ret;
  143. }
  144.  
  145. function getTime(n) {
  146. let time = new Date(n.time_created * 1000);
  147. let options = {
  148. year: 'numeric',
  149. month: 'numeric',
  150. day: 'numeric',
  151. hour: 'numeric',
  152. minute: 'numeric',
  153. second: 'numeric'
  154. };
  155. return time.toLocaleDateString("en-US", options);
  156. }
  157.  
  158. function showNotifications() {
  159. getNotifications(function(response) {
  160. let target = $('.m-newsfeed--boost-sidebar')[0];
  161. target.innerHTML = "";
  162. let nn = response.notifications;
  163. for (var i = 0; i < nn.length; i++) {
  164. var n = nn[i];
  165.  
  166. if (n.notification_view === "group_activity") {
  167. target.innerHTML += formatGroupActivity(n);
  168. } else
  169. if (n.notification_view === "comment") {
  170. target.innerHTML += formatComment(n);
  171. } else
  172. if (n.notification_view === "tag") {
  173. target.innerHTML += formatTag(n);
  174. } else {
  175. //console.log(n);
  176. }
  177. }
  178. });
  179. }
  180.  
  181. function getNotifications(callback) {
  182. return http('GET', 'api/v1/notifications/all?limit=100', null, callback);
  183. }
  184.  
  185. function getCookie(name) {
  186. var value = "; " + document.cookie;
  187. var parts = value.split("; " + name + "=");
  188. if (parts.length == 2) {
  189. return parts.pop().split(";").shift();
  190. }
  191. }
  192.  
  193. function http(method, url, payload, callback) {
  194. $.ajax({
  195. method: method,
  196. url: url,
  197. headers: {
  198. 'x-xsrf-token': getCookie('XSRF-TOKEN')
  199. }
  200. })
  201. .done(function(ret) {
  202. callback(ret);
  203. });
  204. }
  205.  
  206. showNotifications();
  207. setInterval(showNotifications, 5 * 1000);