Advertisement
fistsofthenorthstar

ocuk script

Feb 4th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. /*
  2. * Cross-browser Overclockers UK (OcUK) shopping cart to forum BB-Code converter bookmarklet backend
  3. * by Andrew Killer <akiller@gmail.com>
  4. * http://code.google.com/p/ocuk-shopping-cart-to-forum-converter
  5. *
  6. * Rewrite/pilfer of Oliver UK's OcUK Shopping Cart Viewer (https://chrome.google.com/webstore/detail/empfloiadabicdlgahhamannadefhehj?gl=IN)
  7. *
  8. * Bookmarklet template: http://stackoverflow.com/a/6235308/171703
  9. * Bookmarklet generator: http://userjs.up.seesaa.net/js/bookmarklet.html
  10. */
  11.  
  12. /*
  13. * Get next table row
  14. */
  15. function getNextRow(currRow) {
  16. return $(currRow).next('tr');
  17. };
  18.  
  19. /*
  20. * String format function
  21. * http://stackoverflow.com/a/2648463/171703
  22. */
  23. String.prototype.format = String.prototype.f = function() {
  24. var s = this, i = arguments.length;
  25. while (i--) {s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);}
  26. return s;
  27. };
  28.  
  29. /*
  30. * Store gathered data as JSON object:
  31. */
  32. var data = {
  33. cart_items : [],
  34. shipping_cost: 0,
  35. total_cost: 0,
  36. };
  37.  
  38. /*
  39. * Build and parse information from shopping cart into data:
  40. */
  41. var cartTable = $("#shoppingBkt");
  42.  
  43. cartTable.find('tr:gt(0)').not('.basket_option').not('.totalRow').not('#orderTotal').each(function() {
  44.  
  45. var row = $(this);
  46. var productConfiguration = [];
  47.  
  48. // Look for system configuration options
  49. var nextRow = getNextRow(row);
  50. while (nextRow.hasClass("basket_option")) {
  51. // a complete system with no option price or count
  52. if (nextRow.has('input').length > 0) {
  53. var rowItems = nextRow.children();
  54. productConfiguration.push({
  55. 'type' : 'complete',
  56. 'desc' : rowItems.eq(0).html().split('<br>')[1],
  57. 'qty' : rowItems.eq(1).find('input:first').val(),
  58. 'price' : rowItems.eq(2).find('.price').text(),
  59. 'total' : rowItems.eq(3).find('.lineTotal').text()
  60. });
  61. }
  62. // a fully configurable build
  63. else {
  64. var productOptions = nextRow.find('td:first').html().split('<br>');
  65. for (var i=1; i<productOptions.length; i++) {
  66. var productDesc = productOptions[i].replace(' - ', '');
  67.  
  68. if (productDesc.length > 0) {
  69. productConfiguration.push({
  70. 'type' : 'configurable',
  71. 'desc' :productOptions[i].replace(' - ', '')
  72. });
  73. }
  74. }
  75. }
  76. nextRow = getNextRow(nextRow);
  77. }
  78.  
  79. var rowItems = row.children();
  80. data.cart_items.push({
  81. 'image' : rowItems.eq(0).find('img:first').prop('src'),
  82. 'desc' : rowItems.eq(1).text(),
  83. 'configuration' : productConfiguration,
  84. 'link' : rowItems.eq(1).find('a:first').prop('href'),
  85. 'qty' : rowItems.eq(2).find('input:first').val(),
  86. 'price' : rowItems.eq(3).find('.price').text(),
  87. 'total' : rowItems.eq(4).find('.lineTotal').text()
  88. });
  89. });
  90.  
  91. data.shipping_cost = cartTable.find("tr.totalRow:nth-child(2n)").children('td:last').text();
  92. data.total_cost = cartTable.find("tr#orderTotal").children('td:last').text();
  93.  
  94. /*
  95. * Turn data into BB code for forum
  96. */
  97. var cartContents="", cartContentsFooter="";
  98. jQuery.each(data.cart_items, function(i, item) {
  99.  
  100. // Custom build configuration data
  101. var cartContentsConfiguration="";
  102. if (item.configuration.length > 0) {
  103. jQuery.each(item.configuration, function(j, configuration) {
  104. if (configuration.type == "complete") {
  105. cartContentsConfiguration += "\n[COLOR=\"Wheat\"] - {0} x {1}[/COLOR] [b][COLOR=\"Yellow\"]{2}[/COLOR][/b]"
  106. .format(configuration.qty, configuration.desc, configuration.price);
  107. } else {
  108. cartContentsConfiguration += "\n[COLOR=\"Wheat\"] - {0}[/COLOR]".format(configuration.desc);
  109. }
  110. });
  111. }
  112.  
  113. cartContents += "{0} x [url=\"{1}\"]{2}[/url] [b][COLOR=\"Yellow\"]{3}[/COLOR][/b]{4}{5}\n"
  114. .format(item.qty, item.link, item.desc, item.price, (item.qty > 1 ? " [b][COLOR=\"DarkOrange\"]({0})[/COLOR][/b]".format(item.total) : ""), cartContentsConfiguration);
  115.  
  116. if (item.image) cartContentsFooter += "[url=\"{0}\"][img]{1}[/img][/url] ".format(item.link, item.image);
  117. });
  118.  
  119. var cartBBCode = ("[COLOR=\"Yellow\"][b]YOUR BASKET[/b][/COLOR]\n{0}[b]Total : [COLOR=\"Yellow\"]{1}[/COLOR][/b] (includes shipping : {2}).\n\n{3}\n").format(cartContents, data.total_cost, data.shipping_cost, cartContentsFooter);
  120.  
  121. if(window.clipboardData)
  122. {
  123. window.clipboardData.setData("Text", cartBBCode);
  124. }
  125. else
  126. {
  127. /*
  128. * Display to users (somewhat of a hack thanks to no clipboard support from JavaScript without Flash or Internet Explorer)
  129. */
  130. var dataPlacementLocation = $("#maincell #centre");
  131.  
  132. var OcUKBBGenerator = $("#OcUKBBGenerator");
  133. if ((OcUKBBGenerator).length)
  134. {
  135. OcUKBBGenerator.find('textarea').val(cartBBCode);
  136. } else
  137. {
  138. dataPlacementLocation.append("<div id=\"OcUKBBGenerator\"><h1 class=\"bigred\">Your Cart Contents BB-Code</h1><p>Copy and paste the following into the forums:</p><textarea id=\"OcUKBBGeneratorTextArea\" cols=\"130\" rows=\"20\" style=\"width:99%\" readonly=\"readonly\">" + cartBBCode + "</textarea></div>");
  139. OcUKBBGenerator = $("#OcUKBBGenerator");
  140. }
  141.  
  142. $(window).scrollTop(OcUKBBGenerator.position().top)
  143.  
  144. console.log(data);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement