Skip to content

Instantly share code, notes, and snippets.

@sleemanj
Last active February 21, 2019 11:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sleemanj/95035bf2c0141ac72420 to your computer and use it in GitHub Desktop.
Save sleemanj/95035bf2c0141ac72420 to your computer and use it in GitHub Desktop.
Return the Quantity Search on Aliexpress
// ==UserScript==
// @name Return the Quantity Search, Show Per Piece Pricing
// @namespace https://gist.github.com/sleemanj/95035bf2c0141ac72420
// @updateURL https://gist.github.com/sleemanj/95035bf2c0141ac72420/raw/aliexpress-qty-search-fix.js
// @downloadURL https://gist.github.com/sleemanj/95035bf2c0141ac72420/raw/aliexpress-qty-search-fix.js
// @version 1.1
// @description Put the quantity search fields back into the search interface on Aliexpress when they are not present and add a per-piece pricing when possible on the listing pages.
// @author James Sleeman
// @match *://*.aliexpress.com/*
// @exclude *://trade.aliexpress.com/orderList.htm*
// @grant GM_addStyle
// ==/UserScript==
(function(){
if(document.getElementById('filter-quantity-c')) return;
if(!document.getElementById('filter')) return;
var d = document.createElement('div');
d.innerHTML = '<div class="filter-quantity-c" data-widget-cid="widget-25" style="-webkit-user-select: text;">' +
'<span id="filter-quantity-c" style="-webkit-user-select: text;"><span class="ui-label">Quantity:</span>' +
' <input class="ui-textfield ui-textfield-system" value="" autocomplete="off" id="filter-quantity-from" name="minQuantity" tabindex="22" style="-webkit-user-select: text;"> ' +
' <span>-</span> ' +
' <input class="ui-textfield ui-textfield-system" value="" autocomplete="off" id="filter-quantity-to" name="maxQuantity" tabindex="23">' +
'<input type="submit" value="OK" />' +
' </span> ' +
'</div>';
document.getElementById('filter').appendChild(d);
if(document.location.search.match(/minQuantity=([0-9]+)/))
{
document.getElementById('filter-quantity-from').value = RegExp.$1;
}
if(document.location.search.match(/maxQuantity=([0-9]+)/))
{
document.getElementById('filter-quantity-to').value = RegExp.$1;
}
// Display per piece price
var items = document.getElementsByClassName('list-item');
console.log('Found ' + items.length + ' Items');
for(var x = 0; x < items.length; x++)
{
var PerLot = 1;
if(items[x].getElementsByClassName('min-order').length)
{
PerLot = parseInt(items[x].getElementsByClassName('min-order')[0].innerHTML.replace(/ pieces.*lot/,'').replace(/\s/g,''));
}
else if(items[x].getElementsByClassName('history-item product').length && items[x].getElementsByClassName('history-item product')[0].title.match(/([^0-9]|^)([0-9]+)\s*pcs/i))
{
PerLot = RegExp.$2;
}
if(PerLot > 1)
{
var PriceLot = items[x].getElementsByClassName('price')[0].getElementsByClassName('value')[0].innerHTML.replace(/[^0-9.]/g, '');
var ShippingPriceLot = 0;
if(items[x].getElementsByClassName('pnl-shipping').length)
{
ShippingPriceLot = parseFloat(items[x].getElementsByClassName('pnl-shipping')[0].getElementsByClassName('value')[0].innerHTML.replace(/[^0-9.]/g, ''));
}
var NewSpan = document.createElement('div');
NewSpan.style.fontWeight = 'bold';
NewSpan.style.fontSize = 'larger';
NewSpan.innerHTML = '( ' + ((parseFloat(PriceLot)+ShippingPriceLot)/parseInt(PerLot)).toFixed(3) + ' / Piece' + ( items[x].getElementsByClassName('min-order').length ? ' ' : '? ') + (ShippingPriceLot ? ' inc Shipping ':'') + ')';
items[x].getElementsByClassName('price-m')[0].appendChild(NewSpan);
}
}
})();
@jnogueira1
Copy link

Base on your work, made this.

with:
// @run-at document-start

window.addEventListener('beforescriptexecute', function(e) {
    var pos_div = document.getElementById('keyword-c');
    console.log(pos_div.innerHTML);

    if(pos_div && !document.getElementById('filter-quantity-c')) {
        var new_div = document.createElement('div');
        new_div.className = 'filter-quantity-c';
        new_div.innerHTML = '\
            <span id="filter-quantity-c"><span class="ui-label">Quantity:</span>\
                <input class="ui-textfield ui-textfield-system" value="" autocomplete="off" id="filter-quantity-from" name="minQuantity" tabindex="22" >\
                <span>-</span>\
                <input class="ui-textfield ui-textfield-system" value="" autocomplete="off" id="filter-quantity-to" name="maxQuantity" tabindex="23">\
            </span>\
            <a href="javascript:;" style="display:none" id="quantity-submit" class="ui-button narrow-go">OK</a>\
        ';
        pos_div.parentNode.insertBefore(new_div, pos_div.nextSibling);

        if(document.location.search.match(/minQuantity=([0-9]+)/)) { 
            document.getElementById('filter-quantity-from').value = RegExp.$1;
        }

        if(document.location.search.match(/maxQuantity=([0-9]+)/)) { 
            document.getElementById('filter-quantity-to').value = RegExp.$1;
        }
    }

    window.removeEventListener(e.type, arguments.callee, true);
}, true);

@Checkumeito
Copy link

Is it possible for you to add a price per piece check box into this great userscript? The search url parameter is &isUnitPrice=y (y or n for yes and no)

@FPeter84
Copy link

FPeter84 commented Nov 2, 2016

Hello!

First, thanks for Your efforts! But, I don't know if I missed something during import or what happened, but Your script does nothing in my current setup - Tampermonkey reports that "No script is running", even though im browsing on aliexpress's search page... :( So, is this script outdated and its normal, or did i miss some settings instead of importing it with defaults?

Best regards,
Peter

@wakawaka54
Copy link

Hey! I've got a project that returns a lot of the Search functionality back to AliExpress . It is a seperate site and I perform the search on our databases so that allows me to add more features. I have Quantity Search, Unit Price Display, and Shipping Type right now but I will be adding lots more 😃 , It's free to use!

Aliseeks - www.aliseeks.com

There is also an API which I can provide access to, just contact me for now if you want access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment