// ==UserScript==
// @name Newegg cart to post converter for eggxpert.com
// @namespace http://userscripts.org/users/62850
// @description Converts the cart in to postable html that has been patched for the forums
// @include http://secure.newegg.com/Shopping/ShoppingCart.aspx*
// @include http://secure.newegg.ca/Shopping/ShoppingCart.aspx*
// @version 2
// @grant none
// ==/UserScript==
function findEle(target,i,e){
if(i==9)
return document.evaluate(target, e?e:document, null, i, null).singleNodeValue;
else
return document.evaluate(target, e?e:document, null, i, null);
}
function inArray(arr,val){
if((arr.constructor==Array)===false){
if(arr.toString().indexOf(',')!=-1){
arr=arr.split(',');
}
else{
if(arr==val){
return true;
}
else{
return false;
}
}
}
for(var i=0;i<arr.length;i++){
if(arr[i]==val){
return true;
}
}
return false;
}
function make(tag){
return document.createElement(tag);
}
var holder=findEle('.//table[@class="head"]/tbody/tr[@class="tool-bar"]/td',6,false),btn;
if(holder.snapshotLength==2){
btn=make('a');
btn.className='button button-mini button-tertiary';
btn.href="javascript:return false;";
btn.textContent='Generate HTML Code';
btn.title="HTML code for posting on forums";
btn.addEventListener('click',function(){
var table,tbody,tr,td,div,
i,cart,item,img,IMG,a,txt,items=Array();
table=make('table');
table.appendChild(make('tbody'));
tbody=table.childNodes[0];
tr=make('tr');
td=make('th');
td.textContent='Item';
td.setAttribute('colspan',2);
tr.appendChild(td);
td=make('th');
td.textContent='Quanity';
tr.appendChild(td);
td=make('th');
td.textContent='Price';
tr.appendChild(td);
tbody.appendChild(tr);
cart=findEle('.//table[@class="shipping-group"]/tbody/tr',6);
for(i=0;i<cart.snapshotLength;i++){
item=cart.snapshotItem(i);
tr=make('tr');
td=make('td');
img=make('img');
IMG=findEle('./td/div/a/img[@class="l-block product-image"]',9,item);
img.src=IMG.src;
td.appendChild(img);
tr.appendChild(td);
td=make('td');
a=make('a');
a.href=IMG.parentNode.href;
a.textContent=IMG.parentNode.title;
td.appendChild(a);
tr.appendChild(td);
td=make('td');
txt=findEle('./td[@width="50px"]/input',9,item);
td.textContent=txt?txt.value:findEle('./td[@width="50px"]/div',9,item).textContent;
tr.appendChild(td);
a=a.href.slice(a.href.lastIndexOf('=')+1);
if(!inArray(items,a))
items.push(a+'|'+td.textContent);
td=make('td');
txt=findEle('./td[@align="right"]//li[starts-with(@class,"price-current")]',9,item);
td.textContent=txt?txt.textContent.replace(/(\s|\t|\n)/g,''):'Combo';
a=make('a');
a.textContent='Add to cart';
a.href='http://secure.newegg.com/Shopping/AddToCart.aspx?Submit=ADD&ItemList='+items[items.length-1];
td.appendChild(make('br'));
td.appendChild(a);
tr.appendChild(td);
tbody.appendChild(tr);
}
cart=findEle('.//table[@class="shipping-group"]/tfoot',9);
tr=make('tr');
td=make('td');
td.setAttribute('colspan',3);
td.textContent="Subtotal:";
tr.appendChild(td);
txt=findEle('.//span[@class="amount"]',6,cart);
td=make('td');
td.textContent=txt.snapshotItem(0).textContent.replace(/(\s|\t|\n)/g,'')
tr.appendChild(td);
tbody.appendChild(tr);
tr=make('tr');
td=make('td');
td.setAttribute('colspan',3);
td.textContent="Shipping:";
tr.appendChild(td);
txt=findEle('.//span[@class="amount"]',6,cart);
td=make('td');
td.textContent=txt.snapshotItem(1).textContent.replace(/(\s|\t|\n)/g,'')
tr.appendChild(td);
tbody.appendChild(tr);
cart=findEle('.//table[@class="applied-code"]/tbody/tr',6,false);
for(i=0;i<cart.snapshotLength;i++){
tr=make('tr');
item=cart.snapshotItem(i);
td=make('td');
td.setAttribute('colspan',3);
td.textContent='Promo Code: '+findEle('./td/strong',9,item).textContent;
tr.appendChild(td);
td=make('td');
td.textContent=findEle('./td[@class="discount"]',9,item).textContent;
tr.appendChild(td);
tbody.appendChild(tr);
}
tr=make('tr');
td=make('td');
td.setAttribute('colspan',3);
td.textContent="Grand Total:";
tr.appendChild(td);
td=make('td');
td.textContent=findEle('.//td[@class="grand-total"]/span[@class="amount"]',9,false).textContent.replace(/(\s|\t|\n)/g,'')
tr.appendChild(td);
tbody.appendChild(tr);
div=make('div');
div.appendChild(table);
a=make('a');
a.href='http://secure.newegg.com/Shopping/AddToCart.aspx?Submit=ADD&ItemList='+items.join(',');
a.textContent='Add all to cart';
div.appendChild(a);
window.open('data:text/html;charset=utf-8,'+encodeURIComponent('<html><head><title>HTML Code to Post</title><head><body>If you would like to clean the html code, paste it in the <a href="http://tools.arantius.com/tabifier">Tabifier</a>.<textarea style="width:100%;height:calc(100% - 21px);" id="txtBox"></textarea></body></html>')).addEventListener('load',function(){
this.document.getElementById('txtBox').value=div.innerHTML;
},false);
},false);
holder.snapshotItem(1).appendChild(btn);
}