Skip to content

Instantly share code, notes, and snippets.

@q00u
Last active April 8, 2022 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save q00u/639243da26e4c30899f5ae6aa7ec7c4a to your computer and use it in GitHub Desktop.
Save q00u/639243da26e4c30899f5ae6aa7ec7c4a to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Easy-Clip
// @namespace https://gist.github.com/q00u
// @version 0.4
// @description One button clip all coupons
// @author Phoenix G
// @include /^https:\/\/www\.(safeway|albertsons)\.com\/(justforu|foru)\/coupons-deals\.html.*/
// @icon https://www.google.com/s2/favicons?domain=safeway.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Make elements to insert
console.log('Making Easy-Clip elements');
const svgURI = 'http://www.w3.org/2000/svg';
const newPath = document.createElementNS( svgURI, 'path' );
newPath.setAttribute( 'd', 'M 15 3 C 12.031398 3 9.3028202 4.0834384 7.2070312 5.875 A 1.0001 1.0001 0 1 0 8.5058594 7.3945312 C 10.25407 5.9000929 12.516602 5 15 5 C 20.19656 5 24.450989 8.9379267 24.951172 14 L 22 14 L 26 20 L 30 14 L 26.949219 14 C 26.437925 7.8516588 21.277839 3 15 3 z M 4 10 L 0 16 L 3.0507812 16 C 3.562075 22.148341 8.7221607 27 15 27 C 17.968602 27 20.69718 25.916562 22.792969 24.125 A 1.0001 1.0001 0 1 0 21.494141 22.605469 C 19.74593 24.099907 17.483398 25 15 25 C 9.80344 25 5.5490109 21.062074 5.0488281 16 L 8 16 L 4 10 z' );
const newSvg = document.createElementNS( svgURI, 'svg' );
newSvg.setAttribute( 'viewBox', '0 0 30 30' );
newSvg.setAttribute( 'class', 'svg-icon' );
newSvg.setAttribute( 'style', 'width: 20px; height: 20px' );
newSvg.appendChild( newPath );
const newA = document.createElement( 'a' );
newA.setAttribute( 'href', 'javascript:(function () {"use strict"; var promises = []; var allcoupons = Object.values(JSON.parse(localStorage.getItem("abJ4uCoupons"))["objCoupons"]); var coupons = allcoupons.filter(function(x){return x.status==="U";}).filter(function(y){return y.deleted!==0;}); if (coupons.length > 0) { window.alert("clipping " + coupons.length + " of " + allcoupons.length + " coupons"); coupons.forEach(function(item){ var data = {"items":[]}, clip = {}, list = {}; clip.clipType="C";clip.itemId=item.offerId;clip.itemType=item.offerPgm; list.clipType="L";list.itemId=item.offerId;list.itemType=item.offerPgm; data.items.push(clip);data.items.push(list); var request = new Request(window.AB.couponClipPath + "?storeId\x3d" + window.AB.userInfo.j4u.storeId, { method: %27POST%27, mode: %27cors%27, redirect: %27error%27, headers: new Headers(window.AB.j4uHttpOptions), body: JSON.stringify(data) }); var promise = fetch(request).then(function(response) { return response.json(); }) .then(function(itemjson) { if (itemjson.items[0]["status"] === 1) { var wtf = JSON.parse(localStorage.getItem("abJ4uCoupons")); if (wtf.objCoupons[item.offerId] && wtf.objCoupons[item.offerId]["offerId"] === item.offerId) { wtf.objCoupons[item.offerId].status = "C"; localStorage.setItem("abJ4uCoupons", JSON.stringify(wtf)); } else { var index = wtf.objCoupons.findIndex(function(obj){return obj.offerId === item.offerId}); if (index !== -1) { wtf.objCoupons[index].status = "C"; localStorage.setItem("abJ4uCoupons", JSON.stringify(wtf)); } } } }); promises.push(promise); }); Promise.all(promises).then(function(){ if (Object.values(JSON.parse(localStorage.getItem("abJ4uCoupons"))["objCoupons"]).filter(function(x){return x.status==="U";}).filter(function(y){return y.deleted!==0;}).length > 0) { window.alert("there are still some unclipped coupons - something probably broke this script"); } else { window.alert("all coupons clipped - reloading page"); } localStorage.removeItem("abCoupons"); localStorage.removeItem("abJ4uCoupons"); location.reload(); }); } else { if (allcoupons.length > 0) { window.alert("no clippable coupons"); } else { window.alert("no coupons detected"); } } })();' );
newA.setAttribute( 'title', 'Clip all coupons' );
newA.appendChild( newSvg );
const newLi = document.createElement( 'li' );
newLi.className = 'menu-nav__list-item';
newLi.appendChild( newA );
// Get location to insert button
const rightContainer = document.getElementsByClassName('menu-nav__right-container')[0];
console.log('Inserting Easy-Clip elements into', rightContainer);
rightContainer.appendChild( newLi );
})();
@q00u
Copy link
Author

q00u commented Mar 18, 2022

0.3 Update match domain

@q00u
Copy link
Author

q00u commented Apr 8, 2022

0.4 Match Albertsons as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment