Advertisement
Guest User

Untitled

a guest
Jan 6th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. // ==UserScript==
  2. // @name lor karma and scores
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.0
  5. // @author vvn_black
  6. // @match https://www.linux.org.ru/*
  7. // @updateURL https://lorka.sytes.net/lorka.user.js
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14.  
  15. const articles = () => [...document.querySelectorAll('article.msg')].map(item => item.id.split('-')[1]);
  16.  
  17. const get_score = ids => {
  18. fetch(`https://lorka.sytes.net/scores/?ids=${ids.join(',')}`)
  19. .then(response => response.json())
  20. .then(renderScores)
  21. .catch(error => console.log(error));
  22. };
  23.  
  24. const vote_comment = (a_id, vote) => {
  25. fetch(`https://lorka.sytes.net/vote/${a_id}/${vote}`)
  26. .then(response => response.json())
  27. .then(json => {
  28. const data = {};
  29. data[a_id] = json;
  30. renderScores(data);
  31. })
  32. .catch(error => console.log(error));
  33. };
  34.  
  35. const vote_karma = (user, vote = '') => {
  36. fetch(`https://lorka.sytes.net/karma/${user}/${vote}`)
  37. .then(response => response.json())
  38. .then(json => {
  39. renderKarma(json, user);
  40. })
  41. .catch(error => console.log(error));
  42. };
  43.  
  44. const renderKarma = (data, user) => {
  45. const { up, down, vote } = data;
  46.  
  47. var karma = document.createElement('div');
  48. karma.id = 'karma';
  49. karma.innerHTML = '<b>Карма: </b>';
  50.  
  51. var thumbsUp = document.createElement('i');
  52. thumbsUp.style.cssText = 'margin-left:1em;cursor:pointer;';
  53. thumbsUp.classList.add('fa', vote === 'up' ? 'fa-thumbs-up' : 'fa-thumbs-o-up');
  54. thumbsUp.addEventListener('click', () => {vote_karma(user, vote === 'up' ? 'zero' : 'up');});
  55.  
  56. var score = document.createElement('span');
  57. score.style.cssText = 'margin-left:1em;';
  58. score.innerHTML = `${up - down}`;
  59.  
  60. var thumbsDown = document.createElement('i');
  61. thumbsDown.style.cssText = 'margin-left:1em;cursor:pointer;';
  62. thumbsDown.classList.add('fa', vote === 'down' ? 'fa-thumbs-down' : 'fa-thumbs-o-down');
  63. thumbsDown.addEventListener('click', () => {vote_karma(user, vote === 'down' ? 'zero' : 'down');});
  64.  
  65. karma.appendChild(thumbsUp);
  66. karma.appendChild(score);
  67. karma.appendChild(thumbsDown);
  68.  
  69.  
  70. const vcard = document.querySelector('.vcard');
  71. const parent = vcard.parentNode;
  72. var child = document.getElementById('karma');
  73. if (child) {
  74. parent.replaceChild(karma, child);
  75. } else {
  76. parent.insertBefore(karma, vcard.nextSibling);
  77. }
  78. };
  79.  
  80. const renderScore = (a_id, state = {}) => {
  81. const { up = 0, down = 0, vote = 'zero' } = state;
  82.  
  83. var score = document.createElement('div');
  84. score.id = `score-${a_id}`;
  85. score.style.cssText = 'float: right;';
  86.  
  87. var thumbsUp = document.createElement('i');
  88. thumbsUp.style.cssText = 'cursor:pointer;';
  89. thumbsUp.style.display = 'none';
  90. thumbsUp.classList.add('fa', vote === 'up' ? 'fa-thumbs-up' : 'fa-thumbs-o-up');
  91. thumbsUp.innerHTML = `<span>&nbsp;${up}</span>`;
  92. thumbsUp.addEventListener('click', () => {vote_comment(a_id, vote === 'up' ? 'zero' : 'up');});
  93.  
  94. var handO = document.createElement('i');
  95. handO.classList.add('fa', 'fa-hand-o-right');
  96. handO.innerHTML = `<span>&nbsp;&nbsp;${up - down}</span>`;
  97.  
  98. var thumbsDown = document.createElement('i');
  99. thumbsDown.style.cssText = 'margin-left:1em;cursor:pointer;';
  100. thumbsDown.style.display = 'none';
  101. thumbsDown.classList.add('fa', vote === 'down' ? 'fa-thumbs-down' : 'fa-thumbs-o-down');
  102. thumbsDown.innerHTML = `<span>&nbsp;${down}</span>`;
  103. thumbsDown.addEventListener('click', () => {vote_comment(a_id, vote === 'down' ? 'zero' : 'down');});
  104.  
  105. score.appendChild(thumbsUp);
  106. score.appendChild(handO);
  107. score.appendChild(thumbsDown);
  108.  
  109. score.addEventListener('mouseover', () => {
  110. handO.style.display = 'none';
  111. thumbsUp.style.display = 'inline';
  112. thumbsDown.style.display = 'inline';
  113. });
  114. score.addEventListener('mouseout', () => {
  115. thumbsUp.style.display = 'none';
  116. thumbsDown.style.display = 'none';
  117. handO.style.display = 'inline';
  118. });
  119.  
  120. return score;
  121. };
  122.  
  123. const renderScores = data => {
  124. Object.keys(data).map(item => {
  125. var article = document.querySelectorAll(`#comment-${item}`);
  126. if (!article.length) {
  127. article = document.querySelectorAll(`#topic-${item}`);
  128. }
  129. if (!article.length) {
  130. return;
  131. }
  132.  
  133. var landing = article[0].getElementsByClassName('sign');
  134.  
  135. const child = document.getElementById(`score-${item}`);
  136. if (child) {
  137. landing[0].replaceChild(renderScore(item, data[item]), child);
  138. } else {
  139. landing[0].appendChild(renderScore(item, data[item]));
  140. }
  141. });
  142. };
  143.  
  144.  
  145. // Append FontAwesome
  146. var head = document.getElementsByTagName('head')[0];
  147. var fa = document.createElement('link');
  148. fa.innerHTML = '<link rel="stylesheet" ' +
  149. 'href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" />';
  150. head.appendChild(fa);
  151.  
  152. // Entry point
  153. if (window.location.href.indexOf('profile') != -1) {
  154. vote_karma(window.location.href.split('/')[4]);
  155. } else {
  156. get_score(articles());
  157. }
  158.  
  159. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement