Advertisement
Pugglemuggle

AO3 Floaty Review Box

Dec 15th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        AO3: Floaty Review Box
  3. // @description Adds floaty review box as your read your fic!
  4. // @require     https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js
  5. // @include     http://archiveofourown.org/*
  6. // @include     https://archiveofourown.org/*
  7. // ==/UserScript==
  8.  
  9. // I DIDN'T MAKE THIS! I just tried to fix someone else's code //
  10. // The original can be found here: https://pastebin.com/vYBCYWu4 //
  11.  
  12. // borrowed from https://www.sitepoint.com/6-jquery-cursor-functions/
  13. // i think this just makes stuff work.... idk what it does
  14. jQuery.fn.getSelectionStart = function(){
  15.     if(this.lengh === 0) return -1;
  16.     input = this[0];
  17.  
  18.     var pos = input.value.length;
  19.  
  20.     if (input.createTextRange) {
  21.         var r = document.selection.createRange().duplicate();
  22.         r.moveEnd('character', input.value.length);
  23.         if (r.text === '')
  24.         pos = input.value.length;
  25.         pos = input.value.lastIndexOf(r.text);
  26.     } else if(typeof(input.selectionStart)!="undefined")
  27.     pos = input.selectionStart;
  28.  
  29.     return pos;
  30. };
  31.  
  32. // more stuff from the same above link that magically makes it work
  33. jQuery.fn.getCursorPosition = function(){
  34.     if(this.lengh === 0) return -1;
  35.     return $(this).getSelectionStart();
  36. };
  37.  
  38. // Getting text for potential input... in a jenky way
  39. var HighlightedText = "";
  40.  
  41. document.addEventListener("selectionchange", function() {
  42.   var text = window.getSelection().toString();
  43.   if(text) {
  44.     HighlightedText = text;
  45.   }
  46. });
  47.  
  48. (function($) {
  49.  
  50.     // check if it's a list of works or bookmarks, or header on work page, and attach the menu
  51.     checkWork();
  52.  
  53.     // check if it's a list of works/bookmarks/statistics, or header on work page
  54.     function checkWork() {
  55.  
  56.         var found_work = $('ul.work');
  57.  
  58.         if (found_work.length) {
  59.            
  60.             // HTML to define layout of popup box
  61.             // Include x button to close box
  62.             var sHtml = '<p class="close actions" id="close_floaty"><a aria-label="cancel" style="display: inline-block; margin-top: 5px;">x</a></p>';
  63.             // Button to insert highlighted text and for a help list
  64.             sHtml += '<ul class="actions" style="float: left; margin-top: 15px;"><li id="insert_floaty_text"><a>Insert</a></li><li id="pop_up_review_tips"><a>Review Tips</a></li></ul>';
  65.             // Header
  66.             sHtml += '<div style="background: #900 url(/images/skins/textures/tiles/xred-ao3.png.pagespeed.ic.Glrf2YTGxy.png); margin: 0px; width: 100%; height:15px;" id="reviewTextArea_header"></div>';
  67.             // Textarea
  68.             sHtml += '<textarea style="margin: 5px; width: 97%;" id="floaty_textarea"></textarea>';
  69.  
  70.             // Create popup box
  71.             $("<div/>", {
  72.                 id: "reviewTextArea",
  73.                 width:600, // Change for dimensions
  74.                 height:300, // Change for dimensions
  75.                 css: {
  76.                     backgroundColor:"#ffffff",
  77.                     opacity: 0.75,
  78.                     border: "thin solid black",
  79.                     display: "inline-block",
  80.                     //"padding-right": 10,
  81.                     position: "fixed",
  82.                     top: 150,
  83.                     right: 5,
  84.                     resize: "both",
  85.                     overflow: "auto"
  86.                 },
  87.                 html: sHtml
  88.             }).appendTo("body"); // .resizable().draggable().appendTo("body");
  89.  
  90.             // Hide the popup box by default (comment out line below if you want it to always appear by adding // before it)
  91.             $('#reviewTextArea').hide();
  92.  
  93.             // To close the box
  94.             $('#close_floaty').click(function() {
  95.                 $('#reviewTextArea').hide();
  96.             });
  97.            
  98.             // Anything you type in the box gets inserted into the real comment box below
  99.             $('#floaty_textarea').on('input', function() {
  100.                 $('.comment_form').val($('#floaty_textarea').val());
  101.             });
  102.  
  103.             // Add Float review box button to the top
  104.             $('ul.work').prepend('<li id="floaty_review_box"><a>Floaty Review Box</a></li>');
  105.  
  106.             // If the above button is clicked, display the review box
  107.             $('#floaty_review_box').click(function() {
  108.                 $('#reviewTextArea').show();
  109.             });
  110.  
  111.             // Insert highlighted/selected text into textarea when Insert button is clicked
  112.             $('#insert_floaty_text').click(function() {
  113.                 var sInitialText = $('#floaty_textarea').val();
  114.                 var iPosition = $('#floaty_textarea').getCursorPosition();
  115.  
  116.                 var sHighlightedText = HighlightedText;
  117.  
  118.                 var sNewText = sInitialText.substr(0, iPosition) + '<i>"' + sHighlightedText + '"</i>\n' + sInitialText.substr(iPosition);
  119.                 $('#floaty_textarea').val(sNewText); // $('#floaty_textarea').val(sNewText);
  120.  
  121.                 // Copy into real comment box
  122.                 $('.comment_form').val($('#floaty_textarea').val());
  123.             });
  124.  
  125.             // Create the review tips box
  126.             sReviewTipsHtml = '<p class="close actions" id="close_review_tips"><a aria-label="cancel" style="display: inline-block;">x</a></p>' +
  127.                     'Writers will love any love you give them. If you&#39;re looking for things to help jumpstart a review, there are lots of different things you could focus on.<br />' +
  128.                     '<ul><li>Quotes you liked</li><li>Scenes you liked</li><li>What&#39;s your feeling at the end of the chapter (did it move you?)</li><li>What are you most looking forward to next?</li>' +
  129.                     '<li>Do you have any predictions for the next chapters you want to share?</li><li>Did this chapter give you any questions you can&#39;t wait to find out the answers for?</li>' +
  130.                     '<li>How would you describe the fic to a friend if you were recommending it?</li><li>Is there something unique about the story that you like?</li><li>Does the author have a style that really works for you?</li>' +
  131.                     '<li>Did the author leave any comments in the notes that said what they wanted feedback on?</li>' +
  132.                     '<li>Even if all you have are &quot;incoherent screams of delight&quot;, and can&#39;t come up with a real comment at the moment, authors love to hear that as well</li></ul>';
  133.             $("<div/>", {
  134.                 id: "reviewTips",
  135.                 width:600, // Change for dimensions
  136.                 height:300, // Change for dimensions
  137.                 css: {
  138.                     backgroundColor:"#ffffff",
  139.                     border: "thin solid black",
  140.                     'font-size': '80%',
  141.                     padding: '10px 10px 0 10px',
  142.                     position: "fixed",
  143.                     top: 150,
  144.                     right: 620,
  145.                 },
  146.                 html: sReviewTipsHtml
  147.             }).appendTo("body"); // .resizable().draggable()
  148.             $('#reviewTips li').css('list-style', 'circle inside none');
  149.             $('#reviewTips').hide();
  150.  
  151.             // Pop up list of review tips
  152.             $('#pop_up_review_tips').click(function() {
  153.                 $('#reviewTips').show();
  154.             });
  155.  
  156.             $('#close_review_tips').click(function() {
  157.                 $('#reviewTips').hide();
  158.             });
  159.         }
  160.     }
  161.  
  162. })(jQuery);
  163.  
  164. // This draggable stuff was copied from
  165. // https://www.w3schools.com/howto/howto_js_draggable.asp
  166. // because this bitch doesn't know shit
  167.  
  168. //Make the DIV element draggagle:
  169. if (document.getElementById(("reviewTextArea"))) {
  170.     dragElement(document.getElementById(("reviewTextArea")));
  171.     dragElement(document.getElementById(("reviewTips")));
  172. }
  173.  
  174. function dragElement(elmnt) {
  175.   var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  176.   if (document.getElementById(elmnt.id + "_header")) {
  177.     /* if present, the header is where you move the DIV from:*/
  178.     document.getElementById(elmnt.id + "_header").onmousedown = dragMouseDown;
  179.   } else {
  180.     /* otherwise, move the DIV from anywhere inside the DIV:*/
  181.     elmnt.onmousedown = dragMouseDown;
  182.   }
  183.  
  184.   function dragMouseDown(e) {
  185.     e = e || window.event;
  186.     // get the mouse cursor position at startup:
  187.     pos3 = e.clientX;
  188.     pos4 = e.clientY;
  189.     document.onmouseup = closeDragElement;
  190.     // call a function whenever the cursor moves:
  191.     document.onmousemove = elementDrag;
  192.   }
  193.  
  194.   function elementDrag(e) {
  195.     e = e || window.event;
  196.     // calculate the new cursor position:
  197.     pos1 = pos3 - e.clientX;
  198.     pos2 = pos4 - e.clientY;
  199.     pos3 = e.clientX;
  200.     pos4 = e.clientY;
  201.     // set the element's new position:
  202.     elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  203.     elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  204.   }
  205.  
  206.   function closeDragElement() {
  207.     /* stop moving when mouse button is released:*/
  208.     document.onmouseup = null;
  209.     document.onmousemove = null;
  210.   }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement