Skip to content

Instantly share code, notes, and snippets.

@thewheat
Created July 31, 2020 04:34
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 thewheat/29390847b19425dc46ca8a33069e0302 to your computer and use it in GitHub Desktop.
Save thewheat/29390847b19425dc46ca8a33069e0302 to your computer and use it in GitHub Desktop.
Epic book https://www.getepic.com/ bookmarklet to start audio book from page 1. Converted to a bookmarklet via https://mrcoles.com/bookmarklet/
javascript:(function()%7BpausePlayButton%20%3D%20document.querySelector(%22.read-to-me-button%22)%3BpageSlider%20%3D%20document.querySelector(%22.book-page-slider-container%20.mat-slider%22)%3BclassIndicatingPaused%20%3D%20%22paused%22%3B%2F*%20based%20on%20https%3A%2F%2Fstackoverflow.com%2Fa%2F6158160%2F723139%20*%2Ffunction%20simulatedMousedown(target%2C%20options)%20%7Bvar%20event%20%3D%20target.ownerDocument.createEvent('MouseEvents')%2Coptions%20%3D%20options%20%7C%7C%20%7B%7D%2Copts%20%3D%20%7B%20%2F*%20These%20are%20the%20default%20values%2C%20set%20up%20for%20un-modified%20left%20clicks%20*%2Ftype%3A%20'mousedown'%2CcanBubble%3A%20true%2Ccancelable%3A%20true%2Cview%3A%20target.ownerDocument.defaultView%2Cdetail%3A%201%2CscreenX%3A%200%2C%20%2F*%20The%20coordinates%20within%20the%20entire%20page%20*%2FscreenY%3A%200%2CclientX%3A%200%2C%20%2F*%20The%20coordinates%20within%20the%20viewport%20*%2FclientY%3A%200%2CctrlKey%3A%20false%2CaltKey%3A%20false%2CshiftKey%3A%20false%2CmetaKey%3A%20false%2C%20%2F*%20I%20*think*%20'meta'%20is%20'Cmd%2FApple'%20on%20Mac%2C%20and%20'Windows%20key'%20on%20Win.%20Not%20sure%2C%20though!%20*%2Fbutton%3A%200%2C%20%2F*%200%20%3D%20left%2C%201%20%3D%20middle%2C%202%20%3D%20right%20*%2FrelatedTarget%3A%20null%2C%7D%3B%2F*%20Merge%20the%20options%20with%20the%20defaults%20*%2Ffor%20(var%20key%20in%20options)%20%7Bif%20(options.hasOwnProperty(key))%20%7Bopts%5Bkey%5D%20%3D%20options%5Bkey%5D%3B%7D%7D%2F*%20Pass%20in%20the%20options%20*%2Fevent.initMouseEvent(opts.type%2Copts.canBubble%2Copts.cancelable%2Copts.view%2Copts.detail%2Copts.screenX%2Copts.screenY%2Copts.clientX%2Copts.clientY%2Copts.ctrlKey%2Copts.altKey%2Copts.shiftKey%2Copts.metaKey%2Copts.button%2Copts.relatedTarget)%3B%2F*%20Fire%20the%20event%20*%2Ftarget.dispatchEvent(event)%3B%7DsimulatedMousedown(pageSlider%2C%20%7BclientX%3A%200%2C%20clientY%3A%200%7D)%3Bif(pausePlayButton.classList.contains(classIndicatingPaused))%20pausePlayButton.click()%7D)()
pausePlayButton = document.querySelector(".read-to-me-button");
pageSlider = document.querySelector(".book-page-slider-container .mat-slider");
classIndicatingPaused = "paused";
/* based on https://stackoverflow.com/a/6158160/723139 */
function simulatedMousedown(target, options) {
var event = target.ownerDocument.createEvent('MouseEvents'),
options = options || {},
opts = { /* These are the default values, set up for un-modified left clicks */
type: 'mousedown',
canBubble: true,
cancelable: true,
view: target.ownerDocument.defaultView,
detail: 1,
screenX: 0, /* The coordinates within the entire page */
screenY: 0,
clientX: 0, /* The coordinates within the viewport */
clientY: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false, /* I *think* 'meta' is 'Cmd/Apple' on Mac, and 'Windows key' on Win. Not sure, though! */
button: 0, /* 0 = left, 1 = middle, 2 = right */
relatedTarget: null,
};
/* Merge the options with the defaults */
for (var key in options) {
if (options.hasOwnProperty(key)) {
opts[key] = options[key];
}
}
/* Pass in the options */
event.initMouseEvent(
opts.type,
opts.canBubble,
opts.cancelable,
opts.view,
opts.detail,
opts.screenX,
opts.screenY,
opts.clientX,
opts.clientY,
opts.ctrlKey,
opts.altKey,
opts.shiftKey,
opts.metaKey,
opts.button,
opts.relatedTarget
);
/* Fire the event */
target.dispatchEvent(event);
}
simulatedMousedown(pageSlider, {clientX: 0, clientY: 0});
if(pausePlayButton.classList.contains(classIndicatingPaused)) pausePlayButton.click();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment