Created
June 19, 2019 20:13
-
-
Save rparrett/8b98d0790d5f3291be31a58d7745a46d to your computer and use it in GitHub Desktop.
Userscript for showing GrubHub delivery fees in search results
This file contains hidden or 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 Show Grubhub Delivery Fees | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Why on earth do I have to do this? I hate you. | |
// @author You | |
// @match https://www.grubhub.com/search* | |
// @grant none | |
// ==/UserScript== | |
let oldXHROpen = window.XMLHttpRequest.prototype.open; | |
window.XMLHttpRequest.prototype.open = function(XMLHttpRequest) { | |
var self = this; | |
this.addEventListener("readystatechange", function() { | |
if (!this.readyState === 4) { | |
return; | |
} | |
if (this.responseURL.indexOf('search') == -1) { | |
return; | |
} | |
// fix up some slight inconsistencies in data format on first and subsequent loads | |
var d = JSON.parse(this.responseText); | |
if (!d) { | |
return; | |
} | |
if (d.search_result) { | |
d = { | |
results: d.search_result.results, | |
sponsored_result: d.sponsored_result | |
}; | |
} | |
if (!d.results) { | |
return; | |
} | |
var allResults = d.results; | |
if (d.sponsored_result && d.sponsored_result.results) { | |
allResults = allResults.concat(d.sponsored_result.results); | |
} | |
if (allResults.length < 1) { | |
return; | |
} | |
// Results for previous orders and stuff are showing up at this point. | |
// Get rid of them. | |
if (!allResults[0].restaurant_id) { | |
return; | |
} | |
// Set up an interval to occasionally check to see if we can populate the | |
// new data yet, and then do so. | |
(function() { | |
var updater = setInterval(function(d) { | |
if (!document.querySelector('#' + CSS.escape(d[0].restaurant_id))) { | |
return; | |
} | |
function formatPrice(pennies) { | |
return '$' + (parseInt(pennies) / 100.0).toFixed(2); | |
} | |
for (var i = 0; i < d.length; i++) { | |
var anchor = document.querySelector('#' + CSS.escape(d[i].restaurant_id)); | |
if (!anchor) { | |
return; | |
} | |
var sibling = anchor.nextSibling; | |
var deliveryMin = sibling.querySelector('ghs-restaurant-delivery-min'); | |
var deliveryPrice = deliveryMin.querySelector('.deliveryPrice'); | |
if (!deliveryPrice) { | |
deliveryPrice = document.createElement('p') | |
deliveryPrice.textContent = 'Fee: ' + formatPrice(d[i].delivery_fee.price); | |
deliveryPrice.className = 'deliveryPrice'; | |
deliveryMin.appendChild(deliveryPrice); | |
} else { | |
deliveryPrice.textContent = 'Fee: ' + formatPrice(d[i].delivery_fee.price); | |
} | |
if (d[i].delivery_fee.price <= 200) { | |
deliveryPrice.style.color = '#0e7c28'; | |
} else if (d[i].delivery_fee.price <= 350) { | |
deliveryPrice.style.color = 'black'; | |
} else { | |
deliveryPrice.style.color = '#ff8080'; | |
} | |
} | |
clearInterval(updater); | |
}, 200, allResults); | |
})(allResults); | |
}, false); | |
oldXHROpen.apply(this, arguments); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment