Skip to content

Instantly share code, notes, and snippets.

@superdave
Created April 28, 2020 18:10
Show Gist options
  • Save superdave/f4ef334a6fecd9869e71e2811b8bf65a to your computer and use it in GitHub Desktop.
Save superdave/f4ef334a6fecd9869e71e2811b8bf65a to your computer and use it in GitHub Desktop.
Fine Wines and Even Better Spirits
// ==UserScript==
// @name FineWines
// @version 1
// @match https://www.finewineandgoodspirits.com/*
// @run-at document-start
// finewinesandgoodspirits.com has a "throttling" mechanism on their page which aims to
// reduce the load on their server with the below script tacked to the body of each page:
/*
<script type="text/javascript">
var validate = "false";
var limitOrders = "true";
var userType = "G";
if(userType != 'A'){
if(limitOrders == 'true'){
if(validate == 'false'){
var url = "http://";
url += window.location.hostname;
url +='/webapp/wcs/stores/servlet/SpecialAccessLandingPageView?langId=-1&storeId=10051&catalogId=10051';
window.location.href= url;
}
}
}
</script>
*/
// In order to neutralize this client-only solution, we need to remove or alter the script
// after it loads in the DOM, but before it runs. This is challenging, because it means we
// need to watch DOM events to see when it gets parsed as text, then either remove it from
// the DOM or change the content to something relatively harmless. The MutationObserver
// is perfect for this, as it'll notify us of new content as it's parsed into the DOM (but
// it means we need to be absolutely sure to run at document-start or the script will most
// likely fire and redirect us before we can patch it out).
// We need a MutationObserver in order to patch the inline scripts before they execute.
new MutationObserver((list, observer) => {
// Each call has a list of MutationRecords.
for(let x of list.values()) {
// Each MutationRecord has a list of added nodes (and other things, but we're mostly
// interested in those).
for(let node of x.addedNodes) {
// If the node is a script node and includes the special redirect page name, we can
// reasonably assume that this is the one we want to zap.
if(node.nodeName == "SCRIPT" && node.textContent.includes("SpecialAccessLandingPageView")) {
// We could erase it entirely, but we should probably put in some of the variables
// it creates just in case they're inspected elsewhere.
node.textContent = "validate = false; userType = 'A';"
// We're assuming we only need to zap one script, so stop listening so it doesn't
// kill performance later.
observer.disconnect()
// And we're done, so GTFO.
return;
}
}
}
}).observe(document.documentElement, { childList: true, subtree: true });
// ==/UserScript==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment