Last active
August 9, 2018 05:56
-
-
Save idpaterson/3ec09bfb54cafeb9cb63f1ed8d899bcf to your computer and use it in GitHub Desktop.
Adds convenient keyboard shortcuts to Sears Shop Your Way Sweeps
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 Shop Your Way Sweeps | |
// @description Adds keyboard shortcuts to Shop Your Way sweepstakes | |
// @version 0.1.3 | |
// @author Ian Paterson | |
// @namespace http://idpaterson.github.io/ | |
// @copyright Copyright (c) 2017-2018 Ian Paterson | |
// @license MIT - https://tldrlegal.com/license/mit-license | |
// @match https://cpe.sears.com/sweepstakes/web/landing* | |
// @grant none | |
// @run-at document-idle | |
// @updateURL https://gist.github.com/idpaterson/3ec09bfb54cafeb9cb63f1ed8d899bcf/raw/shop-your-way-sweeps.meta.js | |
// @downloadURL https://gist.github.com/idpaterson/3ec09bfb54cafeb9cb63f1ed8d899bcf/raw/shop-your-way-sweeps.user.js | |
// ==/UserScript== |
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 Shop Your Way Sweeps | |
// @description Adds keyboard shortcuts to Shop Your Way sweepstakes | |
// @version 0.1.3 | |
// @author Ian Paterson | |
// @namespace http://idpaterson.github.io/ | |
// @copyright Copyright (c) 2017-2018 Ian Paterson | |
// @license MIT - https://tldrlegal.com/license/mit-license | |
// @match https://cpe.sears.com/sweepstakes/web/landing* | |
// @grant none | |
// @run-at document-idle | |
// @updateURL https://gist.github.com/idpaterson/3ec09bfb54cafeb9cb63f1ed8d899bcf/raw/shop-your-way-sweeps.meta.js | |
// @downloadURL https://gist.github.com/idpaterson/3ec09bfb54cafeb9cb63f1ed8d899bcf/raw/shop-your-way-sweeps.user.js | |
// ==/UserScript== | |
setTimeout(function () { | |
'use strict'; | |
$(document).keydown(handleKeyboardShortcuts); | |
var KEYS = { | |
escape: 27, | |
n: 78, | |
return: 13, | |
s: 83, | |
}; | |
var lastFocusedSweepId; | |
function handleKeyboardShortcuts(event) { | |
// Do not process typing in text fields as keyboard shortcuts | |
if ($(event.target).is(':input:not(:button, :checkbox, :radio)')) { | |
return true; | |
} | |
return ( | |
handleSweepListShortcuts(event) || | |
handleModalShortcuts(event) | |
) || true; | |
} | |
function handleModalShortcuts(event) { | |
var sel; | |
switch (event.which) { | |
case KEYS.escape: | |
sel = 'button.close'; | |
break; | |
case KEYS.return: | |
sel = 'button.continue-button:not(:focus), button.submit:not(:focus), button.sweeps-button:not(:focus)'; | |
break; | |
case KEYS.s: | |
sel = '.skip-button'; | |
break; | |
} | |
if (sel) { | |
$('.modal').find(sel).filter(':visible').first().click(); | |
} | |
return !!sel; | |
} | |
function handleSweepListShortcuts(event) { | |
var sel; | |
var forward = !event.shiftKey; | |
switch (event.which) { | |
case KEYS.n: | |
sel = 'button.get-started:idp-in-active-sweep, .instant-win-winner-button:idp-in-active-sweep, .step-continue:idp-in-active-sweep'; | |
break; | |
} | |
if (sel) { | |
var sweeps = $('.SweepView'); | |
var totalSweeps = sweeps.length; | |
// Reduce the sweeps to those preceding or following the last focused sweep | |
if (lastFocusedSweepId) { | |
var lastFocusedSweep = sweeps.filter('[data-id=' + lastFocusedSweepId + ']'); | |
var index = sweeps.index(lastFocusedSweep); | |
if (forward) { | |
sweeps = sweeps.slice(index + 1); | |
} else { | |
sweeps = sweeps.slice(0, index); | |
} | |
} | |
var elements = sweeps.find(sel).filter(':visible'); | |
// Scroll down to load more when necessary | |
if (forward && elements.length === 0) { | |
window.scrollBy(0, 9999); | |
// Wait until more have loaded, or stop if nothing after 2 seconds | |
var count = 0; | |
var interval = setInterval(function () { | |
if ($('.SweepView').length > totalSweeps) { | |
handleSweepListShortcuts(event); | |
clearInterval(interval); | |
} | |
if (++count > 8) { | |
clearInterval(interval); | |
} | |
}, 250); | |
return false; | |
} | |
var element = elements[forward ? 'first' : 'last'](); | |
// Some elements are not keyboard accessible so a click must be emulated | |
if (element.is('span')) { | |
element.attr('tabindex', '0').keypress(clickOnReturnKey); | |
} | |
element.focus(); | |
} | |
return !!sel; | |
} | |
function clickOnReturnKey(event) { | |
if (event.which === KEYS.return) { | |
$(event.target).click(); | |
} | |
} | |
// Track the last focused sweep | |
$('.SweepListView').on('focus', '.SweepView', function () { | |
lastFocusedSweepId = $(this).attr('data-id'); | |
}); | |
// Focus the first form field or button when a modal is shown | |
$('#modals').on('shown.bs.modal', '.modal', function () { | |
$(this).find('.form-wrapper :input:visible:not([style*="hidden"]), button.continue-button, button.submit, button.sweeps-button').first().focus(); | |
}); | |
// Allow filtering on sweeps that are not current blocked with a please wait message | |
$.extend($.expr[':'], { | |
'idp-in-active-sweep': function parentSweepNotBlocked(el) { | |
return $(el).closest('.SweepView').find('.blockUI.blockOverlay').length === 0; | |
} | |
}); | |
}, 1500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you use this script? Tried in chrome/ tampermonkey expecting the N, S, Return, and Esc keys to do something.