Thingiverse - Multifix

Fixes multiple Thingiverse site bugs, and makes the site a bit easier to use. See source for details

  1. // ==UserScript==
  2. // @name Thingiverse - Multifix
  3. // @namespace https://greasyfork.org/users/77886
  4. // @version 0.3
  5. // @description Fixes multiple Thingiverse site bugs, and makes the site a bit easier to use. See source for details
  6. // Features and Fixes:
  7. // Fix: Removes collections with a NULL description from Thingiverse API requests, which can cause the infamous "Something went wrong" response
  8. // Fix: Returns up to 9,999 collections so that you can assign a thing to any of your collections (instead of the 20 most recent)
  9. // Feature: Sort collections by name (instead of by Popularity or Newest)
  10. // Feature: Return 100 search results (instead of 20)
  11. // @author muchtall
  12. // @license MIT
  13. // @match https://www.thingiverse.com/*
  14. // @require https://cdn.jsdelivr.net/npm/xhook@1.4.9/dist/xhook.min.js
  15. // @grant none
  16. // /grant GM_getValue // If I enable this, then something breaks XHook when grant is no longer in the "none" state. So leaving this out for now.
  17. // @run-at document-start
  18. // ==/UserScript==
  19.  
  20. var fix_borked_collections = true;
  21. var fix_missing_collections = true;
  22. var more_search_results = true;
  23. var sort_collections = true;
  24.  
  25. // -- Disabled because I can't get GM_getValue to work without breaking XHook.
  26. // Get user's perferences on what features are enabled. All features are enabled by default.
  27. // To disable any of these features, set a Value in ViolentMonkey like so:
  28. // Key: more_search_results
  29. // Value: false
  30. //let fix_borked_collections = GM_getValue('fix_borked_collections', true);
  31. //let fix_missing_collections = GM_getValue('fix_missing_collections', true);
  32. //let more_search_results = GM_getValue('more_search_results', true);
  33. //let sort_collections = GM_getValue('sort_collections', true);
  34.  
  35. var userscriptname = 'Thingiverse - Multifix';
  36. console.log(userscriptname + ' - Started');
  37.  
  38. if ( fix_missing_collections ) {
  39. console.log(userscriptname + ' - Enabled feature: Fix Missing Collections');
  40. } else {
  41. console.log(userscriptname + ' - DISABLED feature: Fix Missing Collections');
  42. }
  43.  
  44. if ( more_search_results ) {
  45. console.log(userscriptname + ' - Enabled feature: More Search Results');
  46. } else {
  47. console.log(userscriptname + ' - DISABLED feature: More Search Results');
  48. }
  49.  
  50. if ( fix_borked_collections ) {
  51. console.log(userscriptname + ' - Enabled feature: Fix Borked Collections');
  52. } else {
  53. console.log(userscriptname + ' - DISABLED feature: Fix Borked Collections');
  54. }
  55.  
  56. if ( sort_collections ) {
  57. console.log(userscriptname + ' - Enabled feature: Sort Collections');
  58. } else {
  59. console.log(userscriptname + ' - DISABLED feature: Sort Collections');
  60. }
  61.  
  62. xhook.before(function(request) {
  63. if ( more_search_results ) {
  64. if(request.url.match(/https:\/\/api.thingiverse.com\/search\/\?.*/)) {
  65. console.log(userscriptname + ' - More Search Results - Old URL:' + request.url);
  66. request.url = request.url.replace(/per_page=[0-9]+/,"per_page=100");
  67. console.log(userscriptname + ' - More Search Results - New URL:' + request.url);
  68. }
  69. }
  70. if ( fix_missing_collections ) {
  71. if(request.url.match(/https:\/\/api.thingiverse.com\/users\/[^\/]+\/search\/\?.*type=collections.*/)) {
  72. console.log(userscriptname + ' - Fix Missing Collections - Old URL:' + request.url);
  73. request.url = request.url.replace(/per_page=[0-9]+/,"per_page=9999");
  74. console.log(userscriptname + ' - Fix Missing Collections - New URL:' + request.url);
  75. }
  76. }
  77. });
  78.  
  79. xhook.after(function(request, response) {
  80. if(request.url.match(/users\/[^\/]+\/search\/\?.*type=collections.*/)) {
  81. if ( fix_borked_collections ) {
  82. // --- Fix Borked Collections ---
  83. console.log(userscriptname + ' - Fix Borked Collections - Found URL: ' + request.url);
  84. var json = JSON.parse(response.text);
  85. var i=json.hits.length;
  86. while (i--) {
  87. if(json.hits[i].name === null){
  88. console.log(userscriptname + ' - Fix Borked Collections - Removed borked collection: ' + json.hits[i].id);
  89. json.hits.splice(i,1);
  90. } else {
  91. //console.log("Keeping good collection: " + json.hits[i].id);
  92. }
  93. }
  94. }
  95. if ( sort_collections ) {
  96. // --- Sort collections by Name ---
  97. json.hits.sort(function (a, b) {
  98. return a.name.localeCompare(b.name);
  99. });
  100. console.log(userscriptname + ' - Sorted Collections - Collections sorted by name');
  101. }
  102. // Send response to browser
  103. response.text = JSON.stringify(json);
  104. }
  105. });