HF Thread Citing

Makes citing threads convenient and useful!

  1. // ==UserScript==
  2. // @name HF Thread Citing
  3. // @author Emylbus
  4. // @namespace sublyme.net
  5. // @description Makes citing threads convenient and useful!
  6. // @include *.hackforums.net/showthread.php*
  7. // @exclude *.hackforums.net/showthread.php*&*
  8. // @version 1
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. function getThreadTitle(){
  13. // Grab the title from the navigation menu.
  14. var navArray = document.getElementsByClassName('navigation')[0].innerHTML.split('\n');
  15. for(i = 0; i < navArray.length; i++){
  16. // The open page is given an "active" class, coupled with my @include I can know that this will give me the thread name.
  17. if(navArray[i].indexOf('class="active"') != -1)
  18. {
  19. // Screw regex, yay ugly string splitting! &nbsp; is the character between thread tags and their title.
  20. var threadTitle = navArray[i].split('class="active">')[1].split('</span>')[0].replace('&nbsp;',' ');
  21. var threadString = "[url="+window.location+"][b]"+threadTitle+"[/b][/url]";
  22. return threadString;
  23. }
  24. }
  25. return "ERROR: Could not determine the thread name! :(";
  26. }
  27.  
  28. function getThreadOP(){
  29. // Grab the user name and profile link from the first post. Coupled with my @exclude I know that this will only get the first post on the first page, hence OP.
  30. var authorArray = document.getElementsByClassName("post_author")[0].getElementsByClassName("largetext")[0];
  31. var authorProfile = authorArray.getElementsByTagName("a")[0];
  32. // For some reason trying to parse a staff member's name, I would constantly get fucked up by this strong tag. Don't need it for my purposes anyway.
  33. var authorName = authorProfile.innerHTML.replace("<strong>","");
  34. // Again, screw regex!
  35. authorName = authorName.replace(">","<").split("<");
  36. // Some tricksy stuff to grab the name from the middle of tags.
  37. authorName = authorName[Math.floor(authorName.length/2)];
  38. var authorString = "[url="+authorProfile+"]"+authorName+"[/url]";
  39. return authorString;
  40. }
  41.  
  42. function generateCitation(){
  43. var citation = getThreadTitle()+" by "+getThreadOP();
  44. window.prompt("Press Ctrl+C to copy thread citation!",citation);
  45. }
  46.  
  47. function main(){
  48. // This took me forever to figure out, turns out userscripts aren't directly injected into the site code so you need to use an event listener.
  49. document.getElementsByClassName('navigation')[0].innerHTML = document.getElementsByClassName('navigation')[0].innerHTML + '<small><a title="Cite this thread!" href="javascript:void(0);" id="citer">[cite]</a></small>';
  50. var elementLink = document.getElementById('citer');
  51. elementLink.addEventListener("click", generateCitation, true);
  52. }
  53.  
  54. main(); // Dunno if I should be putting javascript in a main() function, but that's how I grew up with other languages so :P