Advertisement
Guest User

Untitled

a guest
Jan 4th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. // ==UserScript==
  2. // @name lor karma and scores
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.9
  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 } = 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', 'fa-thumbs-up');
  54. thumbsUp.addEventListener('click', () => {vote_karma(user, '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', 'fa-thumbs-down');
  63. thumbsDown.addEventListener('click', () => {vote_karma(user, '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, up, down) => {
  81. var score = document.createElement('div');
  82. score.id = `score-${a_id}`;
  83. score.style.cssText = 'float: right;';
  84.  
  85. var thumbsUp = document.createElement('i');
  86. thumbsUp.style.cssText = 'cursor:pointer;';
  87. thumbsUp.style.display = 'none';
  88. thumbsUp.classList.add('fa', 'fa-thumbs-up');
  89. thumbsUp.innerHTML = `<span>&nbsp;${up}</span>`;
  90. thumbsUp.addEventListener('click', () => {vote_comment(a_id, 'up');});
  91.  
  92. var handO = document.createElement('i');
  93. handO.classList.add('fa', 'fa-hand-o-right');
  94. handO.innerHTML = `<span>&nbsp;&nbsp;${up - down}</span>`;
  95.  
  96. var thumbsDown = document.createElement('i');
  97. thumbsDown.style.cssText = 'margin-left:1em;cursor:pointer;';
  98. thumbsDown.style.display = 'none';
  99. thumbsDown.classList.add('fa', 'fa-thumbs-down');
  100. thumbsDown.innerHTML = `<span>&nbsp;${down}</span>`;
  101. thumbsDown.addEventListener('click', () => {vote_comment(a_id, 'down');});
  102.  
  103. score.appendChild(thumbsUp);
  104. score.appendChild(handO);
  105. score.appendChild(thumbsDown);
  106.  
  107. score.addEventListener('mouseover', () => {
  108. handO.style.display = 'none';
  109. thumbsUp.style.display = 'inline';
  110. thumbsDown.style.display = 'inline';
  111. });
  112. score.addEventListener('mouseout', () => {
  113. thumbsUp.style.display = 'none';
  114. thumbsDown.style.display = 'none';
  115. handO.style.display = 'inline';
  116. });
  117.  
  118. return score;
  119. };
  120.  
  121. const renderScores = data => {
  122. Object.keys(data).map(item => {
  123. const { up, down } = data[item];
  124.  
  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, up, down), child);
  138. } else {
  139. landing[0].appendChild(renderScore(item, up, down));
  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