Minds Show Impressions

Shows the number of impressions/views on all posts.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Minds Show Impressions
// @namespace    http://www.minds.com/
// @version      0.1
// @description  Shows the number of impressions/views on all posts.
// @author       You
// @match        https://www.minds.com/*
// @grant        none
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==

function getCookie(name) {
    var value = "; " + document.cookie;
    var parts = value.split("; " + name + "=");
    if (parts.length == 2) {
        return parts.pop().split(";").shift();
    }
}

function http(method, url, payload, callback) {
    $.ajax({
        method: method,
        url: url,
        headers: {
            'x-xsrf-token': getCookie('XSRF-TOKEN')
        }
    }).done(function(ret) {
        callback(ret);
    });
}

function getImpressions(id, callback) {
    http('GET', 'https://www.minds.com/api/v1/newsfeed/single/'+ id, '', function(ret) {callback(ret.activity.impressions)});
}

var hash = []
function updateViews() {
    var pl = document.getElementsByClassName('permalink');
    for(var i = 0; i < pl.length; i++) {
        if (hash.indexOf(pl[i]) != -1) {
            continue;
        }
        hash.push(pl[i]);

        // Needed to create separate closures or will
        // otherwise capture the same element.
        var ff = function(el) {
            return function(views) {
                el.childNodes[0].innerHTML += `<br>${views} views<br>`;
            };
        }

        var match = /\/([^\/]*)$/.exec(pl[i].href);
        if (match != null) {
            var id = match[1];
            getImpressions(id, ff(pl[i]));
        }
    }
}
// A simple callback on document or window load is not enough, because
// Minds loads content dynamically and subsequently loaded posts would
// be loaded unnoticed by this script. Therefor, we the following will
// need an observer to notify it of document changes. But we must hash
// the elements observed or this will cause an inifite loop as changes
// to the DOM cause renewed triggers of the observer.
var callback = function(mutationsList) {
    mutationsList.forEach((mutation) => {
        if (mutation.type == 'childList') {
            updateViews();
        }
    });
};
var observer = new MutationObserver(callback);
observer.observe(document.body, { childList: true, subtree: true });