TescoVegan

Always apply the vegan filter on tesco.com

  1.  
  2. // ==UserScript==
  3. // @name TescoVegan
  4. // @author Ivy9217
  5. // @version 1.0.6
  6. // @description Always apply the vegan filter on tesco.com
  7. // @match https://www.tesco.com/groceries/*
  8. // @exclude /https://www.tesco.com/groceries/en-GB/(products|trolley|slots|orders).*/
  9. // @connect tesco.com
  10. // @run-at document-end
  11. // @namespace https://greasyfork.org/users/982144
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. // Content on the same page changes dynamically.
  16. // It's not enough to change the page URL after load, we must use this instead
  17. function observeDOM(callback) {
  18. var mutationObserver = new MutationObserver(function(mutations) {
  19. mutations.forEach(function(mutation) {
  20. callback(mutation)
  21. });
  22. });
  23.  
  24. mutationObserver.observe(document.body, {
  25. attributes: true,
  26. childList: true,
  27. subtree: true,
  28. attributeFilter: ["class"]
  29. });
  30. }
  31.  
  32. function appendUrlParameters(mutation) {
  33.  
  34. var oldUrl = window.location.search;
  35.  
  36. // Check if the filter is already applied
  37. if (!/dietary=Vegan/.test(oldUrl)) {
  38. var newURL = window.location.protocol + "//" +
  39. window.location.host +
  40. window.location.pathname + "?dietary=Vegan&viewAll=dietary" +
  41. window.location.search.replace('?', '&') +
  42. window.location.hash;
  43. window.location.replace(newURL);
  44. }
  45. }
  46.  
  47.  
  48. function run() {
  49. observeDOM(appendUrlParameters);
  50. }
  51.  
  52. setTimeout(run, 1000);
  53.