// ==UserScript==
// @name Show price combinations list - buttons
// @namespace http://tampermonkey.net/
// @version 0.5
// @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);
console.log('mainObject 1', mainObject.skuModule.skuPriceList);
mainObject.skuModule.skuPriceList.sort(function(a, b) {
let actPriceField = a.skuVal.actSkuMultiCurrencyCalPrice ? 'actSkuMultiCurrencyCalPrice' : 'skuMultiCurrencyCalPrice';
let defaultPriceField = a.skuVal.actSkuMultiCurrencyCalPrice ? 'actSkuMultiCurrencyBulkPrice' : 'actSkuBulkCalPrice';
var keyA = parseFloat(a.skuVal[actPriceField]),
keyB = parseFloat(b.skuVal[actPriceField]),
keyC = parseFloat(a.skuVal[defaultPriceField]),
keyD = parseFloat(b.skuVal[defaultPriceField]);
if (keyA == keyB) {
return (keyC > keyD) ? -1 : (keyA < keyD) ? 1 : 0;
} else {
return (keyA < keyB) ? -1 : 1;
}
});
console.log('mainObject 2', mainObject.skuModule.skuPriceList);
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 = [];
if (propsId.length) {
propsId.forEach(function(prop, index) {
if (mainObject.skuModule.productSKUPropertyList && mainObject.skuModule.productSKUPropertyList.length) {
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 class="buy" type="button">BUY</button> <button class="cart" type="button">CART</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: 190px;
align-items: center;
justify-content: flex-end;
text-align: right;
margin-left: 20px;
width: 10%;
flex-shrink: 0;
}
.price-row__right .buy{
padding: 2px 6px;
background-color: #ff4747;
color: white;
border: none;
box-shadow: none;
margin-left: 5px;
}
.price-row__right .cart{
padding: 2px 6px;
background-color: #ff9a00;
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');
if (pricesIndexes.length) {
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 .buy').click(function(event) {
setTimeout(function() {
$('.product-action .buynow').trigger('click')
}, 1000);
});
$('.price-row .cart').click(function(event) {
setTimeout(function() {
$('.product-action .addcart').trigger('click')
}, 1000);
});
});
})();