// ==UserScript==
// @name Show price combinations list
// @namespace http://tampermonkey.net/
// @version 0.3
// @author Dimokin
// @match https://ru.aliexpress.com/item/*
// @match https://www.aliexpress.com/item/*
// @grant none
// @require http://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==
(function() {
'use strict';
var $ = window.jQuery;
$(document).ready(function() {
let startHTML = document.querySelector("html").outerHTML.split('window.runParams =')[1];
let endHTML = startHTML.split('var GaData')[0];
function strToObj(str) {
var obj = {};
if (str && typeof str === 'string') {
var objStr = str.match(/\{(.)+\}/g);
eval("obj =" + objStr);
}
return obj
}
let mainObject = strToObj(endHTML);
console.log('mainObject', mainObject);
mainObject.skuModule.skuPriceList.sort(function(a, b) {
let priceField = a.skuVal.actSkuMultiCurrencyCalPrice ? 'actSkuMultiCurrencyCalPrice' : 'skuMultiCurrencyCalPrice';
var keyA = parseFloat(a.skuVal[priceField]),
keyB = parseFloat(b.skuVal[priceField]);
if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
});
var div = document.createElement('div');
div.className = 'prices';
div.innerHTML += `
<button class="price-toggle">
Show/Hide
</div>
`;
$(div).append('<div class="price-list"></div>');
let pricesIndexes = [];
mainObject.skuModule.skuPriceList.forEach(function(element) {
let propsId = element.skuPropIds.split(',');
let availableCount = element.skuVal.availQuantity;
let propString = '';
let propIndex = [];
propsId.forEach(function(prop, index) {
mainObject.skuModule.productSKUPropertyList.forEach(function(listItem) {
listItem.skuPropertyValues.forEach(function(skuProp, skuIndex) {
if (prop == skuProp.propertyValueId && availableCount > 0) {
propString += skuProp.propertyValueDisplayName;
propIndex.push(skuIndex);
}
});
});
if (index !== propsId.length - 1) {
propString += ' - '
}
});
if (propIndex.length) {
pricesIndexes.push(propIndex);
}
let priceField = element.skuVal.actSkuMultiCurrencyCalPrice ? 'actSkuMultiCurrencyCalPrice' : 'skuMultiCurrencyCalPrice';
let price = element.skuVal[priceField]
var oldHtml = $(div).find('.price-list').html();
if (element.skuVal.availQuantity > 0) {
$(div).find('.price-list').html(oldHtml + `
<div class="price-row">
<div class="price-row__left">${propString} </div>
<div class="price-row__right">${price} ${mainObject.commonModule.currencyCode} <button type="button">BUY</button></div>
</div>
`);
}
});
var styles = `
.prices {
position: fixed;
top: 0;
right: 0;
background: white;
box-shadow: 1px -1px 4px 1px;
max-width: 40%;
max-height: 400px;
padding: 10px 20px;
overflow-y: auto;
overflow-x: hidden;
z-index:9999;
}
.price-list {
margin-top: 15px;
}
.price-row {
display:flex;
padding:3px;
margin-bottom:0;
justify-content: space-between;
cursor:pointer;
}
.price-row:hover,.price-row.active {
background: lightgrey;
}
.price-row__left {}
.price-row__right {
display: inline-flex;
min-width: 130px;
align-items: center;
justify-content: flex-end;
text-align: right;
margin-left: 20px;
width: 10%;
flex-shrink: 0;
}
.price-row__right button{
padding: 2px 8px;
background-color: #ff4747;
color: white;
border: none;
box-shadow: none;
margin-left: 5px;
}
.price-toggle {
display: block;
margin-right: auto;
cursor:pointer;
}`
var styleSheet = document.createElement("style")
styleSheet.type = "text/css"
styleSheet.innerText = styles
document.head.appendChild(styleSheet)
document.body.appendChild(div);
$(document).on("click", ".price-toggle", function() {
$(div).find('.price-list').toggle();
setTimeout($(window).trigger('resize'), 300);
});
let propertyLists = $('.product-sku').find('.sku-property-list');
pricesIndexes[0].forEach(function(price, priceIndex) {
for (var i = 0; i < propertyLists.length; i++) {
if (i === priceIndex) {
let propIndex = pricesIndexes[0][priceIndex];
let prop = $('.sku-property-list').eq(i).find('.sku-property-item').eq(propIndex)
if (prop.hasClass("selected")) {} else {
prop.trigger('click');
}
}
}
});
$('.price-row').eq(0).addClass('active');
$('.price-row').click(function(event) {
let index = $(this).index();
$('.price-row').each(function() {
if ($(this).hasClass("active")) {
$(this).removeClass('active');
}
});
$(this).addClass('active');
let propertyLists = $('.product-sku').find('.sku-property-list');
pricesIndexes[index].forEach(function(price, priceIndex) {
for (var i = 0; i < propertyLists.length; i++) {
if (i === priceIndex) {
let propIndex = pricesIndexes[index][priceIndex];
let prop = $('.sku-property-list').eq(i).find('.sku-property-item').eq(propIndex)
if (prop.hasClass("selected")) {} else {
prop.trigger('click');
}
}
}
});
});
$('.price-row button').click(function(event) {
setTimeout(function() {
$('.product-action .buynow').trigger('click')
}, 1000);
});
});
})();