Commit 08961265 authored by Hubert Denkmair's avatar Hubert Denkmair

initial checkin

parents
Pipeline #223 failed with stages
in 56 seconds
function conrad_parse_cart()
{
return [...document.querySelectorAll(".cart-item-container")].map(
function (a)
{
var qty = parseInt(a.querySelector('.quantity-input-box').value);
var extended_price = parse_price(a.querySelector('.item-price').textContent);
var unit_price = (qty==1) ? extended_price : parse_price(a.querySelector('.item-unit-price').textContent.split(" ")[1]);
var link = a.querySelector('a.product-info-container');
return {
'oc': a.querySelector('.product-number').textContent.split(" ")[1],
'qty': qty,
'descr': a.querySelector('.product-title').textContent.trim(),
'unit_price': unit_price,
'extended_price': extended_price,
'link': link.protocol+"//"+link.host+link.pathname+link.search
};
}
);
}
function conrad_try_add_button()
{
var container = document.querySelector('.proceed-to-checkout-container');
if (container != null)
{
var btn = document.createElement('button');
btn.className = "large";
btn.style.backgroundColor = "#ffe600";
btn.style.minWidth = "140px";
btn.style.width = "100%";
btn.style.height = "50px";
btn.style.marginBottom = "10px";
btn.innerHTML = '<span>Sammelbestellung kopieren</span>';
btn.addEventListener('click', function () {
copy_cart_clipboard(conrad_parse_cart());
alert('In Zwischenablage kopiert.');
return false;
});
container.insertBefore(btn, container.firstChild);
//container.appendChild(btn);
}
else
{
window.setTimeout(conrad_try_add_button, 200);
}
}
conrad_try_add_button();
function digikey_parse_cart()
{
return [...document.querySelectorAll("#cartDetails tbody tr.detailRow:not(.subRow)")].map(
function (a)
{
console.log(a);
return {
'oc': a.querySelector('[id*="__PartNumber"]').value,
'qty': parseInt(a.querySelector("[id*=\"__Quantity\"]").value),
'descr': a.querySelector('[id*="__ManufacturerPartNumber"]').value,
'unit_price': parse_price(a.querySelector('[id*="__UnitPrice"]').value),
'extended_price': parse_price(a.querySelector('[id*="__ExtendedPriceDisplay"]').value),
'link': 'https:' + a.querySelector('[id*="__DetailUrl"]').value
};
}
);
}
function digikey_add_button(toolbar) {
var span = document.createElement('span');
span.className = "cart-tool-share";
span.innerHTML = '<a href="#"><span>Sammelbestellung kopieren</span></a>';
span.firstChild.addEventListener('click', function () {
copy_cart_clipboard(digikey_parse_cart());
alert('In Zwischenablage kopiert.');
return false;
});
toolbar.appendChild(span);
}
digikey_add_button(document.querySelector('.table-buttons'));
{
"manifest_version": 2,
"name": "bytewerk.org Sammelbestellungen",
"version": "1.0",
"description": "Wandelt Warenkörbe von Elektronikversendern in Mediawiki-Syntax",
"content_scripts": [
{
"matches": ["https://www.mouser.de/Cart/"],
"js": ["sammelbestellung.js", "mouser.js"]
},
{
"matches": ["https://www.reichelt.de/Warenkorb/*"],
"js": ["sammelbestellung.js", "reichelt.js"]
},
{
"matches": ["https://www.pollin.de/checkout/cart"],
"js": ["sammelbestellung.js", "pollin.js"]
},
{
"matches": ["https://www.digikey.de/ordering/shoppingcart*"],
"js": ["sammelbestellung.js", "digikey.js"]
},
{
"matches": ["https://www.conrad.de/de/cart.html"],
"js": ["sammelbestellung.js", "conrad.js"]
}
]
}
function mouser_parse_cart()
{
return [...document.querySelectorAll(".cart-body tr.grid-row")].map(
function (a)
{
return {
'oc': a.getAttribute("data-partnumber"),
'qty': parseInt(a.querySelector(".jsQuantityInput").value),
'descr': a.querySelector('.col-desc div div:nth-child(2)').firstChild.nodeValue.trim(),
'unit_price': parse_price(a.querySelector('[data-name="UnitPrice"] div div:nth-child(2)').firstChild.nodeValue.trim()),
'extended_price': parse_price(a.querySelector('[data-name="ExtendedPrice"] div div:nth-child(2)').firstChild.nodeValue.trim()),
'link': 'https://www.mouser.de/ProductDetail/' + a.getAttribute("data-partnumber")
};
}
);
}
var copyLink = document.createElement('a');
copyLink.href = "javascript:void('0');";
copyLink.innerHTML = '<i class="padding5right margin20left fa fa-lg fa-copy"></i>Sammelbestellung in Zwischenablage';
copyLink.addEventListener('click', function () {
copy_cart_clipboard(mouser_parse_cart());
alert('In Zwischenablage kopiert.');
return false;
});
document.querySelector('div.cart-actions').appendChild(copyLink);
function pollin_parse_cart()
{
return [...document.querySelectorAll('div.content--basket .row--product')].map(
function (a)
{
return {
'oc': a.querySelector('.content--sku').textContent.split(' ')[1].replace("\n", ""),
'qty': parseInt(a.querySelector("input[name='sQuantity']").value),
'descr': a.querySelector('.content--title').title,
'unit_price': parse_price(a.querySelector('.column--unit-price div').nextSibling.textContent.trim()),
'extended_price': parse_price(a.querySelector('.column--total-price div').nextSibling.textContent.trim()),
'link': a.querySelector('.content--title').href
};
}
);
}
var copyLink = document.createElement('a');
copyLink.className = "btn is--large";
copyLink.innerHTML = '<i class="icon--basket"></i> Sammelbestellung kopieren';
copyLink.addEventListener('click', function () {
copy_cart_clipboard(pollin_parse_cart());
alert('In Zwischenablage kopiert.');
return false;
});
document.querySelector('.main--actions').appendChild(copyLink);
function reichelt_parse_cart()
{
return [...document.querySelectorAll('#shoppingcart ul.wk_article')].map(
function (a)
{
var link = a.querySelector('li.artnr a');
return {
'oc': link.text,
'qty': a.querySelector('li.quantity input[type="number"]').value,
'descr': link.title,
'unit_price': parse_price(a.querySelector('li.single ul li span:nth-child(2)').textContent),
'extended_price': parse_price(a.querySelector('li.overall ul li').textContent),
'link': link.href,
};
}
);
}
var li = document.querySelector('#PRINTIT').parentNode;
li.innerHTML = '<a href="#"><span class="rchltaprint"></span> <span class="text">Sammelbestellung kopieren</span></a></button>';
li.firstChild.addEventListener('click', function () {
copy_cart_clipboard(reichelt_parse_cart());
alert('In Zwischenablage kopiert.');
return false;
});
function parse_price(s)
{
s = s.trim();
if (s.endsWith(''))
{
s = s.substr(0, s.length-1).trim();
}
var lastPoint = s.lastIndexOf(".");
var lastComma = s.lastIndexOf(",");
if (lastPoint > lastComma)
{
s = s.replace(',', '');
}
else if (lastComma > lastPoint)
{
s = s.replace('.', '');
s = s.replace(',', '.');
}
return parseFloat(s);
}
function format_price(p)
{
return p.toFixed(2).replace('.', ',') + '';
}
function format_link(url)
{
return '[' + url + '] Link';
}
function generate_wiki_text(cart)
{
var result = ['{|class="wikitable"', '!Anz.', '!Bestellnr.', '!Bezeichnung', '!Preis/1', '!Preis/N', '!Link'];
var sum = 0;
for (const a of cart)
{
result.push('|-');
result.push('| ' + [a.qty, a.oc , a.descr, format_price(a.unit_price) , format_price(a.extended_price), format_link(a.link)].join(" || "));
sum += a.extended_price;
}
result.push('|-', "|colspan=\"4\"| || '''" + sum.toFixed(2) + "''' ||", "|}");
return result.join("\n");
}
function copy_cart_clipboard(cart)
{
const el = document.createElement('textarea');
el.value = generate_wiki_text(cart);
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment