Greasy Fork is available in English.

Deku Deals - Additional Filter

Add sale type filters to all pages

  1. // ==UserScript==
  2. // @name Deku Deals - Additional Filter
  3. // @namespace MKScripts
  4. // @match https://www.dekudeals.com/*
  5. // @grant none
  6. // @version 2.8
  7. // @author MKScripts
  8. // @description Add sale type filters to all pages
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. // Common badge selector
  15. const spanBadgeWarningSelector = '.badge-warning';
  16.  
  17. // Find the new injection element
  18. const injectionElement = document.querySelector('body > main > div.d-flex.flex-md-nowrap.flex-wrap > div.search-left');
  19.  
  20. if (injectionElement) {
  21. // Create a container div for the radio buttons
  22. const radioContainer = document.createElement('div');
  23. radioContainer.style.display = 'block'; // Set to block for vertical layout
  24. radioContainer.style.marginBottom = '20px'; // Make room below the div
  25.  
  26. // Create radio buttons
  27. const radioButtons = [
  28. createRadioButton('Show All', 'showAll', showAll),
  29. createRadioButton('Regular Sale', 'regularSale', regularSale),
  30. createRadioButton('Matches Previous Low', 'matchesPreviousLow', matchesPreviousLow),
  31. createRadioButton('Lowest Price Ever', 'lowestPriceEver', lowestPriceEver)
  32. ];
  33.  
  34. // Set "Show All" as checked by default
  35. radioButtons[0].input.checked = true;
  36.  
  37. // Append radio buttons to the container
  38. radioButtons.forEach(({ container }) => radioContainer.appendChild(container));
  39.  
  40. // Insert the container into the new injection point
  41. injectionElement.insertAdjacentElement('afterbegin', radioContainer);
  42.  
  43. // See if we are on a subsequent page where we need to filter:
  44. const urlParams = new URLSearchParams(window.location.search);
  45. const filterValue = urlParams.get('MKScriptsFilter') || 'showAll'; // Default to 'showAll'
  46. if (filterValue !== 'showAll') setURLfilter ( filterValue );
  47. }
  48.  
  49. function setURLfilter ( filterValue ) {
  50. // Get all radio buttons with the name "dealType"
  51. const radioButtons = document.querySelectorAll('input[name="dealType"]');
  52.  
  53. //Find the radio button with the matching value, set it, and call the sub
  54. //set up values and functions to call
  55. const filterFunctions = {
  56. regularSale: regularSale,
  57. matchesPreviousLow: matchesPreviousLow,
  58. lowestPriceEver: lowestPriceEver,
  59. showAll: showAll
  60. };
  61. radioButtons.forEach(radioButton => {
  62. if (radioButton.value === filterValue) {
  63. // Click the button
  64. radioButton.checked = true;
  65. if (filterFunctions[filterValue]) {
  66. filterFunctions[filterValue]();
  67. }
  68. }
  69. });
  70. }
  71.  
  72.  
  73. // Function to create a radio button
  74. function createRadioButton(label, value, handler) {
  75. const radioButton = document.createElement('input');
  76. radioButton.type = 'radio';
  77. radioButton.name = 'dealType';
  78. radioButton.value = value;
  79. radioButton.style.cursor = 'pointer';
  80.  
  81. const labelElement = document.createElement('label');
  82. labelElement.textContent = label;
  83. labelElement.style.marginLeft = '7px'; // Add margin-left to the labels
  84. labelElement.style.cursor = 'pointer';
  85.  
  86. // Append the radio button and label to a div
  87. const container = document.createElement('div');
  88. container.style.marginBottom = '0px'; // Spacing between radio buttons
  89. container.style.cursor = 'pointer';
  90. container.appendChild(radioButton);
  91. container.appendChild(labelElement);
  92.  
  93. // Attach event listener to the container
  94. container.addEventListener('click', function () {
  95. radioButton.checked = true;
  96. handler(value); // Pass the filter value to the handler
  97. });
  98.  
  99. return { input: radioButton, container };
  100. }
  101.  
  102. // Function to get the appropriate item selector and default display style
  103. function getViewModeConfig() {
  104. const listViewItems = document.querySelectorAll('.list-view');
  105. if (listViewItems.length > 0) {
  106. return { selector: '.list-view', display: 'flex' };
  107. }
  108. return { selector: '.d-block', display: 'block' };
  109. }
  110.  
  111. // Function to update pagination URLs
  112. function updatePaginationUrls(filterValue) {
  113. const paginationLinks = document.querySelectorAll('.pagination .page-link');
  114. paginationLinks.forEach(link => {
  115. const url = new URL(link.href, window.location.origin); // Parse the current URL
  116. url.searchParams.set('MKScriptsFilter', filterValue); // Add or update the parameter
  117. link.href = url.toString(); // Update the link
  118. });
  119. }
  120.  
  121. // Event listener for "Show All"
  122. function showAll() {
  123. const { selector, display } = getViewModeConfig();
  124. const divs = document.querySelectorAll(selector);
  125. divs.forEach(div => (div.style.display = display));
  126. updatePaginationUrls('showAll');
  127. }
  128.  
  129. // Event listener for "Regular Sale"
  130. function regularSale() {
  131. const { selector, display } = getViewModeConfig();
  132. const divs = document.querySelectorAll(selector);
  133. divs.forEach(div => {
  134. const badge = div.querySelector(spanBadgeWarningSelector);
  135. if (badge && (badge.textContent.includes('Lowest price ever') || badge.textContent.includes('Matches previous low'))) {
  136. div.style.setProperty('display', 'none', 'important');
  137. } else {
  138. div.style.display = display;
  139. }
  140. });
  141. updatePaginationUrls('regularSale');
  142. }
  143.  
  144. // Event listener for "Matches Previous Low"
  145. function matchesPreviousLow() {
  146. const { selector, display } = getViewModeConfig();
  147. const divs = document.querySelectorAll(selector);
  148. divs.forEach(div => {
  149. const badge = div.querySelector(spanBadgeWarningSelector);
  150. if (badge && badge.textContent.includes('Matches previous low')) {
  151. div.style.display = display;
  152. } else {
  153. div.style.setProperty('display', 'none', 'important');
  154. }
  155. });
  156. updatePaginationUrls('matchesPreviousLow');
  157. }
  158.  
  159. // Event listener for "Lowest Price Ever"
  160. function lowestPriceEver() {
  161. const { selector, display } = getViewModeConfig();
  162. const divs = document.querySelectorAll(selector);
  163. divs.forEach(div => {
  164. const badge = div.querySelector(spanBadgeWarningSelector);
  165. if (badge && badge.textContent.includes('Lowest price ever')) {
  166. div.style.display = display;
  167. } else {
  168. div.style.setProperty('display', 'none', 'important');
  169. }
  170. });
  171. updatePaginationUrls('lowestPriceEver');
  172. }
  173.  
  174. })();