Sign in with
Sign up | Sign in
Your question

Script to post NewEgg cart on forum

Last response: in Systems
Share
March 10, 2011 12:57:52 AM

I know this is not directly related to New builds, but since it will mostly be used FOR this section, I guessed it was the best place to post it (could post it in Programming, but not that many people go there :p ).

So this is a GreaseMonkey script I got from mkey on userscripts.org and I modified it a bit so it would generate code compliant with this forum and would work with the Canadian NewEgg as well.

Hope you guys find this useful, feel free to upgrade it, but I suggest you send the modified code to the original owner on userscripts.org; I did out of fairness.
  1. // ==UserScript==
  2. // @name Newegg to BBcode
  3. // @namespace http://userscripts.org/scripts/show/87115
  4. // @description Converts the newegg shopping cart to a bbcode table ready to be pasted across various boards
  5. // @date 2010-10-01
  6. // @creator mkey
  7. // @include http://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view
  8. // @include http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Submit=view
  9. // ==/UserScript==
  10.  
  11. var d=document, byId=d.getElementById, create=d.createElement, byClass=d.getElementsByClassName, byTag=d.getElementsByTagName;
  12.  
  13. (function (){
  14. var item= byClass("cartDescription");
  15. var price= byClass("cartPrice");
  16. var pcs= byClass("cartQty");
  17. var t, t1, t2;
  18. var text = "";
  19. for (var i=1; i<item.length; i++){
  20. t= item[i].getElementsByTagName("a");
  21. t1= price[i].getElementsByTagName("dd");
  22. t2= pcs[i].getElementsByTagName("input");
  23. if (t2.length==0) t2=pcs[i].textContent.trim();
  24. else t2= t2[0].value;
  25.  
  26. if (t1.length==1) t1= t1[0].textContent;
  27. else if (t1.length==2 && price[i].getElementsByClassName("cartOrig").length>0) t1= t1[1].textContent;
  28. else if (t1.length==2 && price[i].getElementsByClassName("cartUnit").length>0) t1= t1[0].textContent;
  29. else t1= "ERROR";
  30.  
  31. text += "[*]"+t2+"x [urlExt=http://+t[0].href+"]"+t[0].textContent.trim()+"[/urlExt] ("+t1.trim()+")\n";
  32. }
  33.  
  34. t= byClass("cartSubtotal cartHeader");
  35. if (!t.length) return;
  36. t= t[0].getElementsByTagName("td");
  37. //text+= "[tr][td][center]-[/center][/td][td][size=3][b][align=right]Subtotal[/align][/b][/size][/td][td][center]-[/center][/td][td][size=3][b]"+t[1].textContent.trim()+"[/b][/size][/td][/tr][/tableb]";
  38. byClass("space")[0].innerHTML= "<td align=\"center\" colspan=\"5\" ><textarea cols=\"160\" rows=\"8\" style=\"font-size:12px;\" onclick=\"this.select();\" >"+text+"</textarea></td>";
  39.  
  40. })()
March 10, 2011 3:48:52 AM
March 10, 2011 3:53:03 AM

Links combos correctly but only provides the first product name in the combo.... still it's very convenient isn't it?

Related resources
March 10, 2011 3:57:58 AM

For those wondering, this is used along with a Firefox plugin called GreaseMonkey. Once you have the userscript installed correctly, the newegg shopping cart will load with a small window near the bottom. You simply copy and paste the text from that window into your TH forum post.
March 10, 2011 9:28:32 AM

I know it still has a few bugs, most of them from the original script. The original script was also providing the total, but when NewEgg "splits" the shopping cart, it would only report the first one it found. I will try to fix those issues :) .

So TODO:
  • List all items in a combo
  • Give good total
  • Remove "1x" when there is only 1 item (I think this is superfluous)
    March 10, 2011 4:42:01 PM

    1. if (t2==1)
    2. text += "[urlExt=http://+t[0].href+"]"+t[0].textContent.trim()+"[/urlExt] ("+t1.trim()+" )\n";
    3. else
    4. text += "[*]"+t2+"x [urlExt=http://+t[0].href+"]"+t[0].textContent.trim()+"[/urlExt] ("+t1.trim()+" )\n";


    I think this would get rid of the 1x :) 
    March 10, 2011 5:03:44 PM

    Or just write something like:
    1. text += "[*]";
    2. if(t2 > 1)
    3. text += t2+"x ";
    4. text += "[urlExt=http://+t[0].href+"]"+t[0].textContent.trim()+"[/urlExt] ("+t1.trim()+" )\n";

    I didn't fix it just because I was lazy and had to go to bed :p 
    March 10, 2011 6:48:39 PM

    hehe I cant help on the other issue tho, I know c sharp and java but it still look chinese to me, I would have to invest time that I dont have right now xD
    At least 1/3 is fixed now :D 
    March 10, 2011 9:33:22 PM
    March 10, 2011 10:34:58 PM

    This is nice. I usually just go with wish lists, but they typically don't have the actual links.
    March 10, 2011 11:53:14 PM

    Ok, complete redesign of the code. Now each combo item will show up (as distinct item in the list) with the combo URL as their link. I didn't bother putting the number of items back or the price again, but that is my next step. My biggest concern is the way the Canadian site work as a combo could actually be divided (add this combo to your cart for example and go see how it shows up); this is due to the fact that some items are shipping from the US and they divide shipping.

    1. // ==UserScript==
    2. // @name Newegg to BBcode
    3. // @description Converts the newegg shopping cart to a bbcode table ready to be pasted across various boards
    4. // @date 2011-03-10
    5. // @creator Zenthar
    6. // @include http://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view
    7. // @include http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Submit=view
    8. // ==/UserScript==
    9.  
    10. (function (){
    11. var items= document.getElementsByClassName("cartItem");
    12. var text = "";
    13.  
    14. for (var i=0; i<items.length; i++){
    15. var url, price, qte, desc, links;
    16.  
    17.  
    18. links = items[i].getElementsByTagName("a");
    19.  
    20. url = links[0].href;
    21.  
    22. for(var j=0 ; j<links.length ; j++) {
    23. if(links[j].getAttribute("name") == "CART_ITEM")
    24. text += "[*][urlExt=http://+url+]"+links[j].textContent.trim()+"[/urlExt]\n";
    25. }
    26. }
    27.  
    28. t= document.getElementsByClassName("cartSubtotal cartHeader");
    29. if (!t.length)
    30. return;
    31. t= t[0].getElementsByTagName("td");
    32. document.getElementsByClassName("space")[0].innerHTML= "<td align=\"center\" colspan=\"5\" ><textarea cols=\"160\" rows=\"8\" style=\"font-size:12px;\" onclick=\"this.select();\" >"+text+"</textarea></td>";
    33.  
    34. })()

    Example:
  • Antec Nine Hundred Black Steel ATX Mid Tower Computer Case
  • GIGABYTE GA-880GMA-UD2H AM3 AMD 880G SATA 6Gb/s USB 3.0 HDMI Micro ATX AMD Motherboard
  • Rosewill DESTROYER Black Gaming ATX Mid Tower Computer Case, comes with Three Fans-1x Front Blue LED 120mm Fan, 1x Top 120mm ...
  • Rosewill RV2-500 500 W ATX12V v2.2 / EPS12V SLI Ready Power Supply
  • Kingston HyperX 8GB (2 x 4GB) 240-Pin DDR3 SDRAM DDR3 1600 (PC3 12800) Desktop Memory Model KHX1600C9D3K2/8GX
  • AMD Phenom II X6 1090T Black Edition Thuban 3.2GHz Socket AM3 125W Six-Core Desktop Processor HDT90ZFBGRBOX
  • G.SKILL Phoenix Pro Series FM-25S2S-60GBP2 2.5" 60GB SATA II MLC Internal Solid State Drive (SSD)
  • Antec EarthWatts EA750 750W Continuous Power ATX12V version 2.3 SLI Certified CrossFire Ready 80 PLUS Certified Active PFC ...
  • Seagate Barracuda 7200.11 ST31500341AS 1.5TB 7200 RPM SATA 3.0Gb/s 3.5" Internal Hard Drive -Bare Drive
  • Kingston HyperX Blu 4GB (2 x 2GB) 240-Pin DDR3 SDRAM DDR3 1600 (PC3 12800) Desktop Memory Model KHX1600C9D3B1K2/4GX
    March 11, 2011 12:06:35 AM

    Did you lose the total, I thought that was working in an earlier rev.
    March 11, 2011 12:14:15 AM

    The total existed in the original mkey version, but I dropped it in my first version because it didn't work properly on the Canadian site (working on fixing that too).
    March 11, 2011 12:17:30 AM

    New version, re-added the quantities and it won't show up if only 1.
    1. // ==UserScript==
    2. // @name Newegg to BBcode
    3. // @description Converts the newegg shopping cart to a bbcode table ready to be pasted across various boards
    4. // @date 2011-03-10
    5. // @creator Zenthar
    6. // @include http://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view
    7. // @include http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Submit=view
    8. // ==/UserScript==
    9.  
    10. (function (){
    11. var items= document.getElementsByClassName("cartItem");
    12. var text = "";
    13.  
    14. for (var i=0; i<items.length; i++){
    15. var links = items[i].getElementsByTagName("a");
    16. var url = links[0].href;
    17. var qty = "";
    18. {
    19. var candidates = items[i].getElementsByTagName("td");
    20. for(var j=0 ; j<candidates.length ; j++) {
    21. if(candidates[j].getAttribute("class") == "cartQty") {
    22. var inputs = candidates[j].getElementsByTagName("input");
    23. if(inputs.length > 0)
    24. qty = inputs[0].getAttribute("value")
    25. else
    26. qty = candidates[j].textContent.trim();
    27. }
    28. }
    29. }
    30.  
    31. for(var j=0 ; j<links.length ; j++) {
    32. if(links[j].getAttribute("name") == "CART_ITEM") {
    33. text += "[*]"
    34. if(qty > 1)
    35. text += qty+"x ";
    36. text += "[urlExt=http://+url+]"+links[j].textContent.trim()+"[/urlExt]\n";
    37. }
    38. }
    39. }
    40.  
    41. var t= document.getElementsByClassName("cartSubtotal cartHeader");
    42. if (!t.length)
    43. return;
    44. t= t[0].getElementsByTagName("td");
    45. document.getElementsByClassName("space")[0].innerHTML= "<td align=\"center\" colspan=\"5\" ><textarea cols=\"160\" rows=\"8\" style=\"font-size:12px;\" onclick=\"this.select();\" >"+text+"</textarea></td>";
    46.  
    47. })()

    Example:
  • Antec Nine Hundred Black Steel ATX Mid Tower Computer Case
  • GIGABYTE GA-880GMA-UD2H AM3 AMD 880G SATA 6Gb/s USB 3.0 HDMI Micro ATX AMD Motherboard
  • 2x Rosewill DESTROYER Black Gaming ATX Mid Tower Computer Case, comes with Three Fans-1x Front Blue LED 120mm Fan, 1x Top 120mm ...
  • 2x Rosewill RV2-500 500 W ATX12V v2.2 / EPS12V SLI Ready Power Supply
  • 3x Kingston HyperX 8GB (2 x 4GB) 240-Pin DDR3 SDRAM DDR3 1600 (PC3 12800) Desktop Memory Model KHX1600C9D3K2/8GX
  • AMD Phenom II X6 1090T Black Edition Thuban 3.2GHz Socket AM3 125W Six-Core Desktop Processor HDT90ZFBGRBOX
  • G.SKILL Phoenix Pro Series FM-25S2S-60GBP2 2.5" 60GB SATA II MLC Internal Solid State Drive (SSD)
  • Antec EarthWatts EA750 750W Continuous Power ATX12V version 2.3 SLI Certified CrossFire Ready 80 PLUS Certified Active PFC ...
  • Seagate Barracuda 7200.11 ST31500341AS 1.5TB 7200 RPM SATA 3.0Gb/s 3.5" Internal Hard Drive -Bare Drive
  • Kingston HyperX Blu 4GB (2 x 2GB) 240-Pin DDR3 SDRAM DDR3 1600 (PC3 12800) Desktop Memory Model KHX1600C9D3B1K2/4GX
    March 11, 2011 12:29:54 AM

    Another version, now with grand total added:
    1. // ==UserScript==
    2. // @name Newegg to BBcode
    3. // @description Converts the newegg shopping cart to a bbcode table ready to be pasted across various boards
    4. // @date 2011-03-10
    5. // @creator Zenthar
    6. // @include http://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view
    7. // @include http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Submit=view
    8. // ==/UserScript==
    9.  
    10. (function (){
    11. var items= document.getElementsByClassName("cartItem");
    12. var text = "";
    13.  
    14. for (var i=0; i<items.length; i++){
    15. var links = items[i].getElementsByTagName("a");
    16. var url = links[0].href;
    17. var qty = "";
    18. {
    19. var candidates = items[i].getElementsByTagName("td");
    20. for(var j=0 ; j<candidates.length ; j++) {
    21. if(candidates[j].getAttribute("class") == "cartQty") {
    22. var inputs = candidates[j].getElementsByTagName("input");
    23. if(inputs.length > 0)
    24. qty = inputs[0].getAttribute("value")
    25. else
    26. qty = candidates[j].textContent.trim();
    27. }
    28. }
    29. }
    30.  
    31. for(var j=0 ; j<links.length ; j++) {
    32. if(links[j].getAttribute("name") == "CART_ITEM") {
    33. text += "[*]"
    34. if(qty > 1)
    35. text += qty+"x ";
    36. text += "[urlExt=http://+url+]"+links[j].textContent.trim()+"[/urlExt]\n";
    37. }
    38. }
    39. }
    40.  
    41. var grandTotal = document.getElementsByClassName("cartTotal cartHeader")[0];
    42. grandTotal = grandTotal.getElementsByTagName("td")[1].textContent.trim();//Always 2nd element
    43. text += "TOTAL: [u][b]"+grandTotal+"[/b][/u]";
    44.  
    45. var t= document.getElementsByClassName("cartSubtotal cartHeader");
    46. if (!t.length)
    47. return;
    48. t= t[0].getElementsByTagName("td");
    49. document.getElementsByClassName("space")[0].innerHTML= "<td align=\"center\" colspan=\"5\" ><textarea cols=\"160\" rows=\"8\" style=\"font-size:12px;\" onclick=\"this.select();\" >"+text+"</textarea></td>";
    50.  
    51. })()

    Example:
  • Antec Nine Hundred Black Steel ATX Mid Tower Computer Case
  • GIGABYTE GA-880GMA-UD2H AM3 AMD 880G SATA 6Gb/s USB 3.0 HDMI Micro ATX AMD Motherboard
  • 2x Rosewill DESTROYER Black Gaming ATX Mid Tower Computer Case, comes with Three Fans-1x Front Blue LED 120mm Fan, 1x Top 120mm ...
  • 2x Rosewill RV2-500 500 W ATX12V v2.2 / EPS12V SLI Ready Power Supply
  • 3x Kingston HyperX 8GB (2 x 4GB) 240-Pin DDR3 SDRAM DDR3 1600 (PC3 12800) Desktop Memory Model KHX1600C9D3K2/8GX
  • AMD Phenom II X6 1090T Black Edition Thuban 3.2GHz Socket AM3 125W Six-Core Desktop Processor HDT90ZFBGRBOX
  • G.SKILL Phoenix Pro Series FM-25S2S-60GBP2 2.5" 60GB SATA II MLC Internal Solid State Drive (SSD)
  • Antec EarthWatts EA750 750W Continuous Power ATX12V version 2.3 SLI Certified CrossFire Ready 80 PLUS Certified Active PFC ...
  • Seagate Barracuda 7200.11 ST31500341AS 1.5TB 7200 RPM SATA 3.0Gb/s 3.5" Internal Hard Drive -Bare Drive
  • Kingston HyperX Blu 4GB (2 x 2GB) 240-Pin DDR3 SDRAM DDR3 1600 (PC3 12800) Desktop Memory Model KHX1600C9D3B1K2/4GX
    TOTAL: $1,310.24
    March 11, 2011 1:04:44 AM

    Excellent work, thanks.
    So how do we install this script? I went to the userscripts.org location to install the original one.
    I tried to copy it out of your post and save it as a user.js file, but that didn't seem to work.
    This is the first time I've tried greasemonkey, am I missing something?

    Oh, and if you're still working on it, I really liked the individual prices listed in Version 3.
    March 11, 2011 1:35:58 AM

    CopaMundial said:
    Excellent work, thanks.
    So how do we install this script? I went to the userscripts.org location to install the original one.
    I tried to copy it out of your post and save it as a user.js file, but that didn't seem to work.
    This is the first time I've tried greasemonkey, am I missing something?

    Oh, and if you're still working on it, I really liked the individual prices listed in Version 3.


    Off the top of my head, if you have already got the original installed:

    Click on the grey "bbcode" button above the post.
    Select and copy the appropriate text, or all of it.
    Go to manage user scripts in Greasemonkey and select edit.
    Delete whatever is already there and replace it with what you copied.
    Compare carefully the post to what you pasted. Make sure nothing gets cut off or such (did in mine). Save the script.
    March 11, 2011 1:37:22 AM

    To simplify everyone's life, I just uploaded the script to userscirpts.org so you just have to press the "Install" button ;) 

    Link
    March 11, 2011 1:38:46 AM

    CopaMundial said:
    Oh, and if you're still working on it, I really liked the individual prices listed in Version 3.
    Individual item prices are easy, but how would you like combo prices to show-up?
    March 11, 2011 1:43:17 AM
    March 11, 2011 1:44:13 AM

    I like it, Thanks Zenthar!
    March 11, 2011 1:47:13 AM

    Yeah, this is nice.
    March 11, 2011 2:32:17 AM
    March 11, 2011 8:58:06 AM

    This is excellent. I'll need to install this at work (if I can!)
    March 11, 2011 7:25:39 PM
    March 11, 2011 7:41:47 PM
    March 11, 2011 10:48:44 PM

    hunter315 said:
    I tweaked it quick to make a Chrome friendly version, chrome doesnt require an extension to run scripts like greasemonkey, they appear to have built it in Chrome version
    Did anything change except the switch from @include to @match?
    March 11, 2011 10:50:29 PM

    Nope, so i was pretty sure it was still going to work. I use chrome on my desktop and was sad i couldnt use the fun tool so i fixed that problem.
    March 11, 2011 10:59:27 PM

    hunter315 said:
    Nope, so i was pretty sure it was still going to work. I use chrome on my desktop and was sad i couldn't use the fun tool so i fixed that problem.
    Do you mind if I try putting both @match and @include in my script to make it work with both (not sure if it will work, but worth a try)?
    March 11, 2011 11:02:59 PM

    I just tried with both @include and @match in my Firefox and it seems ok, can someone try in Chrome?
    March 11, 2011 11:11:36 PM

    It works for me in chrome, didnt realize it would play nice with both tags in there, but it does work.
    March 12, 2011 12:24:16 AM

    Thanks, I updated my code to include both; 15 downloads up to now :) 
    March 12, 2011 5:26:34 AM

    What do you guys think about an "adviser's guide" where we link resources like this... a sort of active thread that people could contribute to?
    A kind of "so you want to help out" thing where we could give some tips as well as links. I'm getting ideas for it already, but perhaps it's going too far?
    March 12, 2011 10:29:50 AM

    It could be nice yes, I don't know how many people would actually contribute, but it sure is worth encouraging them.
    March 12, 2011 1:13:41 PM

    It was already "1," but with this as an example, I added another vote to that one.
    !