PTH Request notes

Store notes on requests at PTH

  1. // ==UserScript==
  2. // @name PTH Request notes
  3. // @version 0.2
  4. // @description Store notes on requests at PTH
  5. // @author Chameleon
  6. // @include http*://*redacted.ch/requests.php*
  7. // @include http*://*passthepopcorn.me/requests.php*
  8. // @grant none
  9. // @namespace https://greasyfork.org/users/87476
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. var before=document.getElementById('request_comments');
  16. if(!before)
  17. before=document.getElementById('comments');
  18. if(!before)
  19. return;
  20. var requestId=parseInt(window.location.href.split('id=')[1]);
  21. var comment=getComment(requestId);
  22. var div=document.createElement('div');
  23. before.parentNode.insertBefore(div, before);
  24. div.setAttribute('class', 'box box2');
  25. div.innerHTML='<div class="head"><strong>Notes</strong></div>';
  26. var pad=document.createElement('div');
  27. pad.setAttribute('class', 'pad');
  28. div.appendChild(pad);
  29. var t=document.createElement('textarea');
  30. t.setAttribute('id', 'requestNotes');
  31. t.setAttribute('style', 'width: 100%;');
  32. t.rows="8";
  33. pad.appendChild(t);
  34. t.value=comment;
  35. t.addEventListener('keyup', save.bind(undefined, requestId, t), false);
  36. resize('requestNotes');
  37. })();
  38.  
  39. function save(id, t)
  40. {
  41. var notes=window.localStorage.requestNotes;
  42. if(!notes)
  43. notes=[];
  44. else
  45. notes=JSON.parse(notes);
  46. var noteExisted=false;
  47. for(var i=0; i<notes.length; i++)
  48. {
  49. if(notes[i].id === id)
  50. {
  51. notes[i].comment=t.value;
  52. noteExisted=true;
  53. break;
  54. }
  55. }
  56. if(!noteExisted)
  57. {
  58. notes.push({id:id, comment:t.value});
  59. }
  60. window.localStorage.requestNotes=JSON.stringify(notes);
  61. resize('requestNotes');
  62. }
  63.  
  64. function getComment(id)
  65. {
  66. var notes=window.localStorage.requestNotes;
  67. if(!notes)
  68. notes=[];
  69. else
  70. notes=JSON.parse(notes);
  71. for(var i=0; i<notes.length; i++)
  72. {
  73. if(notes[i].id === id)
  74. return notes[i].comment;
  75. }
  76. return '';
  77. }