pagination-jump.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. (function () {
  2. "use strict";
  3. if (window.__ffkjPaginationJumpReady) return;
  4. window.__ffkjPaginationJumpReady = true;
  5. var paginationSelector = [
  6. ".news-pagination",
  7. ".inner-pagination",
  8. ".vc-pagination",
  9. ".edu-pagination",
  10. ".gjjx-pagination",
  11. ".laypage_main",
  12. "#layPage[data-total-items]"
  13. ].join(",");
  14. function toPositiveInt(value) {
  15. var number = parseInt(String(value || "").replace(/\D/g, ""), 10);
  16. return number > 0 ? number : 0;
  17. }
  18. function parsePageFromUrl(url) {
  19. try {
  20. return toPositiveInt(new URL(url, window.location.href).searchParams.get("page"));
  21. } catch (error) {
  22. return 0;
  23. }
  24. }
  25. function findActive(root) {
  26. return root.querySelector(".active, .is-active, .laypage_curr, .current, [aria-current='page']");
  27. }
  28. function findTotal(root) {
  29. var explicit = toPositiveInt(root.getAttribute("data-page-count"));
  30. if (explicit) return explicit;
  31. var totalItems = toPositiveInt(root.getAttribute("data-total-items"));
  32. var pageSize = toPositiveInt(root.getAttribute("data-page-size"));
  33. if (totalItems && pageSize) return Math.ceil(totalItems / pageSize);
  34. var summary = root.querySelector("[data-page-count], .page-summary, .laypage_total");
  35. if (summary) {
  36. explicit = toPositiveInt(summary.getAttribute("data-page-count"));
  37. if (explicit) return explicit;
  38. }
  39. var summaryMatch = (root.textContent || "").match(/共\s*(\d+)\s*页/);
  40. if (summaryMatch) return toPositiveInt(summaryMatch[1]);
  41. var total = 0;
  42. Array.prototype.forEach.call(root.querySelectorAll("[data-page], a[href]"), function (item) {
  43. total = Math.max(total, toPositiveInt(item.getAttribute("data-page")), parsePageFromUrl(item.getAttribute("href")));
  44. });
  45. Array.prototype.forEach.call(root.querySelectorAll("a[href]"), function (item) {
  46. if (/^(上一页|下一页|首页|尾页|«|»|<|>)$/.test((item.textContent || "").trim())) return;
  47. total = Math.max(total, toPositiveInt(item.textContent));
  48. });
  49. var active = findActive(root);
  50. if (active) {
  51. total = Math.max(total, toPositiveInt(active.getAttribute("data-page")) || toPositiveInt(active.textContent));
  52. }
  53. return total;
  54. }
  55. function findSummary(root) {
  56. return root.querySelector(".ffkj-page-summary, .page-summary, .edu-page-summary, .vc-page-summary");
  57. }
  58. function ensureSummary(root, total) {
  59. var summary = findSummary(root);
  60. if (summary) {
  61. if (!/共\s*\d+\s*页/.test(summary.textContent || "")) {
  62. summary.textContent = "共 " + total + " 页";
  63. }
  64. return;
  65. }
  66. if (/共\s*\d+\s*页/.test(root.textContent || "")) return;
  67. summary = document.createElement("span");
  68. summary.className = "ffkj-page-summary";
  69. summary.textContent = "共 " + total + " 页";
  70. root.insertBefore(summary, root.firstChild || null);
  71. }
  72. function normalizeSearchPage() {
  73. if (window.location.pathname.replace(/\/+$/, "") !== "/search") return;
  74. document.body.classList.add("ffkj-search-page");
  75. var sidebarSelectors = [
  76. ".department-column-nav",
  77. ".edu-sidebar",
  78. ".inner-side-panel",
  79. ".content-row > .content-left",
  80. ".news-sidebar",
  81. ".inner-sidebar",
  82. ".quick-nav",
  83. ".quick-navigation",
  84. ".side-nav"
  85. ].join(",");
  86. Array.prototype.forEach.call(document.querySelectorAll(sidebarSelectors), function (sidebar) {
  87. var layout = sidebar.parentElement;
  88. if (!layout) return;
  89. layout.classList.add("ffkj-search-single");
  90. sidebar.parentNode.removeChild(sidebar);
  91. });
  92. }
  93. function findCurrent(root, total) {
  94. var explicit = toPositiveInt(root.getAttribute("data-current-page"));
  95. if (explicit) return explicit;
  96. var active = findActive(root);
  97. if (active) {
  98. explicit = toPositiveInt(active.getAttribute("data-page")) || toPositiveInt(active.textContent);
  99. if (explicit) return explicit;
  100. }
  101. explicit = parsePageFromUrl(window.location.href);
  102. return explicit || 1;
  103. }
  104. function getAccent(root) {
  105. var themeVars = ["--red", "--brand", "--primary", "--theme-color", "--main-color", "--color-primary"];
  106. var candidates = [];
  107. var themeNode = root;
  108. while (themeNode) {
  109. var themeStyle = window.getComputedStyle(themeNode);
  110. for (var k = 0; k < themeVars.length; k += 1) {
  111. var themeValue = themeStyle.getPropertyValue(themeVars[k]).trim();
  112. addColorCandidate(candidates, themeValue, 120);
  113. }
  114. themeNode = themeNode.parentElement;
  115. }
  116. var themeSelectors = [".header-nav", ".main-nav", ".navbar", ".nav", "nav", ".header-wrap", ".header"];
  117. Array.prototype.forEach.call(document.querySelectorAll(themeSelectors.join(",")), function (item) {
  118. var navStyle = window.getComputedStyle(item);
  119. addColorCandidate(candidates, navStyle.backgroundColor, 80);
  120. addColorCandidate(candidates, navStyle.borderBottomColor, 60);
  121. addColorCandidate(candidates, navStyle.borderTopColor, 60);
  122. });
  123. var themeLinks = document.querySelectorAll(".header a, header a, .header-nav a, .main-nav a, .navbar a, nav a");
  124. Array.prototype.forEach.call(themeLinks, function (item) {
  125. var linkStyle = window.getComputedStyle(item);
  126. addColorCandidate(candidates, linkStyle.backgroundColor, 90);
  127. addColorCandidate(candidates, linkStyle.borderBottomColor, 50);
  128. addColorCandidate(candidates, linkStyle.color, 20);
  129. });
  130. var active = findActive(root);
  131. if (active) {
  132. var activeStyle = window.getComputedStyle(active);
  133. addColorCandidate(candidates, activeStyle.backgroundColor, 50);
  134. addColorCandidate(candidates, activeStyle.borderColor, 40);
  135. addColorCandidate(candidates, activeStyle.color, 30);
  136. }
  137. var firstLink = root.querySelector("a");
  138. if (firstLink) {
  139. var linkStyle = window.getComputedStyle(firstLink);
  140. addColorCandidate(candidates, linkStyle.borderColor, 20);
  141. addColorCandidate(candidates, linkStyle.backgroundColor, 20);
  142. addColorCandidate(candidates, linkStyle.color, 10);
  143. }
  144. var rootStyle = window.getComputedStyle(root);
  145. addColorCandidate(candidates, rootStyle.borderColor, 10);
  146. addColorCandidate(candidates, rootStyle.color, 5);
  147. candidates.sort(function (a, b) {
  148. return b.weight - a.weight;
  149. });
  150. if (candidates.length) return candidates[0].value;
  151. return "#0048AE";
  152. }
  153. function isUsableColor(value) {
  154. if (!value || value === "transparent" || value === "currentcolor") return false;
  155. if (/^rgba?\(/i.test(value)) {
  156. var parts = value.match(/[\d.]+/g) || [];
  157. if (parts.length > 3 && parseFloat(parts[3]) === 0) return false;
  158. if (parts.length >= 3) {
  159. var r = parseFloat(parts[0]);
  160. var g = parseFloat(parts[1]);
  161. var b = parseFloat(parts[2]);
  162. if ((r === 0 && g === 0 && b === 0) || (r === 255 && g === 255 && b === 255)) return false;
  163. }
  164. }
  165. return !window.CSS || !window.CSS.supports || window.CSS.supports("color", value);
  166. }
  167. function addColorCandidate(candidates, value, weight) {
  168. if (!isUsableColor(value)) return;
  169. var score = weight;
  170. if (/^rgba?\(/i.test(value)) {
  171. var parts = value.match(/[\d.]+/g) || [];
  172. if (parts.length >= 3) {
  173. var max = Math.max(parseFloat(parts[0]), parseFloat(parts[1]), parseFloat(parts[2]));
  174. var min = Math.min(parseFloat(parts[0]), parseFloat(parts[1]), parseFloat(parts[2]));
  175. var spread = max - min;
  176. var brightness = (max + min) / 2;
  177. if (spread > 80) score += 60;
  178. else if (spread > 25) score += 30;
  179. else if (spread > 8) score += 5;
  180. if (brightness > 245 && spread < 40) score -= 80;
  181. else if (brightness > 230 && spread < 80) score -= 30;
  182. }
  183. }
  184. candidates.push({ value: value, weight: score });
  185. }
  186. function getPageParam(root) {
  187. var explicit = root.getAttribute("data-page-param");
  188. if (explicit) return explicit;
  189. var container = root.closest && root.closest("#layPage[data-page-param]");
  190. if (container) return container.getAttribute("data-page-param");
  191. var scripts = document.getElementsByTagName("script");
  192. for (var i = 0; i < scripts.length; i += 1) {
  193. var source = scripts[i].textContent || "";
  194. if (!/laypage/i.test(source)) continue;
  195. var match = source.match(/\?(pageNo|currentPageNo|page|n)=/i);
  196. if (match) return match[1];
  197. }
  198. return "page";
  199. }
  200. function buildPageUrl(root, page) {
  201. var pageParam = getPageParam(root);
  202. var candidate = null;
  203. Array.prototype.forEach.call(root.querySelectorAll("a[href]"), function (link) {
  204. var href = link.getAttribute("href") || "";
  205. if (!candidate && href && !/^javascript:/i.test(href)) {
  206. try {
  207. if (new URL(href, window.location.href).searchParams.has(pageParam)) candidate = link.href;
  208. } catch (error) {}
  209. }
  210. });
  211. var url = new URL(candidate || window.location.href, window.location.href);
  212. url.pathname = url.pathname.replace(/;jsessionid=[^/?#]*/i, "");
  213. url.searchParams.set(pageParam, String(page));
  214. return url.href;
  215. }
  216. function enhance(root) {
  217. if (!root) return;
  218. if (root.querySelector(".ffkj-page-jump")) return;
  219. var legacyContainer = root.matches(".laypage_main") && root.closest("#layPage");
  220. if (legacyContainer && legacyContainer.querySelector(".ffkj-page-jump")) return;
  221. var total = findTotal(root);
  222. var current = findCurrent(root, total);
  223. var accent = getAccent(root);
  224. root.classList.add("pagination-jump-processed");
  225. root.style.setProperty("--ffkj-page-jump-accent", accent);
  226. if (total <= 1) return;
  227. ensureSummary(root, total);
  228. var form = document.createElement("form");
  229. form.className = "ffkj-page-jump";
  230. form.setAttribute("role", "search");
  231. form.setAttribute("aria-label", "页码跳转");
  232. form.setAttribute("novalidate", "");
  233. form.style.setProperty("--ffkj-page-jump-accent", accent);
  234. var label = document.createElement("label");
  235. label.textContent = "跳至";
  236. var input = document.createElement("input");
  237. input.type = "number";
  238. input.inputMode = "numeric";
  239. input.min = "1";
  240. input.max = String(total);
  241. input.value = String(current);
  242. input.setAttribute("aria-label", "输入页码,共 " + total + " 页");
  243. var suffix = document.createElement("span");
  244. suffix.textContent = "页";
  245. var submit = document.createElement("button");
  246. submit.type = "submit";
  247. submit.textContent = "确定";
  248. form.appendChild(label);
  249. form.appendChild(input);
  250. form.appendChild(suffix);
  251. form.appendChild(submit);
  252. form.addEventListener("submit", function (event) {
  253. event.preventDefault();
  254. var page = toPositiveInt(input.value);
  255. if (!page || page > total) {
  256. input.setAttribute("aria-invalid", "true");
  257. input.focus();
  258. return;
  259. }
  260. input.removeAttribute("aria-invalid");
  261. window.location.assign(buildPageUrl(root, page));
  262. });
  263. root.appendChild(form);
  264. root.setAttribute("data-ffkj-page-jump", "done");
  265. }
  266. function scan() {
  267. Array.prototype.forEach.call(document.querySelectorAll(paginationSelector), enhance);
  268. }
  269. function ready(fn) {
  270. if (document.readyState === "loading") {
  271. document.addEventListener("DOMContentLoaded", fn);
  272. } else {
  273. fn();
  274. }
  275. }
  276. function loadStyle() {
  277. if (document.querySelector("link[data-ffkj-page-jump-style]")) return;
  278. var link = document.createElement("link");
  279. link.rel = "stylesheet";
  280. link.href = "/main/ffkj-portal/pagination-jump.css?v=20261128-search-pages-r1";
  281. link.setAttribute("data-ffkj-page-jump-style", "true");
  282. document.head.appendChild(link);
  283. }
  284. loadStyle();
  285. ready(function () {
  286. normalizeSearchPage();
  287. scan();
  288. if (!window.MutationObserver) return;
  289. var timer = 0;
  290. new MutationObserver(function () {
  291. window.clearTimeout(timer);
  292. timer = window.setTimeout(scan, 30);
  293. }).observe(document.body, { childList: true, subtree: true });
  294. });
  295. })();