Letterboxd Extra Profile Stats

Adds average number of films watched per month and per week to profile pages

当前为 2014-08-27 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Letterboxd Extra Profile Stats
// @namespace   https://github.com/rcalderong/userscripts
// @description Adds average number of films watched per month and per week to profile pages
// @copyright   2014, Ramón Calderón (http://rcalderon.es)
// @homepageURL https://github.com/rcalderong/userscripts
// @supportURL  https://github.com/rcalderong/userscripts/issues
// @icon        https://raw.githubusercontent.com/rcalderong/userscripts/master/img/letterboxd_icon.png
// @license     GPLv3; http://www.gnu.org/licenses/gpl.html
// @version     1.0
// @include     /^http:\/\/(www.)?letterboxd.com\/[\w]+\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/activity\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/films\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/lists\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/people\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/settings\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/invitations\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/about\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/pro\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/welcome\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/legal\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/api\/$/
// @exclude     /^http:\/\/(www.)?letterboxd.com\/contact\/$/
// @grant       none
// ==/UserScript==

(function () {
    var diaryURL,   // URL of the user's film diary
        filmsMonth, // Average number of films watched per month
        filmsWeek,  // Average number of films watched per week
        filmsYear,  // Number of films watched this year
        statsElt;   // Page element that contains user statistics

    // Get data from page
    statsElt = document.querySelector("ul.stats");
    diaryURL = statsElt.children[1].children[0].getAttribute("href");
    filmsYear = statsElt.children[1].children[0].children[0].innerHTML;

    // Calculate averages
    filmsMonth = +(filmsYear / (new Date().getMonth() + 1)).toFixed(1);
    filmsWeek = +((filmsMonth / 30) * 7).toFixed(1);

    // Remove zero after decimal point, if present
    [filmsMonth, filmsWeek].map(function (n) {
        return (n === parseInt(n)) ? parseInt(n) : n;
    });

    // Insert calculated averages in page
    [filmsWeek, filmsMonth].forEach(function (filmsAvg) {
        var infoElt = document.getElementsByClassName("profile-person-info")[0],
            linkElt = document.createElement("a"),
            newElt = document.createElement("li"),
            numberElt = document.createElement("strong"),
            textElt = document.createElement("span");
        
        linkElt.setAttribute("href", diaryURL);
        numberElt.innerHTML = filmsAvg;
        textElt.innerHTML = (filmsAvg === filmsWeek) ? "Per week" : "Per month";

        linkElt.appendChild(numberElt);
        linkElt.appendChild(textElt);
        newElt.appendChild(linkElt);
        
        statsElt.insertBefore(newElt, statsElt.children[2]);
        infoElt.style.width = infoElt.style.maxWidth =
                infoElt.offsetWidth - newElt.offsetWidth + "px";
    });
}());