Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name lazada.vn add search result filters
- // @namespace https://greasyfork.org/en/users/85671-jcunews
- // @version 0.0.1
- // @license AGPL v3
- // @author jcunews
- // @description Context: https://www.reddit.com/r/userscripts/comments/1c67l08/request_search_filter_for_lazadavn/
- // @match https://www.lazada.vn/*
- // @grant none
- // ==/UserScript==
- ((tm, xs) => {
- xs = /(\d+(?:\.\d+)?)(.)? sold/;
- function filter(vms, vxw) {
- vms = parseInt(ms.value.trim());
- vxw = (vxw = xw.value.trim()) && vxw.toLowerCase().replace(/\s+/g, " ").split(" ");
- document.querySelectorAll('div[data-item-id]').forEach((item, el, m, s, w) => {
- if (!isNaN(vms)) {
- if ((el = item.querySelector(':scope>div>div>div>div>span>span:first-child')) && (m = el.textContent.match(xs))) {
- switch (m[2]) {
- case "M": s = parseFloat(m[1]) * 1000000; break;
- case "K": s = parseFloat(m[1]) * 1000; break;
- default : s = parseFloat(m[1])
- }
- s = s >= vms
- }
- } else s = true;
- if (vxw) {
- if (el = item.querySelector(':scope>div>div>div>div>a:not(:has(*))')) {
- el = el.textContent.toLowerCase();
- w = vxw.every(w => !el.includes(w))
- }
- } else w = true;
- item.style.display = s && w ? "" : "none"
- })
- }
- function process(a) {
- if (!window.flt && window.q) {
- q.parentNode.style.display = "flex";
- q.style.position = "static";
- q.parentNode.insertAdjacentHTML("beforeend", `<style>
- #ms,#xw { border:2px solid #ccc; padding:0 .3em; width:5.2em; font-size:11pt }
- #xw { width:7.5em }
- #flt { display:flex; margin-right:45px; border:2px solid #ccc; cursor:pointer }
- #flt:hover { background:#ddd }
- #flt>div { margin:auto; width:1.8em }
- #flt>div>div { margin:2px auto; border-top:2px solid #555; width:1.2em }
- #flt>div>div:nth-child(2) { width:.8em }
- #flt>div>div:nth-child(3) { width:.4em }
- </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>`);
- ms.oninput = () => ms.style.backgroundColor = !ms.value.trim() || /^\s*([-+]?\d+)\s*$/.test(ms.value) ? "" : "#fdd";
- ms.onblur = m => (m = ms.value.match(/^\s*([-+]?\d+)\s*$/)) && (ms.value = parseInt(m[1]));
- xw.onblur = () => xw.value = xw.value.trim();
- flt.onclick = () => {
- if (ms.style.backgroundColor) return alert("Invalid integer number for sold items.");
- filter()
- }
- }
- if (window.flt) filter()
- }
- (new MutationObserver(() => {
- clearTimeout(tm);
- tm = setTimeout(process, 200)
- })).observe(document.body, {childList: true, subtree: true});
- process();
- })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement