DLBOOKS BETTER

make it easier to read books!

  1. // ==UserScript==
  2. // @name DLBOOKS BETTER
  3. // @namespace https://greasyfork.org/ja/scripts/40309-dlbooks-better
  4. // @version 1.05
  5. // @description make it easier to read books!
  6. // @author badfalcon
  7. // @match http://dlbooks.to/detail/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. /* load jQuery */
  11. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. var fullscreen = true;
  18. var slideshow = false;
  19. var expandSidemenu = false;
  20.  
  21. /*
  22. function getSettings(){
  23. fullscreen = GM_getValue('fullscreen',true);
  24. GM_setValue('fullscreen',false);
  25. console.log(fullscreen);
  26. }
  27. getSettings();
  28. */
  29.  
  30. var loading = false;
  31.  
  32. var totalPages = parseInt($('#page_num').text().match(/\((.+)\/(.+)\)/i)[2]);
  33.  
  34. var currentPage = 1;
  35. var loadedPages = 0;
  36. var finishedPages = 0;
  37. var preloadNum = 100;
  38. var intervalTime = 100; // millisec
  39. var slideshowInterval = 3000; // millisec
  40. var interval = false;
  41. var timer;
  42. var imageWidth = 0;
  43. var imageHeight = 0;
  44. var imageRatio = 0;
  45. var windowWidth = 0;
  46. var windowHeight = 0;
  47. var windowRatio = 0;
  48.  
  49. timer = false;
  50.  
  51. var debug = false;
  52.  
  53. if(!debug){
  54. preload();
  55. }
  56.  
  57. $('.movie-in-ad').remove();
  58.  
  59. var modalWindow = $('<div>').attr('id','modalWindow')
  60. .css({'position':'fixed',
  61. 'z-index':'100',
  62. 'background-color':'rgba(0,0,0,0.8)',
  63. 'width':'100%',
  64. 'height':'100%',
  65. });
  66.  
  67. var mainBlock = $('<div>').attr('id','mainBlock')
  68. .css({'display':'flex',
  69. 'justify-content': 'center',
  70. 'width':'100%',
  71. 'height':'100%',
  72. });
  73.  
  74. var statusBar = $('<div>').attr('id','statusBar')
  75. .css({'color':'white',
  76. 'position':'fixed',
  77. 'z-index':'100',
  78. 'background-color':'rgba(0,0,0,0.6)',
  79. // 'top':'0',
  80. // 'left':windowWidth/2,
  81. 'padding':'5px',
  82. 'border-radius':'0px 0px 5px 5px',
  83. '-webkit-border-radius':'0px 0px 5px 5px',
  84. '-moz-border-radius':'0px 0px 5px 5px',
  85. 'display':'flex',
  86. 'flex-direction':'column',
  87. });
  88.  
  89. var statusPopup = $('<div>').attr('id','statusPopup')
  90. .css({'color':'white',
  91. });
  92.  
  93. var statusTimeoutID = null;
  94. function showStatus(str,timer){
  95. // statusBar.css('left',(windowWidth-statusPopup.width())/2);
  96. statusPopup.text(str).slideDown("fast",function(){
  97. // statusBar.css('left',(windowWidth-statusBar.width())/2);
  98. if(statusTimeoutID !== null){
  99. clearTimeout(statusTimeoutID);
  100. }
  101. if(timer){
  102. statusTimeoutID = setTimeout(function(){
  103. statusPopup.slideUp('normal',function(){
  104. // statusBar.css('left',(windowWidth-statusBar.width())/2);
  105. });
  106. },2000);
  107. }else{
  108.  
  109. }
  110. });
  111. }
  112.  
  113. function hideOverlay(){
  114. fullscreen = false;
  115. if(slideshow){slideshowButton.click();}
  116. modalWindow.hide();
  117. }
  118.  
  119. function showOverlay(){
  120. fullscreen = true;
  121. modalWindow.show();
  122. }
  123.  
  124. var openButton = $('<button>').text('fullscreen');
  125. openButton.on('click',function(){
  126. showOverlay();
  127. });
  128. $('#one_col > h2:nth-child(1) > span').after(openButton);
  129.  
  130. var sideMenu = $('<div>').css({
  131. "display":"flex",
  132. "flex-direction":"row",
  133. "position":"fixed",
  134. });
  135.  
  136. var sideList = $('<div>').css({
  137. "display":"flex",
  138. "flex-direction":"column",
  139. "width":"80px",
  140. "height":"100%",
  141. "background-color":"rgba(0,0,0,0.5)",
  142. });
  143.  
  144. var toggleButton = $('<button>').text('>');
  145. toggleButton.on('click',function(){
  146. if (sideList.is(':visible')) {
  147. // 表示されている場合の処理
  148. sideList.hide();
  149. $(this).text('>');
  150. expandSidemenu = false;
  151. } else {
  152. // 非表示の場合の処理
  153. sideList.show();
  154. $(this).text('<');
  155. expandSidemenu = true;
  156. }
  157. });
  158.  
  159. var status = $('<div>').attr('class','statusText').html('C('+currentPage+'/'+totalPages+')<br>L('+loadedPages+ '/' + totalPages + ')').css('color','white');
  160. function updateStatus(){
  161. var slideshowStatus = slideshow ? 'playing ' : '';
  162. status.html(slideshowStatus + 'C('+currentPage+'/'+loadedPages+':'+totalPages+')');
  163. }
  164.  
  165. var reloadButton = $('<button>').text('reload');
  166. reloadButton.on('click',function(){
  167. reloadImages();
  168. });
  169.  
  170. var slideshowTimeoutID = null;
  171. var slideshowButton = $('<button>').text('play');
  172. slideshowButton.on('click',function(){
  173. slideshow = !slideshow;
  174. if(slideshow){
  175. slideshowButton.text('stop');
  176. slideshowTimeoutID = setInterval(function(){
  177. thumbNext(totalPages);
  178. // showStatus("play (" + currentPage + "/" + totalPages + ")",true);
  179. },slideshowInterval);
  180. }else{
  181. slideshowButton.text('play');
  182. clearInterval(slideshowTimeoutID);
  183. }
  184. });
  185.  
  186.  
  187. var closeButton = $('<button>').text('×');
  188. closeButton.on('click',function(){
  189. hideOverlay();
  190. });
  191.  
  192. var downloadButton = $('<button>').text('dlZip');
  193. downloadButton.on('click',function(){
  194. $('#download > li:nth-child(2) > a')[0].click();
  195. });
  196.  
  197. var favoriteButton;
  198. var t = $('#download > li.bookshelf > a > img').attr('src');
  199. if(t != null){
  200. var favorited = $('#download > li.bookshelf > a > img').attr('src').includes('set');
  201.  
  202. favoriteButton = $('<button>').text('fav');
  203. function updateFavButton(){
  204. if(favorited){
  205. favoriteButton.text('unfav');
  206. }else{
  207. favoriteButton.text('fav');
  208. }
  209. }
  210. favoriteButton.on('click',function(){
  211. $('#download > li.bookshelf > a')[0].click();
  212. favorited = !favorited;
  213. updateFavButton();
  214. });
  215. updateFavButton();
  216. }
  217.  
  218. var tags = $('<div>').text('△tags').css('color','white');
  219. var showTags = true;
  220. var tagList = $('#detail > ul.tag_list').clone(true).css({
  221. 'color':'white',
  222. 'display':'flex',
  223. 'flex-direction':'column'
  224. });
  225. tags.on('click',function(){
  226. showTags = !showTags;
  227. if(showTags){
  228. tags.text('△tags');
  229. tagList.show();
  230. }else{
  231. tags.text('▽tags');
  232. tagList.hide();
  233. }
  234. });
  235. statusBar.append(status);
  236. statusBar.append(statusPopup);
  237. mainBlock.append(statusBar);
  238. statusPopup.hide();
  239.  
  240. // sideList.append(status);
  241. sideList.append(closeButton);
  242. sideList.append(reloadButton);
  243. sideList.append(slideshowButton);
  244. sideList.append(downloadButton);
  245. if(t != null){
  246. sideList.append(favoriteButton);
  247. }
  248. sideList.append(tags);
  249. sideList.append(tagList);
  250.  
  251. sideMenu.append(sideList).append(toggleButton);
  252.  
  253. sideList.hide();
  254.  
  255. var mainImage = $('<div>')
  256. // .attr({'':'',
  257. // })
  258. .css({'display':'flex',
  259. 'justify-content':'center',
  260. 'align-items':'center',
  261. '':'',
  262. });
  263. mainImage.append($('#thumbnail1').clone(true));
  264. mainBlock.append(mainImage);
  265.  
  266. modalWindow.append(sideMenu).append(mainBlock);
  267.  
  268. $('#container').before(modalWindow);
  269. // $('#container').before($('#thumbnail1').clone(true));
  270.  
  271. // toggleButton.click();
  272.  
  273. function preload(){
  274. if(!loading){
  275. console.log("load start:"+(finishedPages+1) + "~" + (finishedPages + preloadNum));
  276. loading = true;
  277. var url = $('#thumbnail1').attr('src');
  278. var match = url.match(/(http.+\/)(\d{5})\.(jpg|jpeg|gif|png)/i);
  279. var index = parseInt(match[2]);
  280. var finished = 0;
  281. if(interval){
  282. var i = 1;
  283. var id = setInterval(function(){
  284. if(loadedPages+1 > totalPages){
  285. clearInterval(id); //idをclearIntervalで指定している
  286. console.log("preload finished to " + loadedPages + " pages");
  287. }else{
  288. loadedPages++;
  289. }
  290. $('<img>').attr('src',match[1]+("0000"+loadedPages).slice(-5) + "." + match[3]).on('load',function(){
  291. finished++;
  292. finishedPages++;
  293. if(finished == preloadNum || finishedPages == totalPages){
  294. loading = false;
  295. showStatus("load finished",true);
  296. }else{
  297. showStatus('Loading ('+finishedPages+ '/' + totalPages + ')',true);
  298. }
  299. updateStatus();
  300. });
  301. if(i < preloadNum){
  302. i++;
  303. }else{
  304. clearInterval(id); //idをclearIntervalで指定している
  305. console.log("preload finished to " + loadedPages + " pages");
  306. }
  307. }, intervalTime);
  308. }else{
  309. for(var i = 1;i <= preloadNum;i++){
  310. if(loadedPages+1 > totalPages){
  311. break;
  312. }else{
  313. loadedPages++;
  314. }
  315. $('<img>').attr('src',match[1]+("0000"+loadedPages).slice(-5) + "." + match[3]).on('load',function(){
  316. finished++;
  317. finishedPages++;
  318. if(finished == preloadNum|| finishedPages == totalPages){
  319. loading = false;
  320. showStatus("load finished",true); }else{
  321. showStatus('Loading ('+finishedPages+ '/' + totalPages + ')',true);
  322. }
  323. updateStatus();
  324. });
  325. }
  326. }
  327. }
  328.  
  329. }
  330.  
  331. function reloadImages(){
  332. if(!loading){
  333. loading = true;
  334. var currentTotal = loadedPages;
  335. loadedPages = 0;
  336. var finished = 0;
  337. finishedPages = 0;
  338. var url = $('#thumbnail1').attr('src');
  339. var match = url.match(/(http.+\/)(\d{5})\.(jpg|jpeg|gif|png)/i);
  340. var index = parseInt(match[2]);
  341. if(interval){
  342. var i = 1;
  343. var id = setInterval(function(){
  344. if(loadedPages+1 > totalPages){
  345. clearInterval(id); //idをclearIntervalで指定している
  346. }else{
  347. loadedPages++;
  348. }
  349. $('<img>').attr('src',match[1]+("0000"+loadedPages).slice(-5) + "." + match[3]).on('load',function(){
  350. finished++; finishedPages++;
  351. if(finishedPages == currentTotal){
  352. loading = false;
  353. console.log("reload finished:" + "~" + currentTotal);
  354. showStatus("reload finished",true);
  355. }else{
  356. showStatus('Reloading ('+finishedPages+ '/' + currentTotal + ')',true);
  357. }
  358. updateStatus();
  359. });
  360. if(i < currentTotal){
  361. i++;
  362. }else{
  363. clearInterval(id); //idをclearIntervalで指定している
  364. }
  365. }, intervalTime);
  366. }else{
  367. for(var i = 1;i <= currentTotal;i++){
  368. if(loadedPages+1 > totalPages){
  369. break;
  370. }else{
  371. loadedPages++;
  372. }
  373. $('<img>').attr('src',match[1]+("0000"+loadedPages).slice(-5) + "." + match[3]).on('load',function(){
  374. finished++; finishedPages++;
  375. if(finishedPages == currentTotal){
  376. loading = false;
  377. console.log("reload finished:" + "~" + currentTotal);
  378. showStatus("reload finished",true);
  379. }else{
  380. showStatus('Reloading ('+finishedPages+ '/' + currentTotal + ')',true);
  381. }
  382. updateStatus();
  383. });
  384. }
  385. }
  386. }
  387.  
  388. }
  389.  
  390.  
  391. // Your code here...
  392. $(window).on('scroll',function(e) {
  393. e.stopPropagation();
  394. e.preventDefault();
  395. if($(window).scrollLeft() > 0){
  396. $(window).scrollLeft(0);
  397. }
  398.  
  399. });
  400.  
  401. function initSize(){
  402. imageWidth = $('#thumbnail1').width();
  403. imageHeight = $('#thumbnail1').height();
  404. imageRatio = imageWidth/imageHeight;
  405. onResize();
  406. }
  407.  
  408.  
  409.  
  410. function onResize() {
  411. if (timer !== false) {
  412. clearTimeout(timer);
  413. }
  414. timer = setTimeout(function () {
  415. // 何らかの処理
  416. windowWidth = $(window).outerWidth(true);
  417. windowHeight = $(window).outerHeight(true);
  418. modalWindow.css('width','100%').css('height','100%');
  419. windowRatio = windowWidth/windowHeight;
  420. if(windowRatio >= imageRatio){
  421. $('#thumbnail1').css('width',windowHeight*imageRatio);
  422. $('#thumbnail1').css('height',windowHeight);
  423. }else{
  424. $('#thumbnail1').css('width',windowWidth);
  425. $('#thumbnail1').css('height',windowWidth/imageRatio);
  426. }
  427. // statusBar.css('left',(windowWidth-statusBar.width())/2);
  428. }, 200);
  429. }
  430.  
  431. initSize();
  432.  
  433. window.addEventListener('resize',onResize);
  434.  
  435. $(window).on('keydown',function(e){
  436. var keycode = e.keyCode;
  437. switch(keycode){
  438. case 27:
  439. // esc
  440. if(fullscreen){
  441. hideOverlay();
  442. }else{
  443. showOverlay();
  444. }
  445. break;
  446. default:
  447. break;
  448. }
  449. if(fullscreen){
  450. switch(keycode){
  451. case 38:
  452. // ↑
  453. thumbPre(totalPages);
  454. return false;
  455. case 40:
  456. // ↓
  457. thumbNext(totalPages);
  458. return false;
  459. case 116:
  460. // F5
  461. reloadImages();
  462. return false;
  463. default:
  464. break;
  465. }
  466. }
  467. });
  468.  
  469. var mousewheelevent = 'onwheel' in document ? 'wheel' : 'onmousewheel' in document ? 'mousewheel' : 'DOMMouseScroll';
  470. $(document).on(mousewheelevent,function(e){
  471. var num = parseInt($('.wheel').text());
  472. if(fullscreen){
  473. e.preventDefault();
  474. var delta = e.originalEvent.deltaY ? -(e.originalEvent.deltaY) : e.originalEvent.wheelDelta ? e.originalEvent.wheelDelta : -(e.originalEvent.detail);
  475. if (delta < 0){
  476. thumbNext(totalPages);
  477. return false;
  478. } else {
  479. thumbPre(totalPages);
  480. return false;
  481. }
  482. }
  483. });
  484.  
  485. $('#page_num').on('DOMSubtreeModified propertychange',kd);
  486.  
  487. function kd(){
  488. var pageMatch = $('#page_num').text().match(/\((.+)\/(.+)\)/i);
  489. if(pageMatch != null){
  490. if(currentPage != pageMatch[1]){
  491. currentPage = pageMatch[1];
  492. updateStatus();
  493. if(currentPage >= loadedPages-(preloadNum/2) && loadedPages !== totalPages){
  494. preload();
  495. }
  496. }
  497. }
  498. }
  499.  
  500. })();