Skip to content

Commit

Permalink
♻️ REFACTOR: [tvmaze] improve counter script
Browse files Browse the repository at this point in the history
  • Loading branch information
vBm committed Jan 2, 2024
1 parent bd54e22 commit b2524e1
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions counter.tvmaze.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,38 @@
// @namespace tvmaze
// @description Display how many episodes are left to watch
// @icon https://tvmazecdn.com/images/favico/favicon.ico
// @author vBm <vbm@omertabeyond.com>
// @author vBm <the.vbm@gmail.com>
// @oujs:author vBm
// @license The MIT License (MIT)
// @contributionURL https://www.paypal.me/thevbm/3
// @contributionAmount €3.00
// @supportURL https://github.com/vBm/snippets/issues
// @include http://www.tvmaze.com/watchlist*
// @include https://www.tvmaze.com/watchlist*
// @version 0.5
// @date 25/12/2017
// @match http://www.tvmaze.com/watchlist*
// @match https://www.tvmaze.com/watchlist*
// @version 0.6
// @date 01/01/2024
// @grant none
// ==/UserScript==

var totalEpisodesSum, totalEpisodes = [];

$('.watched-eps').each(function() {
totalEpisodes.push(
$(this).text().trim().split('/').map(el => +el).reverse().reduce((a, b) => a - b)
);
const totalEpisodes = Array.from(document.querySelectorAll('.watched-eps')).map(element => {
return element.textContent.trim().split('/').map(element => +element).reverse().reduce((a, b) => a - b);
});

totalEpisodesSum = totalEpisodes.reduce((a, b) => a + b);
const totalEpisodesSum = totalEpisodes.reduce((a, b) => a + b, 0);

localStorage.setItem('totalEps', totalEpisodesSum);

$('#filter').append(
$('<div>').append(
$('<span>').attr({
class: 'grid-x grid-margin-x align-center',
id: 'remaining'
}).text(`Remaining episodes to watch: ${localStorage.totalEps} from ${$('.watched-eps').length} shows`)
)
);
const filterElement = document.getElementById('filter');
const remainingSpan = document.createElement('span');
remainingSpan.classList.add('grid-x', 'grid-margin-x', 'align-center');
remainingSpan.id = 'remaining';
remainingSpan.textContent = `Remaining episodes to watch: ${localStorage.totalEps} from ${document.querySelectorAll('.watched-eps').length} shows`;

filterElement.appendChild(document.createElement('div').appendChild(remainingSpan));

$('.watch-dropdown').change(function(){
if ($(this).val() === '0') {
localStorage.setItem('totalEps', (localStorage.totalEps - 1));
$('#remaining').text(`Remaining episodes to watch: ${localStorage.totalEps} from ${$('.watched-eps').length} shows`);
document.querySelector('.watch-dropdown').addEventListener('change', (event) => {
if (event.target.value === '0') {
localStorage.setItem('totalEps', localStorage.totalEps - 1);
remainingSpan.textContent = `Remaining episodes to watch: ${localStorage.totalEps} from ${document.querySelectorAll('.watched-eps').length} shows`;
}
});

0 comments on commit b2524e1

Please sign in to comment.