Advertisement
Foxscotch

Locked topic quotes

Jan 15th, 2016
1,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Locked topic quotes
  3. // @namespace    https://foxscotch.us/
  4. // @version      1.3
  5. // @description  Show quote buttons on locked topics
  6. // @author       Foxscotch
  7. // @match        https://forum.blockland.us/index.php?topic=*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. var mainUrl = 'https://forum.blockland.us/';
  12. var quoteImageUrl = mainUrl + 'Themes/Blockland/images/english/quote.gif';
  13.  
  14. function getStatus() {
  15.     var status = {};
  16.     var statusImages = document.querySelectorAll('.titlebg > td > img');
  17.    
  18.     if (statusImages[statusImages.length - 1].src.includes('locked')) {
  19.         status.locked = true;
  20.     }
  21.     if (statusImages.length > 1) {
  22.         status.poll = true;
  23.         if (statusImages[0].src.includes('locked')) {
  24.             status.pollLocked = true;
  25.         }
  26.     }
  27.    
  28.     return status;
  29. }
  30. var status = getStatus();
  31.  
  32. function getSession() {
  33.     var logout = document.querySelector('img[alt="Logout"]').parentElement;
  34.     return logout.href.split('=')[2];
  35. }
  36. var sessId = getSession();
  37.  
  38. function addTextBox() {
  39.     var container = document.createElement('div');
  40.     container.id = 'locked-topic-container';
  41.    
  42.     var textArea = document.createElement('textarea');
  43.     textArea.id = 'locked-topic-textbox';
  44.    
  45.     var copyButton = document.createElement('button');
  46.     copyButton.id = 'locked-topic-copy';
  47.     copyButton.textContent = "Copy to clipboard";
  48.    
  49.     container.style.margin = 'auto';
  50.     container.style.width = '25%';
  51.    
  52.     textArea.style.display = 'block';
  53.     textArea.style.height = '150px';
  54.     textArea.style.width = "100%";
  55.    
  56.     copyButton.addEventListener('click', function () {
  57.         textArea.select();
  58.         var copied = document.execCommand('copy');
  59.         copied ? console.log('Copied') : console.log('Not copied');
  60.     });
  61.    
  62.     var breaks = document.getElementsByTagName('br');
  63.     breaks[breaks.length - 2].parentElement.insertBefore(container, breaks[breaks.length - 1]);
  64.     container.appendChild(copyButton);
  65.     container.appendChild(textArea);
  66. }
  67.  
  68. function getQuote(link) {
  69.     window.ajax_indicator(1);
  70.     var xhr = new XMLHttpRequest();
  71.     var quote;
  72.  
  73.     xhr.onload = function () {
  74.         xml = xhr.responseXML;
  75.         quote = xml.getElementsByTagName('quote')[0].textContent;
  76.  
  77.         document.getElementById('locked-topic-textbox').value += quote;
  78.  
  79.         window.ajax_indicator(0);
  80.     };
  81.  
  82.     xhr.open('GET', `${mainUrl}index.php?action=quotefast;quote=${link.dataset.messageId};sesc=${sessId};xml`);
  83.     xhr.send();
  84. }
  85.  
  86. function addQuoteButtons() {
  87.     var quoteDestinations = document.querySelectorAll('td[height="20"]:not([width])');
  88.    
  89.     for (var i = 0; i < quoteDestinations.length; i++) {
  90.         var link = document.createElement('a');
  91.         link.href = "#locked-topic-textbox";
  92.        
  93.         var quoteImg = document.createElement('img');
  94.         quoteImg.src = quoteImageUrl;
  95.         quoteImg.alt = 'Reply with quote';
  96.         quoteImg.border = '0';
  97.        
  98.         link.appendChild(quoteImg);
  99.         quoteDestinations[i].appendChild(link);
  100.        
  101.         var msgId = quoteDestinations[i].parentElement.querySelector('div').id.split('_')[1];
  102.         link.dataset.messageId = msgId;
  103.  
  104.         link.addEventListener('click', getQuote.bind(null, link));
  105.     }
  106. }
  107.  
  108. if (status.locked) {
  109.     addTextBox();
  110.     addQuoteButtons();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement