keker123

Untitled

Jul 1st, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. // ==UserScript==
  2. // @name chat rooms
  3. // @namespace http://tampermonkey.net/
  4. // @version v0.0.1
  5. // @description Polls for messages in specified room
  6. // @author LeKSuS
  7. // @match https://online.mephi.ru/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=mephi.ru
  9. // @require https://code.jquery.com/jquery-3.7.1.js
  10. // @require https://code.jquery.com/ui/1.13.3/jquery-ui.js
  11. // @resource JQUERY_UI_CSS https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css
  12. // @grant GM_getResourceText
  13. // @grant GM_addStyle
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. const chatRoomsBackend = "https://rooms.leksus.net";
  20.  
  21. const jqueryUiCss = GM_getResourceText("JQUERY_UI_CSS");
  22. GM_addStyle(jqueryUiCss);
  23.  
  24. const container = document.createElement('div');
  25. container.id = 'draggable-container';
  26. container.style.position = 'absolute';
  27. container.style.top = '0';
  28. container.style.left = '0';
  29. container.style.display = 'flex';
  30. container.style.flexDirection = 'column';
  31. container.style.justifyContent = 'space-between';
  32. container.style.background = 'white';
  33. container.style.padding = '20px';
  34. container.style.visibility = "visible";
  35. container.style.borderRadius = '10px';
  36. container.style.boxShadow = '0 0 2px rgba(0, 0, 0, 0.3)';
  37.  
  38. const dragHandle = document.createElement('div');
  39. dragHandle.id = 'draggable-handle';
  40. dragHandle.style.position = 'absolute';
  41. dragHandle.style.top = '5px';
  42. dragHandle.style.left = '5px';
  43. dragHandle.style.width = '20px';
  44. dragHandle.style.height = '20px';
  45. dragHandle.style.background = '#80808026';
  46. dragHandle.style.cursor = 'move';
  47.  
  48. const img = document.createElement('img');
  49. img.src = 'https://imgs.xkcd.com/comics/automation.png';
  50. img.style.maxWidth = '100%';
  51. img.style.maxHeight = '100%';
  52. img.style.objectFit = 'contain';
  53.  
  54. const description = document.createElement('p');
  55. description.textContent = 'xkcd 1319, Automation';
  56.  
  57. const dummy = document.createElement('div');
  58. dummy.style.flexGrow = '1';
  59.  
  60. const bottomRow = document.createElement('div');
  61. bottomRow.style.display = 'flex';
  62. bottomRow.style.justifyContent = 'space-between';
  63. bottomRow.style.alignItems = 'center';
  64. bottomRow.style.padding = '10px';
  65. bottomRow.style.opacity = 0.3;
  66.  
  67. let page = 0;
  68. const updatePage = (updater) => {
  69. page = messages.length === 0 ? 0 : (updater(page) + messages.length) % messages.length;
  70. saveState();
  71. updateUI();
  72. }
  73. const pageCounter = document.createElement('p');
  74. pageCounter.style.userSelect = "none";
  75. pageCounter.textContent = "0/0";
  76. pageCounter.onclick = () => updatePage(p => p + 1);
  77.  
  78. const opacitySlider = document.createElement('input');
  79. opacitySlider.type = 'range';
  80. opacitySlider.min = '0';
  81. opacitySlider.max = '1';
  82. opacitySlider.step = '0.1';
  83. opacitySlider.value = '1';
  84. opacitySlider.style.flexGrow = '1';
  85. opacitySlider.oninput = function() {
  86. container.style.opacity = this.value;
  87. saveState();
  88. };
  89.  
  90. let roomId = "";
  91. let scrollToLastPage = false;
  92. let messages = [];
  93. const roomIdInput = document.createElement('input');
  94. roomIdInput.type = 'text';
  95. roomIdInput.placeholder = 'room id';
  96. roomIdInput.oninput = function() {
  97. roomId = this.value;
  98. saveState();
  99. }
  100. const updateUI = () => {
  101. pageCounter.textContent = `${page + 1}/${messages.length}`;
  102. if (messages[page].image_file !== null) {
  103. img.style.display = "block";
  104. img.src = `${chatRoomsBackend}/uploads/${messages[page].image_file}`;
  105. } else {
  106. img.style.display = "none";
  107. }
  108. description.textContent = messages[page].message;
  109. };
  110. const refreshMessages = (callback) => {
  111. if (roomId === "") {
  112. return;
  113. }
  114.  
  115. fetch(`${chatRoomsBackend}/messages/${roomId}`, { method: "GET" })
  116. .then(response => {
  117. if (response.status === 200) {
  118. return response.json();
  119. }
  120. throw "Room not found";
  121. })
  122. .then(json => {
  123. messages = json;
  124. if (scrollToLastPage) {
  125. page = messages.length - 1;
  126. }
  127. callback();
  128. })
  129. .catch(error => console.error(`Failed to fetch messages from room ${roomId}: ${error}`));
  130. }
  131.  
  132. const loadState = () => {
  133. let state = localStorage.getItem("chrms.state");
  134. if (state === null) {
  135. state = {
  136. container: {
  137. top: "20px",
  138. left: "20px",
  139. width: "400px",
  140. height: "600px",
  141. opacity: 0.7,
  142. },
  143. room: {
  144. id: "",
  145. page: 0,
  146. scrollToLastPage: true,
  147. }
  148. }
  149. } else {
  150. state = JSON.parse(state);
  151. }
  152. opacitySlider.value = state.container.opacity;
  153. container.style.opacity = state.container.opacity
  154. container.style.top = state.container.top;
  155. container.style.left = state.container.left;
  156. container.style.width = state.container.width;
  157. container.style.height = state.container.height;
  158. container.style.visibility = state.container.visibility;
  159. roomId = state.room.id;
  160. roomIdInput.value = state.room.id;
  161. page = state.room.page;
  162. scrollToLastPage = state.room.scrollToLastPage;
  163. }
  164. const saveState = () => {
  165. const position =
  166. localStorage.setItem("chrms.state", JSON.stringify({
  167. container: {
  168. top: container.style.top,
  169. left: container.style.left,
  170. width: container.style.width,
  171. height: container.style.height,
  172. opacity: opacitySlider.value,
  173. visibility: container.style.visibility,
  174. },
  175. room: {
  176. id: roomIdInput.value,
  177. page: page,
  178. scrollToLastPage: scrollToLastPage,
  179. },
  180. }))
  181. }
  182.  
  183. loadState();
  184.  
  185. bottomRow.appendChild(pageCounter);
  186. bottomRow.appendChild(opacitySlider);
  187. bottomRow.appendChild(roomIdInput);
  188. container.appendChild(dragHandle);
  189. container.appendChild(img);
  190. container.appendChild(description);
  191. container.appendChild(dummy);
  192. container.appendChild(bottomRow);
  193. document.body.appendChild(container);
  194.  
  195. (function($) {
  196. $(document).ready(function() {
  197. $("#draggable-container").draggable({
  198. handle: "#draggable-handle",
  199. drag: saveState,
  200. }).resizable({
  201. handles: "all",
  202. resize: saveState,
  203. });
  204. });
  205. })(jQuery);
  206.  
  207. document.addEventListener('contextmenu', function(event) {
  208. event.preventDefault(); // Отключить стандартное контекстное меню
  209. });
  210.  
  211. document.addEventListener('mousedown', function(event) {
  212. if (event.button === 1) { // Middle click
  213. event.preventDefault(); // Отключить стандартное действие средней кнопки мыши
  214. container.style.visibility = container.style.visibility === "hidden" ? "visible" : "hidden";
  215. saveState();
  216. } else if (event.button === 2) { // right click
  217. updatePage(p => p + 1);
  218. } else if (event.button === 0) { // left click
  219. updatePage(p => p - 1);
  220. }
  221. });
  222.  
  223. document.addEventListener('keydown', (event) => {
  224. const key = event.key || event.keyCode;
  225. switch (key) {
  226. case "z":
  227. container.style.visibility = container.style.visibility === "hidden" ? "visible" : "hidden";
  228. saveState();
  229. break;
  230.  
  231. case "ArrowLeft":
  232. updatePage(p => p - 1);
  233. break;
  234.  
  235. case "ArrowRight":
  236. updatePage(p => p + 1);
  237. break;
  238.  
  239. case "f":
  240. scrollToLastPage = !scrollToLastPage;
  241. saveState();
  242. break;
  243. }
  244. });
  245.  
  246. refreshMessages(updateUI);
  247. setInterval(() => {
  248. refreshMessages(updateUI);
  249. }, 1000);
  250. })();
Advertisement
Add Comment
Please, Sign In to add comment