All tables sortable

Make all tables on any page sortable by converting them to dataTables

  1. // ==UserScript==
  2. // @name All tables sortable
  3. // @namespace https://gf.qytechs.cn/ru/users/303426
  4. // @version 1.3
  5. // @description Make all tables on any page sortable by converting them to dataTables
  6. // @author Титан
  7. // @match *://*/*
  8. // @icon data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgZmlsbD0iI2ZmZiIgY2xhc3M9ImJpIGJpLXRhYmxlIiB2aWV3Qm94PSIwIDAgMTYgMTYiPgogIDxwYXRoIGQ9Ik0wIDJhMiAyIDAgMCAxIDItMmgxMmEyIDIgMCAwIDEgMiAydjEyYTIgMiAwIDAgMS0yIDJIMmEyIDIgMCAwIDEtMi0yVjJ6bTE1IDJoLTR2M2g0VjR6bTAgNGgtNHYzaDRWOHptMCA0aC00djNoM2ExIDEgMCAwIDAgMS0xdi0yem0tNSAzdi0zSDZ2M2g0em0tNSAwdi0zSDF2MmExIDEgMCAwIDAgMSAxaDN6bS00LTRoNFY4SDF2M3ptMC00aDRWNEgxdjN6bTUtM3YzaDRWNEg2em00IDRINnYzaDRWOHoiLz4KPC9zdmc+
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
  10. // @grant GM_registerMenuCommand
  11. // @require https://cdn.jsdelivr.net/npm/datatables.net@1.13.6/js/jquery.dataTables.min.js
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17. let autolaunch = false;
  18.  
  19. function ConvertTables() {
  20. if (document.querySelector("head > link[href=\"https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css\"]") == null)
  21. $('head').append('<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">'); // add dataTAbles css
  22.  
  23.  
  24. let tables = $('table')
  25.  
  26. for (let i = 0; i < tables.length; i++) {
  27. try {
  28. ConvertTable_ToDataTable(tables[i]);
  29. } catch (e) {
  30. console.log(`[All tables sortable] table ${i}/${tables.length} error: ${e}`);
  31. console.log(tables[i]);
  32. }
  33. }
  34. }
  35.  
  36. function ConvertTable_ToDataTable(table) {
  37. if (!table.querySelector("thead")) {
  38. ConvertHeadlessTable_ToHeadedTable(table);
  39. }
  40.  
  41. $(table).DataTable({
  42. "paging": false,
  43. });
  44. }
  45.  
  46. function ConvertHeadlessTable_ToHeadedTable(table) {
  47. let tbody = table.querySelector("tbody");
  48. if (tbody && tbody.rows.length > 0) {
  49. let thead = document.createElement("thead");
  50. let firstRow = tbody.rows[0]; // first row = header
  51. thead.appendChild(firstRow); // move it to <thead>
  52. table.insertBefore(thead, tbody); // add <thead> before <tbody>
  53. }
  54. }
  55.  
  56. GM_registerMenuCommand('Convert Tables', () => {
  57. ConvertTables();
  58. });
  59.  
  60.  
  61. $(document).ready(function() {
  62. window.alert = (function() { // suppress DataTables alert
  63. let nativeAlert = window.alert;
  64. return function(message) {
  65. //window.alert = nativeAlert;
  66. message.indexOf("DataTables warning") === 0 ?
  67. console.warn(message) :
  68. nativeAlert(message);
  69. }
  70. })();
  71.  
  72. if (autolaunch) {
  73. ConvertTables();
  74. }
  75.  
  76. });
  77.  
  78. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址