E-Comm Shipping Calculator

Implements a shipping calculator on the current page.

目前為 2016-05-25 提交的版本,檢視 最新版本

// ==UserScript==
// @name        E-Comm Shipping Calculator
// @namespace   https://gf.qytechs.cn
// @include     https://sellers.shopgoodwill.com/sellers/newAuctionItem-catsel.asp*
// @include     https://sellers.shopgoodwill.com*
// @version     0.0.0.2
// @description Implements a shipping calculator on the current page.
// @grant       none
// ==/UserScript==

// These are editable! Format is as follows:
//
// "Button name" : price,
//
// Important notes:
// 1. Name MUST be in quotation marks.
// 2. A comma MUST follow the price, 

$('head').append('<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"><script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>');

var minimumWeight = 3; // This is the minimum weight we'll charge.
upsDivisor = 225;

var shippingMethods = {
    "Sm flat rate box" : {
        "price" : 6.80,
        "note" : "Small flat rate box",
        "method" : "USPS",
        "tooltip" : "Interior dimensions: 5x8.5x1.5 - NOTE: remember room for packing material!",
    },
    "Bubble mailer" : {
        "price" : 6.80,
        "note" : "Bubble mailer",
        "tooltip" : "Bubble mailers are padded, but consider if your item needs extra padding as well.",
        "method" : "USPS"
    },
    "Med flat rate box" : {
        "price" : 13.00,
        "note" : "Medium flat rate box",
        "method" : "USPS",
        "tooltip" : "Interior dimensions: 12x13.5x3.5 OR 11x8.5x5.5 - NOTE: remember room for packing material!"
    },
    "Media" : {
        "note" : "Media",
        "tooltip" : "Any: book; movie (VHS, DVD, Blu-Ray, laserdisc, film reel); music (record, 8-track, tape, CD) - regardless of size or weight. NOT comic books, magazines, newspapers, or video games.",
        "method" : "USPS"
    },
    "Lt clothing" : {
        "price" : 4.99,
        "note" : "Poly-mailer",
        "tooltip" : "Poly-mailer. Use if a clothing item is light - like a t-shirt.",
        "method" : "USPS"
    },
    "Med clothing" : {
        "price" : 6.99,
        "note" : "Poly-mailer",
        "tooltip" : "Poly-mailer. Use if a clothing item is a bit heavier - like a pair of jeans.",
        "method" : "USPS"
    },
    "Sm guitar box" : {
        "note" : "6x18x44 guitar box",
        "tooltip" : "Interior dimensions: 6x18x44; shipping weight: " + Math.ceil((7*19*45)/upsDivisor),
        "weight" : Math.ceil((7*19*45)/upsDivisor),
        "method" : "UPS"
    },
    "Lg guitar box" : {
        "note" : "8x20x50 guitar box",
        "tooltip" : "Interior dimensions: 8x20x50; shipping weight: " + Math.ceil((9*21*51)/upsDivisor),
        "weight" : Math.ceil((9*21*51)/upsDivisor),
        "method" : "UPS"
    },
    "Sm print box" : {
        "note" : "5x24x30 print box",
        "tooltip" : "Interior dimensions: 5x24x30; shipping weight: " + Math.ceil((6*24*31)/upsDivisor),
        "weight" : Math.ceil((6*24*31)/upsDivisor),
        "method" : "UPS"
    },
    "Lg print box" : {
        "note" : "5x30x36 print box",
        "tooltip" : "Interior dimensions: 5x30x36; shipping weight: " + Math.ceil((6*31*37)/upsDivisor),
        "weight" : Math.ceil((6*31*37)/upsDivisor),
        "method" : "UPS"
    },
/*    "Huge print box" : {
        "note" : "5.5x36x48 print box",
        "tooltip" : "Interior dimensions: 5.5x36x48; shipping weight: " + Math.ceil((7*37*49)/upsDivisor),
        "weight" : Math.ceil((7*37*49)/upsDivisor),
        "method" : "UPS"
    },*/
/*    "8x8 long box" : {
        "note" : "8x8 long box",
        "tooltip" : "Interior dimensions: 8x8x?",
        "method" : "UPS"
    },
    "12x12 long box" : {
        "note" : "12x12 long box",
        "tooltip" : "Interior dimensions: 8x8x?",
        "method" : "UPS"
    },*/
    "Sm coat box" : {
        "note" : "9x12x12 coat box",
        "tooltip" : "Interior dimensions: 9x12x12; shipping weight: " + Math.ceil((10*13*13)/upsDivisor),
        "weight" : Math.ceil((10*13*13)/upsDivisor),
        "method" : "UPS"
    },
    "Med coat box" : {
        "note" : "6x14x18 coat box",
        "tooltip" : "Interior dimensions: 6x14x18; shipping weight: " + Math.ceil((7*15*19)/upsDivisor),
        "weight" : Math.ceil((7*15*19)/upsDivisor),
        "method" : "UPS"
    },
    "Very lg coat box" : {
        "note" : "10x14x18 coat box",
        "tooltip" : "Interior dimensions: 10x14x18; shipping weight: " + Math.ceil((11*15*19)/upsDivisor),
        "weight" : Math.ceil((11*15*19)/upsDivisor),
        "method" : "UPS"
    },
    "Standard sm UPS box" : {
       "note" : "6.25x7.25x10.25 small box",
       "tooltip" : "Interior dimensions: 6.25x7.25x10.25; shipping weight: " + Math.ceil((7*8*11)/upsDivisor),
       "weight" : Math.ceil((7*8*11)/upsDivisor),
       "method" : "UPS",
    },
    "1-2 games/few cards" : {
       "price" : 2.99,
       "note" : "Game/cards",
       "tooltip" : "One or two small games (or several Gameboy games), or a small stack of cards",
       "method" : "USPS",
    },
/*    "Sew mchn w/case" : {
       "note" : "20x14x18 box",
       "tooltip" : "Interior dimensions: 14x18x20; shipping weight: " + Math.ceil((15*19*21)/upsDivisor),
       "weight" : Math.ceil((15*19*21)/upsDivisor),
       "method" : "UPS",
    },
    "Sew mchn, no case" : {
       "note" : "10x14x18 box",
       "tooltip" : "Interior dimensions: 10x14x18; shipping weight: " + Math.ceil((11*15*19)/upsDivisor),
       "weight" : Math.ceil((11*15*19)/upsDivisor),
       "method" : "UPS",
    }*/
};




// This next section sets up our default boxes.

// It's CRITICALLY IMPORTANT that the dimensions for these boxes get listed in ascending order (e.g. 8, 17, 36).

// "Interior" is the interior dimensions of the box - how large an item can fit inside. "Exterior" is the set of dimensions used to calculate the weight.
// The reason that these are defined separately is so that we can require a varying amount of padding per dimension and per box.

var guitarBoxes = {
	"default" : {
		0 : 8,
		1 : 17,
		2 : 36
		/* The interior/exterior doesn't matter here: the default "box" is used to decide "this doesn't need to go in a special box, and should be treated like a regular item". */
	},
	"boxes" : {
		1 : {
			"interior" : {
				0 : 6,
				1 : 17,
				2 : 42,
			},
			"exterior" : {
				0 : 7,
				1 : 19,
				2 : 45
			},
			"name" : "Small guitar box",
      "corresponds" : "Sm guitar box"
		},
		2 : {
			"interior" : {
				0 : 8,
				1 : 20,
				2 : 48
			},
			"exterior" : {
				0 : 9,
				1 : 21,
				2 : 51
			},
			"name" : "Large guitar box",
      "corresponds" : "Lg guitar box"
		}
	}
};

var artBoxes = {
	"default" : {
		0 : 6,
		1 : 18,
		2 : 20
	},
	"boxes" : {
		1 : {
			"interior" : {
				0 : 3,
				1 : 20,
				2 : 20
			},
			"exterior" : {
				0 : 6,
				1 : 25,
				2 : 25
			}
		},
		2 : {
			"interior" : {
				0 : 3,
				1 : 20,
				2 : 26
			},
			"exterior" : {
				0 : 6,
				1 : 25,
				2 : 31
			},
			"name" : "Small print box",
      "corresponds" : "Sm print box"
		},
		3 : {
			"interior" : {
				0 : 3,
				1 : 26,
				2 : 32
			},
			"exterior" : {
				0 : 6,
				1 : 31,
				2 : 37
			},
			"name" : "Large print box",
      "corresponds" : "Lg print box"
		},
/*    4 : {
			"interior" : {
				0 : 3.5,
				1 : 36,
				2 : 48
			},
			"exterior" : {
				0 : 7,
				1 : 37,
				2 : 49
			},
			"name" : "Huge print box",
      "corresponds" : "Huge print box"
		},*/
	}
};

var uspsBoxes = {
	"smallFlat1" : {
	/* Because these are billed at a flat rate, they also don't need two separate sets of dimensions. All we need to know is if the item will fit. */
		0 : 1.25,
		1 : 4.75,
		2 : 8.25,
		"name" : "small ($6.80) flat rate box",
    "corresponds" : "Sm flat rate box",
    "price" : "6.80",
	},
	"medFlat1" : {
		0 : 3.25,
		1 : 11.75,
		2 : 13.25,
		"name" : "medium ($13.00) flat rate box",
    "corresponds" : "Med flat rate box",
    "price" : "13.00",
	},
	"medFlat2" : {
		0 : 5.25,
		1 : 8.25,
		2 : 10.75,
		"name" : "medium ($13.00) flat rate box",
    "corresponds" : "Med flat rate box",
    "price" : "13.00",
	},
};

$("body").prepend("<input id='upsDivisor' type='hidden' value='"+upsDivisor+"'>");
var shippingOptions = "";
var buttonCount = 0;
$.each( shippingMethods, function( key, value ) {
       shippingOptions = shippingOptions + "<div name='" + key + "'  id='" + key + "' class='shipCharge shipType fakeButton'>" + key + "</div>";
       if (key == 'Med clothing' || key == 'Med coat box') {
           shippingOptions = shippingOptions + "<br><br>";
       }
       buttonCount++;
}); 
             
$('.shipType').css('font-size','4');
//$("body").append("<div id='shippingOptions' style='position:relative; bottom:20px;font-size:14px'></div>");

//$('#shippingOptions').after("<br><br>");

$("body").prepend("<div id='boxDefinitions' style='display:none;'></div>");
$("#boxDefinitions").data(shippingMethods);

//shippingOptions = shippingOptions + "<br><br><span class='shipType fakeButton' id='UPS'><b>UPS</b></span>";
//shippingOptions = shippingOptions + "<span class='shipType fakeButton' id='pickupOnly' name='pickupOnly'><b>Pickup Only</b></span>";

$("#shippingOptions").html(shippingOptions);



calcButtonList = {
    "general" : "General",
    "guitar" : "Guitar",
    "art" : "Frame/print",
//    "lot" : "Cub box",
    "long" : "Rug/long"
}

var myCalcButtons = "";

$.each(calcButtonList, function(index, value) {
   myCalcButtons += "<span class='calcButton fakeButton' id='calc-" + index + "' calctype='" + index + "'>" + value + "</span>";
});

$('body').append("<div style='margin-top: 20px; margin-bottom: 20px; padding: 4px; border: 1px solid #AAA; width:400px;' id='shipCalcContainer'>"

      + "<b>Shipping calculator</b><br><br>"
      + "<div style='padding:2px;'><b>Dimensions:</b> <input id='dim1' class='shipCalcInput' size=5 style='position:relative; left:2px;'> <input id='dim2' class='shipCalcInput' size=5> <input id='dim3' class='shipCalcInput' size=5></div>"
      + "<div style='padding:2px;'><b>Real weight:</b> <input id='actualWeight'  class='shipCalcInput' size=5></div>"
	    + "<div style='padding:2px; margin-top:8px;'><b>Add</b> <span type='text' readonly id='addInches' size=2 style='border:0; font-size:120%;'>2</span> <span id='inches'>inches</span> to each side<div id='inchesSlider'></div></div>"
	    + "<div style='padding:2px; margin-bottom:8px;'><b>Add</b> <span type='text' readonly id='addPounds' size=2 style='border:0; font-size:120%;'>2</span> <span id='pounds'>pounds</span> of packing material<div id='poundsSlider'></div></div>"
      + "<div style='padding:2px;'><b id='ownBoxText'>Ship in own (current) box?</b> <input type='checkbox' id='ownBox' class='shipCalcInput' ></div>"
      + "<div style='padding:4px; margin-top:3px;'><b>Calculate:</b> " + myCalcButtons + "</div>"
      + "<div style='padding:2px;' id='myDimWeight'></div>"
      + "<div style='padding:8px;' id='uspsDiv'></div>"
    + "</div>"
);

$("body").append("<div id='myBoxes' style='display:none;'></div>");
$("#myBoxes").data('guitarBoxes', guitarBoxes);
$("#myBoxes").data('artBoxes', artBoxes);
$("#myBoxes").data('uspsBoxes', uspsBoxes);

$('body').append('<input type="hidden" id="currentShipCalcType">');

var bindingTimeouts = window.setTimeout(function(){ // if this isn't on a timeout, it doesn't work right.
  $('#inchesSlider').slider({
    'value' : 2,
    'min' : 1,
    'max' : 3,
    'step' : 1,
    'slide' : function(event, ui){
       $('#addInches').html(ui.value);
      if (ui.value == 1) {
        $('#inches').html('inch');
      } else {
        $('#inches').html('inches');
      }
      calculateShipping();
    }
  }).css({
    'width' : '200px',
  });
  $('#poundsSlider').slider({
    'value' : 2,
    'min' : 1,
    'max' : 20,
    'step' : 1,
    'slide' : function(event, ui){
       $('#addPounds').html(ui.value);
      if (ui.value == 1) {
        $('#pounds').html('pound');
      } else {
        $('#pounds').html('pounds');
      }
      calculateShipping();
    }
  }).css({
    'width' : '200px',
  });
  $('.ui-slider').css({
    'transform':'scale(.9,.9)',
  });
  function makeButton() {
    return $('#dummyButton').clone().css('display','inline-block');
  }
  function getDimensions(){
    return [$('#dim1').val(), $('#dim2').val(), $('#dim3').val()];
  }
  function checkAgainstUSPS(dimList) {
    var uspsBoxes = $('#myBoxes').data('uspsBoxes');
    var uspsBox = {};
    $.each(uspsBoxes, function(index, boxArray) {
      if ($.isEmptyObject(uspsBox) && (dimList[0] < boxArray[0] && dimList[1] < boxArray[1] && dimList[2] < boxArray[2])) {
        $.each(boxArray, function(key, val){
          uspsBox[key] = val;
        });
      }
    });
    $('#uspsDiv').html('');
    if (!$.isEmptyObject(uspsBox)){
      $('#uspsDiv').append("This item <strong><em>may</em></strong> fit into a $<span id='uspsSuggPrice'>" + uspsBox['price'] + "</span> flat-rate box. <strong><em>Please use discretion!</em></strong><br>");
        makeButton().addClass('uspsButton').html('Use this (Post Office)').appendTo('#uspsDiv');
    }
  }
  function calculateShipping() {
    var shipType = $('#currentShipCalcType').val();
    if ($('#dim1').val().length && $('#dim2').val().length && $('#dim3').val().length && shipType.length > 0) {
      if (shipType == 'general') {
        var dimList = getDimensions(); 
        dimList.sort(function(a, b){return a-b});
        checkAgainstUSPS(dimList);
        dimWeight = 1;
        $('#myDimWeight').html('');
        $.each(dimList, function(i, dim){
          console.log('Was: ' + dimWeight);
          dimWeight *= (Math.ceil(dim) + 2*($('#addInches').html()));
          console.log('Times: (' + Math.ceil(dim) + "+ 2*" + Math.ceil($('#addInches').html()) + ")");
          console.log('Is: ' + dimWeight);
        });
        dimWeight /= $('#upsDivisor').val();
        dimWeight = Math.ceil(dimWeight);
        actualWeight = Math.ceil($('#actualWeight').val()) + Math.ceil($('#addPounds').html());
        $('#myDimWeight').html('Shipping weight: ' + Math.max(dimWeight, actualWeight));
      }
    }
  }
  
  $('.calcButton').bind('click', function(){
    $('.calcButton').css('background-color', '#EEE');
    $(this).css('background-color', '#BBB');
    shipType = this.id.substr(5);
    console.log(shipType);
    $('#currentShipCalcType').val(shipType);
    calculateShipping();
  });
  
  $('.shipCalcInput').bind('keyup', function(){
    calculateShipping();
  });
  
}, 1000);


$('body').append("<div id='dummyButton' class='fakeButton'></div>");
// because cloning this will keep the CSS.

$('.fakeButton').css({
  'border' : '1px solid #CCC',
  'background-color' : '#EEE',
  'padding' : '3px',
  'margin' : '3px',
  'display' : 'inline-block',
});

$('#dummyButton').hide();

// End

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址