Minds UI Improvements

Shows comments on the right hand side of the page.

目前為 2018-05-11 提交的版本,檢視 最新版本

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