TVMaze: add "Watched" button to calendar

Add "Mark as watched" button to calendar entries at TVMaze

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        TVMaze: add "Watched" button to calendar
// @description Add "Mark as watched" button to calendar entries at TVMaze
// @namespace   BlackNullerNS
// @include     http*://www.tvmaze.com/calendar*
// @version     1.2
// ==/UserScript==

var btn = document.createElement('button');
btn.innerHTML = '✓';
btn.style.fontWeight = 'bold';
btn.style.border = 0;
btn.style.padding = '2px 4px';
btn.style.background = 'transparent';
btn.style.color = '#3C948B';
btn.style.cursor = 'pointer';
btn.setAttribute('title', 'Mark as watched');

var handler = function (e) {
    e.preventDefault();

    var self = this,
        entry = this.parentNode.parentNode,
        link = entry.querySelector('a[href*="episodes/"]');

    if (!link) {
        return false;
    }

    var eid = link.getAttribute('href').split('episodes/')[1].split('/')[0];
    var url = '/watch/set?episode_id=' + eid;
    var csrf = document.querySelector('meta[name="csrf-token"]').getAttribute('content');

    var xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    xhr.setRequestHeader('X-CSRF-Token', csrf);

    xhr.addEventListener('load', function () {
        entry.classList.add('watched');
        self.remove();
    });

    xhr.send("type=0");
};

var mouseover = function () {
    this.style.color = '#cc0000';
};

var mouseout = function () {
    this.style.color = '#3C948B';
};

var cloned;
var episodes = document.querySelectorAll(".entry:not(.watched)");

for (var i = 0, l = episodes.length; i < l; i++) {
    cloned = btn.cloneNode(true);
    cloned.addEventListener('click', handler);
    cloned.addEventListener('mouseover', mouseover);
    cloned.addEventListener('mouseout', mouseout);

    episodes.item(i).firstElementChild.appendChild(cloned);
}