Advertisement
Guest User

Untitled

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