Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for tampermonkey to add articles count in selection list (My list, archive, favorites)
// ==UserScript==
// @name Pocket Counters
// @namespace http://vanadium23.me/
// @version 0.2
// @description Add counters to pocket list
// @author vanadium23
// @match https://getpocket.com/a/*/
// @ran-at document-end
// @grant none
// ==/UserScript==
(function () {
'use strict';
let globalState = {
counters: {
queue: 0,
favorite: 0,
archive: 0,
},
articles: {
queue: {},
favorite: {},
archive: {},
},
};
const ARTICLE_TIME_PROPERTY = {
queue: 'time_added',
favorite: 'time_favorited',
archive: 'time_read',
};
const ARTICLE_STATES = {
queue: 'section-mylist',
favorite: 'section-favorites',
archive: 'section-archive',
};
const states = Object.keys(ARTICLE_STATES);
const COUNTER_POSTFIX = '-counter';
const FAKE_COUNT_NUMBER = 1000;
// fetch all articles
function fetchArticles(state, offset = 0, count = 100) {
const object = {
offset,
count,
state,
sort: 'newest',
formCheck: window.formCheck,
};
if (state == 'favorite') {
object.state = '';
object.favorite = 1;
}
let body = '';
Object.keys(object).forEach(key => body += `${key}=${object[key]}&`);
body = body.slice(0, -1);
return fetch('/a/x/get.php', {
body: body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
method: 'POST',
credentials: 'include',
})
.then(response => response.json());
}
function countArticles(state, offset = 0) {
fetchArticles(state, offset)
.then(data => {
// if meta result == 1000, then it is not a real count
let count = parseInt(data.search_meta.total_result_count);
let metaCount = true;
if (count == FAKE_COUNT_NUMBER) {
count = offset === 0 ? FAKE_COUNT_NUMBER : Object.keys(data.list).length;
metaCount = false;
}
// update storage
globalState.counters[state] += count;
Object.assign(globalState.articles[state], data.list);
// update UI
drawCounter(state);
// fetch next pack
if (count !== 0 && !metaCount) {
countArticles(state, globalState.counters[state]);
}
});
}
function getMonday(date) {
var day = date.getDay() || 7;
if (day !== 1)
date.setHours(-24 * (day - 1));
return date;
}
function drawCounter(state) {
for (let counterType of ['', '-today-', '-week-']) {
const selector = `.${ARTICLE_STATES[state]}${counterType}${COUNTER_POSTFIX}`;
const counter = document.querySelector(selector);
if (counter) {
let unreadCount = '?';
if (counterType === '') {
unreadCount = globalState.counters[state];
} else if (counterType === '-today-') {
const timeProperty = ARTICLE_TIME_PROPERTY[state];
const articles = globalState.articles[state];
const today = new Date();
today.setHours(0, 0, 0, 0);
unreadCount = Object.keys(articles).filter(
itemId => (parseInt(articles[itemId][timeProperty]) * 1000) > today
).length;
} else if (counterType === '-week-') {
const timeProperty = ARTICLE_TIME_PROPERTY[state];
const articles = globalState.articles[state];
const monday = getMonday(new Date());
monday.setHours(0, 0, 0, 0);
unreadCount = Object.keys(articles).filter(
itemId => (parseInt(articles[itemId][timeProperty]) * 1000) > monday
).length;
}
counter.innerHTML = ` (${unreadCount})`;
}
}
}
function setupCounter(listClass) {
const selector = `.${listClass}`;
const listElement = document.querySelector(selector);
const counterSelector = `${selector}${COUNTER_POSTFIX}`;
const counterElement = document.querySelector(counterSelector);
if (listElement && !counterElement) {
const detailedStatistics = `
<div>
<style>
.pocket-counter {
font-weight: 200;
margin-bottom: 0px;
margin-left: 5px;
}
</style>
<p class="pocket-counter">Today: <span class="${listClass}-today-${COUNTER_POSTFIX}">?</span></p>
<p class="pocket-counter">Week: <span class="${listClass}-week-${COUNTER_POSTFIX}">?</span></p>
<p class="pocket-counter">Total: <span class="${listClass}${COUNTER_POSTFIX}">?</span></p>
</div>
`;
listElement.innerHTML = listElement.innerHTML + detailedStatistics;
}
}
for (let state of states) {
const listClass = ARTICLE_STATES[state];
setupCounter(listClass);
countArticles(state);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment