Last active
March 6, 2024 03:00
-
-
Save dpw1/6db33f31472bc4bc096fc95c065c4e2e to your computer and use it in GitHub Desktop.
How to remove options from the collection filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
const REMOVE = `manual, best-selling`; | |
window.ezfyCollectionFilter = window.ezfyCollectionFilter || {}; | |
ezfyCollectionFilter = (function () { | |
function _isCartPage() { | |
return /cart/.test(window.location.href); | |
} | |
function isCollectionPage() { | |
const str = window.location.href; | |
return str.includes("/collections/") && !str.includes("products/"); | |
} | |
function _addStyle(styleString) { | |
const style = document.createElement("style"); | |
style.textContent = styleString; | |
document.head.append(style); | |
} | |
function _waitForElement(selector, delay = 50, tries = 100) { | |
const element = document.querySelector(selector); | |
if (!window[`__${selector}`]) { | |
window[`__${selector}`] = 0; | |
window[`__${selector}__delay`] = delay; | |
window[`__${selector}__tries`] = tries; | |
} | |
function _search() { | |
return new Promise((resolve) => { | |
window[`__${selector}`]++; | |
setTimeout(resolve, window[`__${selector}__delay`]); | |
}); | |
} | |
if (element === null) { | |
if (window[`__${selector}`] >= window[`__${selector}__tries`]) { | |
window[`__${selector}`] = 0; | |
return Promise.resolve(null); | |
} | |
return _search().then(() => _waitForElement(selector)); | |
} else { | |
return Promise.resolve(element); | |
} | |
} | |
async function removeOptions() { | |
const selector = `[id*='collection'] .select > select option`; | |
const $el = await _waitForElement(selector) | |
if (!$el){ | |
return; | |
} | |
var $select = document.querySelectorAll(selector); | |
var remove = REMOVE.split(",").map(e => e.toLowerCase().trim()); | |
for (var each of $select){ | |
var value = each.value; | |
var found = remove.filter(e => e === value)[0]; | |
if (found && found.length >= 1){ | |
each.remove(); | |
} | |
} | |
} | |
function initCode(){ | |
var e=["background: linear-gradient(-47deg,#8731e8,#4528dc)","border: 1px solid #3E0E02","color: white","display: block","text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3)","box-shadow: 0 1px 0 rgba(255, 255, 255, 0.4) inset, 0 5px 3px -5px rgba(0, 0, 0, 0.5), 0 -13px 5px -10px rgba(255, 255, 255, 0.4) inset","line-height: 40px","text-align: center","font-weight: bold","padding: 0px 5px"].join(";");function r(e){return(e+"").replace(/&#\d+;/gm,function(e){return String.fromCharCode(e.match(/\d+/gm)[0])})}var n=r(`Custom code by https://ezfycode.com`);console.log(`%c ${n}`,e); | |
} | |
return { | |
init: function () { | |
document.addEventListener("DOMContentLoaded", function () { | |
if(isCollectionPage()){ | |
removeOptions(); | |
} | |
}); | |
window.addEventListener("resize", function () {}); | |
window.addEventListener("load", function () { | |
initCode(); | |
if(isCollectionPage()){ | |
removeOptions(); | |
} | |
}); | |
window.addEventListener("scroll", function () {}); | |
}, | |
}; | |
})(); | |
ezfyCollectionFilter.init(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment