-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfahasa-highlight-flashsale-deals-based-on-discount.user.js
49 lines (43 loc) · 1.82 KB
/
fahasa-highlight-flashsale-deals-based-on-discount.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// ==UserScript==
// @name Fahasa Highlight Flashsale Deals Based on Discount
// @namespace metapone
// @version 2.1
// @description Highligh deals above a certain discount value
// @author metapone
// @license GPL-2.0-only; https://opensource.org/licenses/GPL-2.0
// @homepage https://github.com/metapone/userscript-collection
// @supportURL https://github.com/metapone/userscript-collection/issues
// @noframes
// @match *://www.fahasa.com/flashsale*
// @grant none
// ==/UserScript==
const threshold = 50; // Minimum sale to highlight
const color = "yellow"; // Highlight CSS color
const wishlistColor = "cyan"; // Wishlist highlight CSS color
const wishlist = ["Title 1", "Title 2"].map((x) => x.toLowerCase()); // Target book title (case insensitive)
function highlightSale(node) {
for (let item of node.querySelectorAll(".flashsale-item")) {
const discountNode = item.querySelector(".discount-l-fs");
if (!discountNode) continue;
const discount = parseInt(discountNode.innerText.replace(/^\D+/g, ''), 10);
if (discount >= threshold) item.firstChild.style.backgroundColor = color;
const targetNode = item.querySelector(".product-name-no-ellipsis > a");
if (!targetNode) continue;
const productTitle = targetNode.innerText.toLowerCase();
if (wishlist.some((x) => productTitle.includes(x))) item.firstChild.style.backgroundColor = wishlistColor;
}
}
function callback(mutationsList) {
for (let mutation of mutationsList) {
for (let addedNode of mutation.addedNodes) {
highlightSale(addedNode);
}
}
}
function onLoad() {
const targetNode = document.querySelector(".flashsale-page-period-content");
const observer = new MutationObserver(callback);
observer.observe(targetNode, { subtree: true, childList: true });
highlightSale(targetNode);
}
window.addEventListener("load", onLoad, false);