Advertisement
martixy

HF Story Controls

Oct 11th, 2016
2,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         HF Story mode
  3. // @namespace    http://www.hentai-foundry.com/
  4. // @version      0.2.1
  5. // @description  Controls for story reading in HF
  6. // @author       martixy
  7. // @match        https://www.hentai-foundry.com/stories/user/*/Chapter*
  8. // @match        http://www.hentai-foundry.com/stories/user/*/Chapter*
  9. // @require      https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js
  10. // @grant        GM_addStyle
  11. // @grant        GM_setValue
  12. // @grant        GM_getValue
  13. // @copyright    2016+, martixy
  14. // ==/UserScript==
  15. /* jshint -W097 */
  16. //'use strict';
  17.  
  18. /* CHANGELOG:
  19. v0.1 Initial version
  20.  + Controls to change text size and width
  21.  + Remembers settings between sessions
  22. v0.2 22-04-2017
  23.  + Added colour controls.
  24.  + Also helpful titles for everything.
  25.  * Fixed mangled comments.
  26. v0.2.1 13-01-2018
  27.  * Fixed url match to https
  28.  * Fixed changed jQuery selector
  29. */
  30.  
  31. GM_addStyle('\
  32.    .text-controls {\
  33.        float: right;\
  34.        display: inline-block;\
  35.    }\
  36.    .boxtitle {\
  37.        display: inline-block;\
  38.    }\
  39.    .text-controls > span {\
  40.        cursor: pointer;\
  41.    }\
  42.    .color-box {\
  43.        display: inline-block;\
  44.        height: 1.4em;\
  45.        width: 1.4em;\
  46.        padding: 0;\
  47.        vertical-align: text-top;\
  48.        margin-right: 0.3em;\
  49.        border: 1px solid #272822;\
  50.    }\
  51.    ');
  52.  
  53. var controls = $('<div class="text-controls"><input type="color" class="color-box" name="Background Color" id="HFSM_bgColor" title="Change background colour"/><input type="color" class="color-box" name="Text Color" id="HFSM_fontColor" title="Change text colour"/><span id="largerText" title="Increase text size">A</span> <span id="smallerText" title="Decrease text size">a</span> <span id="largerMargin" title="Increase Margin">↦</span> <span id="smallerMargin" title="Decrease Margin">↤</span> </div>');
  54.  
  55. $('#viewChapter > .boxheader').append(controls);
  56. var body = $('#viewChapter > .boxbody');
  57. var fontSize = GM_getValue('HFSM_fontSize', 1);
  58. var marginWidth = GM_getValue('HFSM_marginWidth', 0);
  59. var fontColorDefault = rgb2hex(body.css('color'));
  60. var bgColorDefault = rgb2hex(body.css('background-color'));
  61. var fontColor = GM_getValue('HFSM_fontColor', fontColorDefault);
  62. var bgColor = GM_getValue('HFSM_bgColor', bgColorDefault);
  63.  
  64.  
  65. body.css('font-size', fontSize + "em");
  66. body.css('padding', "0 " + marginWidth + "px");
  67. body.css('color', fontColor);
  68. body.css('background-color', bgColor);
  69. $('#HFSM_fontColor').val(fontColor);
  70. $('#HFSM_bgColor').val(bgColor);
  71.  
  72. $(document).on('keydown', routeKeys);
  73. $('.boxheader').on('click', '#largerText', increaseFontSize);
  74.  
  75. $('.boxheader').on('click', '#smallerText', decreaseFontSize);
  76.  
  77. $('.boxheader').on('click', '#largerMargin', increaseMargin);
  78.  
  79. $('.boxheader').on('click', '#smallerMargin', decreaseMargin);
  80.  
  81. $('.boxheader').on('change', '#HFSM_fontColor', changeFontColor);
  82.  
  83. $('.boxheader').on('change', '#HFSM_bgColor', changeBgColor);
  84.  
  85. function increaseFontSize(ev) {
  86.     fontSize += 0.1;
  87.     GM_setValue('HFSM_fontSize', fontSize);
  88.     body.css('font-size', fontSize + "em");
  89. }
  90.  
  91. function decreaseFontSize(ev) {
  92.     fontSize -= 0.1;
  93.     GM_setValue('HFSM_fontSize', fontSize);
  94.     body.css('font-size', fontSize + "em");
  95. }
  96.  
  97. function increaseMargin(ev) {
  98.     marginWidth += 50;
  99.     GM_setValue('HFSM_marginWidth', marginWidth);
  100.     body.css('padding', "0 " + marginWidth + "px");
  101. }
  102.  
  103. function decreaseMargin(ev) {
  104.     marginWidth = Math.max(0, marginWidth-50);
  105.     GM_setValue('HFSM_marginWidth', marginWidth);
  106.     body.css('padding', "0 " + marginWidth + "px");
  107. }
  108.  
  109. function changeFontColor(ev) {
  110.     fontColor = $(this).val();
  111.     GM_setValue('HFSM_fontColor', fontColor);
  112.     body.css('color', fontColor);
  113. }
  114.  
  115. function changeBgColor(ev) {
  116.     bgColor = $(this).val();
  117.     GM_setValue('HFSM_bgColor', bgColor);
  118.     body.css('background-color', bgColor);
  119. }
  120.  
  121. function reset(ev) {
  122.     fontSize = 1;
  123.     marginWidth = 0;
  124.     GM_setValue('HFSM_fontSize', fontSize);
  125.     GM_setValue('HFSM_marginWidth', marginWidth);
  126.     GM_setValue('HFSM_fontColor', fontColorDefault);
  127.     GM_setValue('HFSM_bgColor', bgColorDefault);
  128.  
  129.     body.css('font-size', fontSize + "em");
  130.     body.css('padding', "0 " + marginWidth + "px");
  131.     body.css('color', fontColorDefault);
  132.     body.css('background-color', bgColorDefault);
  133.  
  134.     $('#HFSM_fontColor').val(fontColorDefault);
  135.     $('#HFSM_bgColor').val(bgColorDefault);
  136. }
  137.  
  138. function routeKeys(ev) {
  139.     if(ev.type == 'keydown') {
  140.         switch(ev.keyCode) {
  141.             case 39: increaseMargin(); break;
  142.             case 37: decreaseMargin(); break;
  143.             case 221: increaseFontSize(); break;
  144.             case 219: decreaseFontSize(); break;
  145.             case 27: reset(); break;
  146.         }
  147.     }
  148. }
  149. // 37 = left
  150. // 39 = right
  151. // 38 = up
  152. // 40 = down
  153. // 221 = ]
  154. // 219 = [
  155.  
  156. // Keycode tester
  157. // https://css-tricks.com/snippets/javascript/javascript-keycodes/
  158.  
  159. function rgb2hex(rgb) {
  160.     rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  161.     function hex(x) {
  162.         return ("0" + parseInt(x).toString(16)).slice(-2);
  163.     }
  164.     return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement