Sign in with
Sign up | Sign in

Script to post NewEgg cart on forum

Last response: in Systems
Share

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 <a href="http://userscripts.org/scripts/show/87115" rel="nofollow" target="_blank">http://userscripts.org/scripts/show/87115</a>
  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 <a href="http://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view" rel="nofollow" target="_blank">http://secure.newegg.com/Shopping/ShoppingCart.aspx?Sub...</a>
  8. // @include <a href="http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Submit=view" rel="nofollow" target="_blank">http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Subm...</a>
  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. })()

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.
Related ressources

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)
    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 :) 

    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 

    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 <a href="http://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view" rel="nofollow" target="_blank">http://secure.newegg.com/Shopping/ShoppingCart.aspx?Sub...</a>
    7. // @include <a href="http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Submit=view" rel="nofollow" target="_blank">http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Subm...</a>
    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
  • 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 <a href="http://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view" rel="nofollow" target="_blank">http://secure.newegg.com/Shopping/ShoppingCart.aspx?Sub...</a>
    7. // @include <a href="http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Submit=view" rel="nofollow" target="_blank">http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Subm...</a>
    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
  • 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 <a href="http://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view" rel="nofollow" target="_blank">http://secure.newegg.com/Shopping/ShoppingCart.aspx?Sub...</a>
    7. // @include <a href="http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Submit=view" rel="nofollow" target="_blank">http://secure.newegg.ca/Shopping/ShoppingCart.aspx?Subm...</a>
    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
  • 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.

    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.

    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?

    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?

    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)?

    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?
    Ask the community
    !