Greasy Fork

Anime History

Highlights episodes that have been visited

目前为 2016-08-28 提交的版本。查看 最新版本

// ==UserScript==
// @name        Anime History
// @namespace   https://greasyfork.org/en/users/62848-klesus
// @description Highlights episodes that have been visited
// @include     https://twist.moe/a/*
// @version     1.1
// @grant       GM_getValue
// @grant       GM_setValue
// @license     https://www.gnu.org/licenses/gpl-3.0-standalone.html
// ==/UserScript==
"use strict";

//inject our own styling we're gonna apply
var node = document.createElement('style');
node.innerHTML = '.visited {background-color: #e53232 !important}';
document.body.appendChild(node);

//fetch which show we're watching and what episode
var aniep = window.location.pathname.split('/').splice(2);
var anime = aniep[0];
var currentepisode = parseInt(aniep[1], 10);
var episodelist = document.getElementsByClassName('episode-list') [0].children[0].children;
var watchedepisodes = [];
var hasMoreButton = function(){
  var more = document.querySelector(".more");
  return more === null ? 0 : 1;
}();

//fetch watched episode-list
function getEpisodes(show) {
  var episodes = GM_getValue(show, "[]");
  return JSON.parse(episodes);
}

//populate list with watched episodes
function setEpisodes(show, episodes) {
  GM_setValue(show, JSON.stringify(episodes));
}

function currentEpisodeAlreadyWatched(episode, index, something) {
  return episode === currentepisode;
}

//check which episodes we've visited
watchedepisodes = getEpisodes(anime);

//add the current episode to the list, but only if we haven't watched it before
if (!watchedepisodes.find(currentEpisodeAlreadyWatched)) {
  watchedepisodes.push(currentepisode);
}

//add the visited class to watched episodes
watchedepisodes.forEach(function (episode) {
  episodelist[episode - 1 + hasMoreButton].children[0].classList.add('visited');
});

//overwrite the new updated watchlist to localstorage
setEpisodes(anime, watchedepisodes);