Advertisement
jcunews

lazada.vn-add-search-result-filters.user.js

Apr 29th, 2024
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         lazada.vn add search result filters
  3. // @namespace    https://greasyfork.org/en/users/85671-jcunews
  4. // @version      0.0.1
  5. // @license      AGPL v3
  6. // @author       jcunews
  7. // @description  Context: https://www.reddit.com/r/userscripts/comments/1c67l08/request_search_filter_for_lazadavn/
  8. // @match        https://www.lazada.vn/*
  9. // @grant        none
  10. // ==/UserScript==
  11.  
  12. ((tm, xs) => {
  13.   xs = /(\d+(?:\.\d+)?)(.)? sold/;
  14.   function filter(vms, vxw) {
  15.     vms = parseInt(ms.value.trim());
  16.     vxw = (vxw = xw.value.trim()) && vxw.toLowerCase().replace(/\s+/g, " ").split(" ");
  17.     document.querySelectorAll('div[data-item-id]').forEach((item, el, m, s, w) => {
  18.       if (!isNaN(vms)) {
  19.         if ((el = item.querySelector(':scope>div>div>div>div>span>span:first-child')) && (m = el.textContent.match(xs))) {
  20.           switch (m[2]) {
  21.             case "M": s = parseFloat(m[1]) * 1000000; break;
  22.             case "K": s = parseFloat(m[1]) * 1000; break;
  23.             default : s = parseFloat(m[1])
  24.           }
  25.           s = s >= vms
  26.         }
  27.       } else s = true;
  28.       if (vxw) {
  29.         if (el = item.querySelector(':scope>div>div>div>div>a:not(:has(*))')) {
  30.           el = el.textContent.toLowerCase();
  31.           w = vxw.every(w => !el.includes(w))
  32.         }
  33.       } else w = true;
  34.       item.style.display = s && w ? "" : "none"
  35.     })
  36.   }
  37.   function process(a) {
  38.     if (!window.flt && window.q) {
  39.       q.parentNode.style.display = "flex";
  40.       q.style.position = "static";
  41.       q.parentNode.insertAdjacentHTML("beforeend", `<style>
  42.   #ms,#xw { border:2px solid #ccc; padding:0 .3em; width:5.2em; font-size:11pt }
  43.   #xw { width:7.5em }
  44.   #flt { display:flex; margin-right:45px; border:2px solid #ccc; cursor:pointer }
  45.   #flt:hover { background:#ddd }
  46.   #flt>div { margin:auto; width:1.8em }
  47.   #flt>div>div { margin:2px auto; border-top:2px solid #555; width:1.2em }
  48.   #flt>div>div:nth-child(2) { width:.8em }
  49.   #flt>div>div:nth-child(3) { width:.4em }
  50. </style><input id=ms placeholder="Min. sold"><input id=xw placeholder="Exclude words"><div id=flt><div><div></div><div></div><div></div></div></div>`);
  51.       ms.oninput = () => ms.style.backgroundColor = !ms.value.trim() || /^\s*([-+]?\d+)\s*$/.test(ms.value) ? "" : "#fdd";
  52.       ms.onblur = m => (m = ms.value.match(/^\s*([-+]?\d+)\s*$/)) && (ms.value = parseInt(m[1]));
  53.       xw.onblur = () => xw.value = xw.value.trim();
  54.       flt.onclick = () => {
  55.         if (ms.style.backgroundColor) return alert("Invalid integer number for sold items.");
  56.         filter()
  57.       }
  58.     }
  59.     if (window.flt) filter()
  60.   }
  61.   (new MutationObserver(() => {
  62.     clearTimeout(tm);
  63.     tm = setTimeout(process, 200)
  64.   })).observe(document.body, {childList: true, subtree: true});
  65.   process();
  66. })()
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement