Skip to content

Instantly share code, notes, and snippets.

@cuylerstuwe
Last active July 21, 2019 19:05
Show Gist options
  • Save cuylerstuwe/cf117bf020092a203c78f53bbf09ca1c to your computer and use it in GitHub Desktop.
Save cuylerstuwe/cf117bf020092a203c78f53bbf09ca1c to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Syntax.FM - Mark Finished Shows
// @namespace salembeats
// @version 1.4
// @description UPDATE: Fix for episode URL formatting (episodes under 100 have leading zeroes in the URL).
// @author Cuyler Stuwe (salembeats)
// @include https://syntax.fm/*
// ==/UserScript==
const FINISHED_SHOW_STYLE = `
opacity: 0.2;
`;
async function main() {
const audioEl = document.querySelector("audio");
const audioPlayerEl = audioEl.parentElement;
const audioPlayerTitleEl = audioPlayerEl.querySelector(".player__title");
const scrapeCurrentEpisodeNumber = () => audioPlayerTitleEl.innerText.match(/(?=\d+:)\d+/)[0];
const formatNumberToEpisodeUrlFormat = number => (+number).toLocaleString("en-US", {minimumIntegerDigits: 3});
const showLinkElForEpisode = episodeNumber => document.querySelector(`[href^="/show/${formatNumberToEpisodeUrlFormat(episodeNumber)}/"]`);
const markShowAsFinished = episodeNumber => { showLinkElForEpisode(episodeNumber).style = FINISHED_SHOW_STYLE; };
const storeShowAsFinished = episodeNumber => { localStorage[episodeNumber] = true; };
audioEl.addEventListener("ended", e => {
const endedEpisodeNumber = scrapeCurrentEpisodeNumber();
storeShowAsFinished(endedEpisodeNumber);
markShowAsFinished(endedEpisodeNumber);
});
const markAllStoredFinishedShowsAsSuch = () => {
const allSavedKeys = Object.keys(localStorage);
allSavedKeys.forEach(savedKey => Number.isInteger(+savedKey) && localStorage[savedKey] ? markShowAsFinished(savedKey) : (0));
};
markAllStoredFinishedShowsAsSuch();
}
main();
@cuylerstuwe
Copy link
Author

1.1:

Switched to using localStorage, because Tampermonkey's working method for inserting items into the context menu is via adding @run-at context-menu to separate userscripts (GM_*Value variables are userscript-scoped and therefore wouldn't have been open to manipulation by separate userscripts).

@cuylerstuwe
Copy link
Author

1.2:

Changed name of markAllStoredFinishedShows to markAllStoredFinishedShowsAsSuch.

A little wordier, but feels marginally better at expressing at-a-glance what it it does.

@cuylerstuwe
Copy link
Author

How To Install

  1. Install Tampermonkey: https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=en

  2. Click Raw button in this Gist (currently toward the upper-right of GitHub Gist's interface):

Location of Raw Button

How To Use

When a Syntax.FM podcast episode reaches its end, it will become semitransparent.

The status of episodes finished as a given user on a given device is persisted across page loads.

@cuylerstuwe
Copy link
Author

1.3:

Fix include path, so that the script always triggers when necessary.

@cuylerstuwe
Copy link
Author

1.4:

Fix for episode URL formatting (episodes under 100 have leading zeroes in the URL):

Leading Zeroes Episode URL Example

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