Advertisement
Kriptanik

Woolworths Australia Filter Script

Aug 23rd, 2024 (edited)
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name Woolworths Filter Script
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Automatically add &filter=SoldBy(Woolworths) to URLs on Woolworths' website
  6. // @author Kriptanik
  7. // @match https://www.woolworths.com.au/*
  8. // @icon https://www.woolworths.com.au/favicon.ico
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Check if the current page is a catalogue page
  16. if (window.location.href.startsWith("https://www.woolworths.com.au/shop/catalogue")) {
  17. return; // Exit the script if on a catalogue page
  18. }
  19.  
  20. const addFilterToURL = () => {
  21. let url = new URL(window.location.href);
  22.  
  23. // Check if we're in the /shop/ section and if the filter is missing
  24. if (url.pathname.startsWith('/shop/') && !url.searchParams.has('filter')) {
  25. // Manually construct the desired URL with the correct parameter format
  26. let query = url.searchParams.toString();
  27. query += (query ? '&' : '') + 'filter=SoldBy(Woolworths)';
  28. let newUrl = `${url.origin}${url.pathname}?${query}${url.hash}`;
  29.  
  30. // Redirect to the correctly formatted URL
  31. window.location.replace(newUrl);
  32. }
  33. };
  34.  
  35. // Run the function initially
  36. addFilterToURL();
  37.  
  38. // Use a MutationObserver to detect changes in the <body> tag (common in SPAs)
  39. const observer = new MutationObserver(() => {
  40. addFilterToURL();
  41. });
  42.  
  43. // Observe the entire document for changes
  44. observer.observe(document.body, { childList: true, subtree: true });
  45.  
  46. })();
Tags: Woolworths
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement