LibraryThing ignore articles/punctuation on combine page

Don't take articles ("a", "an", "the", etc.) or punctuation into account when sorting works on the "Combine Works" pages

  1. // ==UserScript==
  2. // @name LibraryThing ignore articles/punctuation on combine page
  3. // @namespace https://gf.qytechs.cn/en/users/11592-max-starkenburg
  4. // @description Don't take articles ("a", "an", "the", etc.) or punctuation into account when sorting works on the "Combine Works" pages
  5. // @include http*://*librarything.tld/combine.php?*
  6. // @include http*://*librarything.com/combine.php?*
  7. // @exclude http*://*librarything.tld/combine.php?*&sort=1
  8. // @exclude http*://*librarything.com/combine.php?*&sort=1
  9. // @version 1
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. var articles = localStorage.ignoredArticles ? localStorage.ignoredArticles : "the,a,an";
  14. var table = document.getElementsByClassName("combinetable")[0];
  15. var rows = table.getElementsByTagName("tr");
  16. for (var i=0; i<rows.length; i++) {
  17. var row = rows[i];
  18. var sortTitle = row.getElementsByTagName("h2")[0].childNodes[1].textContent.toLowerCase();
  19. // Strip out anything that isn't a number, space, or alpha character (include diacritics)
  20. // Thanks to SO users Wak and Cœur here http://stackoverflow.com/a/11550799/5285945
  21. // as well as SO user bjornd here http://stackoverflow.com/a/6163180/5285945
  22. sortTitle = sortTitle.replace(/[^A-Za-z0-9 \u00C0-\u017F]/g,"").trim().replace(/\s+/g," ");
  23. // Strip out any articles
  24. var articlesArray = articles.split(",");
  25. for (var j=0; j<articlesArray.length; j++) {
  26. // Thanks to SO user Jason McCreary here http://stackoverflow.com/a/4029123/5285945
  27. var articleRE = new RegExp("^" + articlesArray[j].trim() + " ");
  28. sortTitle = sortTitle.replace(articleRE, "");
  29. }
  30. row.sortTitle = sortTitle;
  31. }
  32.  
  33. // Sort the titles with punctuation and articles removed
  34. // Thanks to SO user Oriol here http://stackoverflow.com/a/28311228/5285945
  35. var sortedRows = [].slice.call(rows).sort(function(a,b) {
  36. return a.sortTitle.localeCompare(b.sortTitle);
  37. })
  38.  
  39. // Reattach the works in their new order
  40. for (var k=0; k<sortedRows.length; k++) {
  41. table.getElementsByTagName("tbody")[0].appendChild(sortedRows[k]);
  42. }
  43.  
  44. /* The following lines are necessary because LT has some malformed HTML, basically something like:
  45. <div class="combineHead">
  46. <form>
  47. </div>
  48. <table .../>
  49. </form>
  50. So reattach the form to the "content" div, then reattach elements inside the form
  51. */
  52. var form = document.getElementsByName("works")[0];
  53. document.getElementsByClassName("content")[0].appendChild(form);
  54. var hr = document.getElementsByTagName("hr")[0];
  55. var h2 = form.getElementsByTagName("h2")[0];
  56. var buttons1 = form.getElementsByTagName("p")[0];
  57. var buttons2 = table.nextSibling;
  58. form.appendChild(buttons1);
  59. // detour for new feature (customize ignored words)
  60. var articlesInput = document.createElement("div");
  61. articlesInput.innerHTML = '<br/><div style="float: right;">\
  62. Sort should ignore: <input id="gm-articles-input" type="text" value="' + articles.replace(/,([^\s])/g,", $1") + '"/>\
  63. <input type="button" value="Update page" id="gm-articles-update" onclick="\
  64. localStorage.ignoredArticles = document.getElementById(\'gm-articles-input\').value; \
  65. setTimeout( function() { window.location.reload(false) }, 100);"/></div>';
  66. h2.style.marginTop = "-1em";
  67. form.appendChild(articlesInput);
  68. // end detour
  69. form.appendChild(h2);
  70. form.appendChild(hr);
  71. form.appendChild(table);
  72. if (buttons2 && buttons2.nodeType != 3) form.appendChild(buttons2);
  73. // Only adding the following because those who have the "LT better Combine Works button" script
  74. // end up having the size of the new box affected by the shenanigans in this script
  75. var newButtons = document.getElementById("gm-new-buttons");
  76. if (newButtons) {
  77. newButtons.style.fontSize = ".9em";
  78. }
  79.  
  80. // If "Enter" is typed while editing the ignored articles form, don't inadvertently submit the page's combine form
  81. document.getElementById("gm-articles-input").addEventListener("keyup", function(event) {
  82. event.preventDefault();
  83. if (event.keyCode == 13) {
  84. document.getElementById("gm-articles-update").click();
  85. }
  86. });
  87. document.getElementById("gm-articles-input").addEventListener("keydown", function(event) {
  88. if (event.keyCode == 13) {
  89. event.preventDefault();
  90. }
  91. });

QingJ © 2025

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