Add to Collection

2021-12-17 16:27:51

  1. // ==UserScript==
  2. // @name Add to Collection
  3. // @namespace Forsman.Tomas
  4. // @match https://www.thingiverse.com/*
  5. // @grant none
  6. // @version 1.0
  7. // @author -
  8. // @description 2021-12-17 16:27:51
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. // define a handler
  14. function doc_keyUp(e) {
  15.  
  16. // this would test for whichever key is 40 (down arrow) and the ctrl key at the same time
  17. if (e.ctrlKey && e.key === 'c') {
  18. // call your function to do the thing
  19. $("div.CollectThingWindow__hidden--OSA7G").removeClass("CollectThingWindow__hidden--OSA7G");
  20. }
  21. }
  22.  
  23. document.addEventListener('keyup', doc_keyUp, false);
  24.  
  25. // Extends the search results from 20 to 200 per page
  26. URLSearchParams.prototype._append = URLSearchParams.prototype.append;
  27. URLSearchParams.prototype.append = function append(k, v) {
  28. if(k==='per_page') v = 200;
  29. return this._append(k, v);
  30. };
  31.  
  32. // Add color to current page number
  33. $( document ).ready(function() {
  34. var pathname = window.location.pathname;
  35. var number = pathname.split(":")[1];
  36. $('.pagination-link:contains(' + number + ')').css("color","blue");
  37. });
  38.  
  39.  
  40. // No waiting for downloads
  41. waitForKeyElements ("div.ThingPage__sidebar--6ZDYI > div.SidebarMenu__sidebarMenu--3uBjd > div.SidebarMenu__sideMenuTop--3xCYh > div > a", noBullshit);
  42.  
  43. function noBullshit() {
  44. 'use strict';
  45.  
  46. const downloadElement = document.querySelector('div.ThingPage__sidebar--6ZDYI > div.SidebarMenu__sidebarMenu--3uBjd > div.SidebarMenu__sideMenuTop--3xCYh > div > a');
  47. downloadElement.href = window.location.href + '/zip';
  48. downloadElement.innerHTML = '<div class="button button-primary">DOWNLOAD</div>'
  49. };
  50.  
  51.  
  52. /*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
  53. that detects and handles AJAXed content.
  54.  
  55. Usage example:
  56.  
  57. waitForKeyElements (
  58. "div.comments"
  59. , commentCallbackFunction
  60. );
  61.  
  62. //--- Page-specific function to do what we want when the node is found.
  63. function commentCallbackFunction (jNode) {
  64. jNode.text ("This comment changed by waitForKeyElements().");
  65. }
  66.  
  67. IMPORTANT: This function requires your script to have loaded jQuery.
  68. */
  69. function waitForKeyElements (
  70. selectorTxt, /* Required: The jQuery selector string that
  71. specifies the desired element(s).
  72. */
  73. actionFunction, /* Required: The code to run when elements are
  74. found. It is passed a jNode to the matched
  75. element.
  76. */
  77. bWaitOnce, /* Optional: If false, will continue to scan for
  78. new elements even after the first match is
  79. found.
  80. */
  81. iframeSelector /* Optional: If set, identifies the iframe to
  82. search.
  83. */
  84. ) {
  85. var targetNodes, btargetsFound;
  86.  
  87. if (typeof iframeSelector == "undefined")
  88. targetNodes = $(selectorTxt);
  89. else
  90. targetNodes = $(iframeSelector).contents ()
  91. .find (selectorTxt);
  92.  
  93. if (targetNodes && targetNodes.length > 0) {
  94. btargetsFound = true;
  95. /*--- Found target node(s). Go through each and act if they
  96. are new.
  97. */
  98. targetNodes.each ( function () {
  99. var jThis = $(this);
  100. var alreadyFound = jThis.data ('alreadyFound') || false;
  101.  
  102. if (!alreadyFound) {
  103. //--- Call the payload function.
  104. var cancelFound = actionFunction (jThis);
  105. if (cancelFound)
  106. btargetsFound = false;
  107. else
  108. jThis.data ('alreadyFound', true);
  109. }
  110. } );
  111. }
  112. else {
  113. btargetsFound = false;
  114. }
  115.  
  116. //--- Get the timer-control variable for this selector.
  117. var controlObj = waitForKeyElements.controlObj || {};
  118. var controlKey = selectorTxt.replace (/[^\w]/g, "_");
  119. var timeControl = controlObj [controlKey];
  120.  
  121. //--- Now set or clear the timer as appropriate.
  122. if (btargetsFound && bWaitOnce && timeControl) {
  123. //--- The only condition where we need to clear the timer.
  124. clearInterval (timeControl);
  125. delete controlObj [controlKey]
  126. }
  127. else {
  128. //--- Set a timer, if needed.
  129. if ( ! timeControl) {
  130. timeControl = setInterval ( function () {
  131. waitForKeyElements ( selectorTxt,
  132. actionFunction,
  133. bWaitOnce,
  134. iframeSelector
  135. );
  136. },
  137. 300
  138. );
  139. controlObj [controlKey] = timeControl;
  140. }
  141. }
  142. waitForKeyElements.controlObj = controlObj;
  143. }