MercadoLivre Filter

Filtre palavras na pesquisa do mercadolivre. Filter words when searching in mercadolivre.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         MercadoLivre Filter
// @namespace    https://github.com/argit2/mercadolivre-filter
// @version      0.3
// @description  Filtre palavras na pesquisa do mercadolivre. Filter words when searching in mercadolivre.
// @author       You
// @include      *mercadolivre.com.br*
// @grant        none
// ==/UserScript==

/*
Instruções

Para filtrar a palavra "azul", adicione "-azul" à sua pesquisa no site.
Exemplo: pesquise por "mochila -azul"

Instructions

To filter the word "blue", add "-blue" to your search in the site.
Example: search for "backpack -blue"
*/

var searchQuery = "";
var toFilter = [];
var linkEnd = "";

function getToFilter() {
    let address = document.location.href.replaceAll("%20", " ");
    linkEnd = "#" + address.split("#")[1];
    if (! linkEnd) {
        return;
    }
    searchQuery = linkEnd.split("[")[1].slice(2, -1);
    toFilter = searchQuery.split(" ").filter(x => {
        return x[0] === "-"; // takes part of the query that begin with -
    }).map(x => x.slice(1));
    toFilter = toFilter.map(x => x.toLowerCase());
}

function fixSearchBar() {
    if (! searchQuery) {
        return;
    }
    let searchBar = document.querySelector("input.nav-search-input");
    searchBar.value = searchQuery;
}

function fixLink() {
    let address = document.location.href;
    let parts = address.split("#");
    toFilter.forEach( f => {
        parts[0] = parts[0].replace(f, "");
    })
    newAddress = parts[0] + "#" + parts[1];
    if (address == newAddress){
        return;
    }
    document.location.href = newAddress;
}

function filter (elem) {
    if (! elem) {
        return;
    }
    let titleElem = elem.querySelector(".ui-search-item__title")
    if (! titleElem) {
        return;
    }
    //console.log(toFilter);
    let title = titleElem.innerText.toLowerCase();
    //console.log(title);
    toFilter.forEach( f => {
        if (title.includes(f) && elementExists(elem)) {
            console.log(`Removing ${f}: ${title}`);
            elem.style.display = "none";
        }
    })
}

function elementExists(elem)
{
    if (! elem) {
        return;
    }
    return elem && elem.style.visibility != "hidden" && elem.style.display !== "none";
}

function filterAll() {
    if (! toFilter) {
        return;
    }
    let elements = document.querySelectorAll(".ui-search-layout__item");
    elements.forEach(x => filter(x));
}

function replaceLinks() {
    if (! linkEnd) {
        return;
    }
    let links = Array.from(document.querySelectorAll("a"));
    links.forEach( x => {
        x.href = x.href + linkEnd;
    })
}

getToFilter();
fixLink();
fixSearchBar();
setInterval(filterAll, 1000);
replaceLinks();