javdb resort

resort by number of raters,and auto load 10 pages

  1. // ==UserScript==
  2. // @name javdb resort
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description resort by number of raters,and auto load 10 pages
  6. // @author q27256751 -> metaphor-pump
  7. // @match https://javdb.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=javdb.com
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. let peoplePattern = /由(\d+)人/;
  16. let pagePattern = /page=(\d+)/;
  17. let movieList = $('.movie-list')[0]
  18. let maxPageOffset = 0;
  19. let nextPageButton = $('.pagination-next')
  20. let curPage = getCurPage()
  21. if (nextPageButton && nextPageButton.length) {
  22. nextPageButton = nextPageButton[0]
  23. maxPageOffset = 9;
  24. nextPageButton.href = getPageUrl(curPage+maxPageOffset+1)
  25. nextPageButton.textContent = `下${maxPageOffset + 1}页`
  26. }
  27.  
  28. //add sort button
  29. var sortButton = document.createElement('a');
  30. sortButton.className = 'button is-small';
  31. sortButton.textContent = '评分人数排序';
  32. sortButton.addEventListener('click', sortItem);
  33. $('.buttons.has-addons')[2].appendChild(sortButton);
  34.  
  35. //load page and sort
  36. for (let i = 0; i < maxPageOffset; i++) {
  37. let newPage = curPage + i + 1;
  38. let isLastPage = i == maxPageOffset - 1;
  39. loadPage(newPage, sortItem, isLastPage);
  40. }
  41.  
  42. function sortItem() {
  43. debugger;
  44. console.log(`item start sort`)
  45. let curList = $('.item', movieList);
  46. let allList = []
  47. for (let i of curList) {
  48. allList.push(i)
  49. }
  50. //计算评分人数
  51. for (let item of allList) {
  52. let score = $('.score', item)[0]
  53. let searchRes = peoplePattern.exec(score.textContent)
  54. let peopleCnt = searchRes[1]
  55. peopleCnt = Number.parseInt(peopleCnt)
  56. item.peopleCnt = peopleCnt
  57. score.innerHTML = score.innerHTML.replace(peoplePattern, '由<span style="color:red">$1</span>人')
  58. }
  59. allList.sort((a, b) => b.peopleCnt - a.peopleCnt)
  60. allList = allList.slice(0, 100)
  61. movieList.innerHTML = ""
  62. for (let item of allList) {
  63. movieList.append(item)
  64. }
  65. console.log(`item sorted`)
  66. }
  67.  
  68. function getCurPage() {
  69. let curPath = window.location.href;
  70. let searchRes = pagePattern.exec(curPath)
  71. if (searchRes && searchRes.length > 1) {
  72. return Number.parseInt(searchRes[1])
  73. }
  74. return 1
  75. }
  76.  
  77. function getPageUrl(page) {
  78. let curPath = window.location.href;
  79. let pageIdx = curPath.indexOf('page');
  80. if (pageIdx > 0) {
  81. return curPath.replace(pagePattern, `page=${page}`)
  82. } else {
  83. if (curPath.indexOf('?') > 0) {
  84. return curPath + `&page=${page}`
  85. } else {
  86. return curPath + `?page=${page}`
  87. }
  88. }
  89. }
  90.  
  91. //load and sort
  92. function loadPage(pageNum, sortItem, isLastPage) {
  93. let pageUrl = getPageUrl(pageNum)
  94. getdata(pageUrl, data => {
  95. let container = document.createElement('div');
  96. container.style.display = 'none'
  97. container.innerHTML = data
  98. let newList = $('.movie-list .item', container)
  99. if (newList && newList.length) {
  100. for (let i of newList) {
  101. movieList.append(i)
  102. }
  103. }
  104. if (isLastPage) {
  105. sortItem();
  106. }
  107. console.log(`load page:${page}`)
  108. })
  109. }
  110.  
  111. function getdata(pageUrl, cb) {
  112. $.get(pageUrl, function (data, status) {
  113. cb(data)
  114. });
  115. }
  116.  
  117. })();