Skip to content

Instantly share code, notes, and snippets.

@kielni
Last active August 29, 2015 14:26
Show Gist options
  • Save kielni/8ebe68422fb04b98e412 to your computer and use it in GitHub Desktop.
Save kielni/8ebe68422fb04b98e412 to your computer and use it in GitHub Desktop.
Greasmonkey script to hide items on Amazon Fresh category pages that are not on my list, so I can find the stuff I do want. Right click an item picture, then click Not on my list to hide the item. Demo: https://www.youtube.com/edit?video_id=mTDQGyF3Zvs
// ==UserScript==
// @name FreshThresh
// @namespace github.com/kielni
// @description Hide items from Amazon Fresh via right click menu
// @include https://fresh.amazon.com/*
// @version 1
// @grant none
// ==/UserScript==
/*
This script hides prducts not on my list from Amazon Fresh,
so I can find the stuff I do want. It saves the set of hidden
items in local storage.
*/
// add right click menu
if (!("contextMenu" in document.documentElement &&
"HTMLMenuItemElement" in window)) return;
var body = document.body;
body.addEventListener("contextmenu", initMenu, false);
var menu = body.appendChild(document.createElement("menu"));
menu.outerHTML = '<menu id="userscript-hide" type="context">'+
' <menuitem label="Not on my list"></menuitem>'+
'</menu>';
document.querySelector("#userscript-hide menuitem")
.addEventListener("click", hideItem, false);
// wait for products (.resultSet) to load
var observer = new window.MutationObserver(mutationHandler);
var observerConfig = {
childList: true, attributes: false, subtree: true
};
observer.observe (document, observerConfig);
function mutationHandler (mutationRecords) {
mutationRecords.forEach(function (mutation) {
if (mutation.type == "childList" && mutation.addedNodes.length) {
for (var i = 0; i < mutation.addedNodes.length; i++) {
var node = mutation.addedNodes.item(i);
if (node.nodeType === 1 && node.classList.contains("resultSet")) {
hideItems();
}
}
}
});
}
// hide item ids from list of keys in local storage
function hideItems() {
var hidden = getHidden();
Object.keys(hidden).forEach(function(itemId) {
var $item = jQuery("#"+itemId);
$item.closest(".smallProduct").hide();
console.log("hiding "+itemId+"\t"+hidden[itemId]);
});
}
// get hidden items from local storage
function getHidden() {
var hidden = unsafeWindow.localStorage.getItem("FreshThresh");
return hidden ? JSON.parse(hidden) : {};
}
// show right click menu on click of image, and save item id as attribute
function initMenu(ev) {
if (ev.target.localName !== "img") {
return;
}
body.setAttribute("contextmenu", "userscript-hide");
var itemId = jQuery(ev.target).closest(".itemPicture").attr("id");
var menuitem = document.querySelector("#userscript-hide menuitem");
menuitem.setAttribute("itemId", itemId);
}
// hide an item when it's clicked, and save to local storage for future pageloads
function hideItem(ev) {
var menuitem = document.querySelector("#userscript-hide menuitem");
var itemId = menuitem.getAttribute("itemId");
var $item = jQuery("#"+itemId);
$item.closest(".smallProduct").fadeOut();
var title = $item.closest(".item").find(".title").text();
var hidden = getHidden();
if (!hidden[itemId]) {
hidden[itemId] = title;
unsafeWindow.localStorage.setItem("FreshThresh", JSON.stringify(hidden));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment