add out of stock toggle for spalena53

This commit is contained in:
Lukáš Kucharczyk 2023-03-29 10:30:38 +02:00
parent 8532af9fb3
commit 7916dda024
1 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,68 @@
// ==UserScript==
// @name New script - spalena53.cz
// @namespace Violentmonkey Scripts
// @match https://www.spalena53.cz/hledat/dostojevskij
// @grant none
// @version 1.0
// @author -
// @description 04/03/2023, 07:42:56
// ==/UserScript==
/**
* @param {string} type
* @param {object} props
* @param {...(string|HTMLElement)} children
* @return {HTMLElement}
*/
function elt(type, props, ...children) {
let dom = document.createElement(type);
if (props) Object.assign(dom, props);
for (let child of children) {
if (typeof child != "string") dom.appendChild(child);
else dom.appendChild(document.createTextNode(child));
}
return dom;
}
let bookElements = document.querySelectorAll(".product.book");
let outOfStockElements = Array.from(bookElements).filter(
(node) => node.querySelector(".availability_info.black") != null
);
/**
*
* @param {HTMLElement} element
*/
function toggleDisplay(element) {
if (element.style.display != "none") element.style.display = "none";
else element.style.display = "";
}
let toggleOutOfStockLabel = elt(
"label",
{
for: "out-of-stock",
className: "change_number",
},
"Toggle out of stock"
);
toggleOutOfStockLabel.cssText = `
height: 38px;
margin-right: 1rem;
`;
let toggleOutOfStockCheckbox = elt("input", {
onclick: (event) => {
outOfStockElements.forEach((element) => toggleDisplay(element));
},
id: "out-of-stock",
type: "checkbox",
className: "change_number",
});
toggleOutOfStockCheckbox.style.cssText = `
height: 33px;
`;
let pageHead = document.querySelector(".page_head");
pageHead.append(toggleOutOfStockLabel, toggleOutOfStockCheckbox);