Fanfiction+

Improves readability and allows EBOOK DOWNLOADS!

当前为 2016-10-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Fanfiction+
  3. // @namespace DanielVestol.Fanfiction.net
  4. // @description Improves readability and allows EBOOK DOWNLOADS!
  5. // @include https://www.fanfiction.net/*
  6. // @version 1.16.2
  7. // @grant GM_xmlhttpRequest
  8. // ==/UserScript==
  9.  
  10. // SETTINGS
  11. config = {
  12. ebookServer: "http://8b9b8876.ngrok.io",
  13. };
  14. // END OF SETTINGS
  15. console.log("DOM fully loaded and parsed");
  16.  
  17. // Better change between mobile and desktop edition.
  18. // Credits to SirCxyrtyx for snippet
  19. var mobileVersion = 'location = "https://m.fanfiction.net' + document.location.pathname + '"';
  20. // console.log(mobileVersion);
  21. document.getElementsByClassName('icon-kub-mobile')[0].setAttribute('onclick', mobileVersion);
  22.  
  23. // run code depending on what subpage we are on
  24. // for main page
  25. if(document.location.pathname == "/") {
  26. // Changing some simple styles
  27. document.getElementById('content_wrapper').style.maxWidth = '800px';
  28. // appending a style tag in HTML
  29. $('#top').append('<style>tr>td>div>ins{display:none !important;}.zmenu{display:none !important;}p{-webkit-touch-callout: all;-webkit-user-select: all;-khtml-user-select: all;-moz-user-select: all;-ms-user-select: all;user-select: all;}</style>');
  30. }
  31.  
  32. if(document.location.pathname.substr(0, 3) == "/s/") {
  33. // Changing some simple styles
  34. $('#top').append('<style>tr>td>div>ins{display:none !important;}.zmenu{display:none !important;}p{-webkit-touch-callout: all;-webkit-user-select: all;-khtml-user-select: all;-moz-user-select: all;-ms-user-select: all;user-select: all;}</style>');
  35. document.getElementById('content_wrapper').style.maxWidth = '800px';
  36. // OCD friendly review field
  37. review = document.getElementById('review');
  38. review.getElementsByTagName('TD')[0].remove();
  39. review.getElementsByTagName('TD')[0].remove(); // Remove element 1 which is now 0
  40. review.getElementsByTagName('DIV')[0].style.maxWidth = '765px';
  41. review.getElementsByTagName('DIV')[0].style.margin = '10px';
  42. document.getElementById('review_name_value').style.maxWidth = '780px';
  43. document.getElementById('review_review').style.maxWidth = '780px';
  44. //
  45. // Show whole book function
  46. //
  47. $('#content_wrapper_inner > span:nth-child(7)').append('<button id="myShit" class="btn" type="BUTTON">Show whole book</button>');
  48. document.getElementById('myShit').addEventListener('click', function() {
  49. document.getElementById('storytextp').innerHTML = "<div id='topSuprSecret' style='position:fixed;top:100px;left:100px;width:auto;height:70px;padding-left:20px;padding-right:20px;background-color:#A2DEF2;'><h1>Loading book...</h1></div>";
  50. $.ajaxSetup({
  51. async: false
  52. });
  53. setTimeout(function(){ //timeout to let it get us a progress bar ish thing
  54. for (i = chapter; i < document.getElementById('chap_select').options.length+1; i++) {
  55. // chapter is variable used by fanfiction, it counts what chapter you are at starting at 1.
  56. // my own call to get chapter has +1 because it fetches a list starting at 0 instead.
  57. console.log("Downloading " + document.location.pathname.substr(3, 8).replace("/", "") + " chapter " + i);
  58. var $div = $('<div>');
  59. $div.load('https://www.fanfiction.net/s/' + document.location.pathname.substr(3, 8).replace("/", "") + '/' + i + ' #storytext', function(){
  60. // now $(this)[0].innerHTML contains #storytext
  61. $("#storytextp").append($(this)[0].innerHTML);
  62. });
  63. }
  64. document.getElementById('topSuprSecret').outerHTML = '';
  65. }, 100); // end of timeout
  66. });
  67. //
  68. // EBOOKS!!!!!!!!!!!!!!!!!
  69. //
  70. $('#content_wrapper_inner > span:nth-child(7)').append('<button id="getEbook" class="btn" type="BUTTON">ebook</button>');
  71. document.getElementById('getEbook').addEventListener('click', function() {
  72. document.getElementById('storytextp').innerHTML = "<div id='topSuprSecret' style='position:fixed;top:100px;left:100px;width:auto;height:70px;padding-left:20px;padding-right:20px;background-color:#A2DEF2;'><h1>Loading book...</h1></div>";
  73. $.ajaxSetup({
  74. async: false
  75. });
  76. data = {
  77. title: document.querySelector("#profile_top > b").innerHTML,
  78. author: document.querySelector("#profile_top > a").innerHTML,
  79. contents: [],
  80. bookID: document.location.pathname.substr(3, 8).replace("/", "")
  81. };
  82. setTimeout(function() {
  83. for (i = 1; i < document.getElementById('chap_select').options.length+1; i++) {
  84. console.log("Downloading " + document.location.pathname.substr(3, 8).replace("/", "") + " chapter " + i);
  85. var $div = $('<div>');
  86. $div.load('https://www.fanfiction.net/s/' + document.location.pathname.substr(3, 8).replace("/", "") + '/' + i + ' #storytext', function(){
  87. // now $(this)[0].innerHTML contains #storytext
  88. $("#storytextp").append($(this)[0].innerHTML);
  89. var tempras = $(this)[0].innerHTML;
  90. // insert our chapter into the data object
  91. data.contents[data.contents.length] = {
  92. title: document.getElementById('chap_select').options[i-1].innerHTML,
  93. data: tempras,
  94. id: i
  95. };
  96. });
  97. }
  98. document.querySelector("#topSuprSecret").innerHTML = "<h1>Converting to epub...</h1>";
  99. console.log(data);
  100. GM_xmlhttpRequest({
  101. method: "POST",
  102. url: config.ebookServer + "/epubify",
  103. data: JSON.stringify(data),
  104. headers: {
  105. "Content-Type": "application/json"
  106. },
  107. onload:function(response) {
  108. console.log(response.responseText + " book is done");
  109. if(response.responseText == "success") {
  110. location.href = config.ebookServer + "/" + document.location.pathname.substr(3, 8).replace("/", "") + ".epub";
  111. } else {
  112. alert("Could not contact ebook server. Please alert author.");
  113. }
  114. document.getElementById('topSuprSecret').outerHTML = '';
  115. }
  116. });
  117. }, 100); // end of timeout
  118. });
  119. }