// ==UserScript==
// @name Kiwifarms subforum post counter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Tally user posts in each kf subforum and generate a pie chart
// @author n
// @match *://kiwifarms.st/search*
// @grant none
// ==/UserScript==
(function() {
'use strict';
let currentPage = getCurrentPageNumber(); // the current page number
const maxPages = 8; // how many pages there are until "view older discussion"
let olderResultsClicks = 0; // older results click
const maxOlderResultsClicks = 1; // this is the "view older discussion button" which will reset the loop
const subforumTally = JSON.parse(localStorage.getItem('subforumTally')) || {};
const nWordCounter = 0;
const trannyCounter = 0;
function getCurrentPageNumber() {
const pageParam = new URLSearchParams(window.location.search).get('page');
return pageParam ? parseInt(pageParam, 10) : 1;
}
function tallyPosts() {
const rows = document.querySelectorAll('.contentRow');
rows.forEach(row => {
const forumElem = row.querySelector('div.contentRow-minor li:last-child a');
if (forumElem) {
const forumName = forumElem.textContent.trim();
subforumTally[forumName] = (subforumTally[forumName] || 0) + 1;
}
});
localStorage.setItem('subforumTally', JSON.stringify(subforumTally));
console.log('Current Tally:', subforumTally);
navigateToNextPageOrClickOlder();
}
function navigateToNextPageOrClickOlder() {
if (currentPage < maxPages) {
currentPage++;
window.location.search = `?page=${currentPage}`;
} else if (olderResultsClicks < maxOlderResultsClicks) {
const olderResultsButton = document.querySelector('.button--link.button');
if (olderResultsButton) {
olderResultsClicks++;
olderResultsButton.click();
}
} else {
console.log('Final Tally:', subforumTally);
localStorage.removeItem('subforumTally');
}
}
function displayPieChart() {
const ctx = document.getElementById('tallyChart').getContext('2d');
const labels = Object.keys(subforumTally);
const data = Object.values(subforumTally);
new Chart(ctx, {
type: 'pie',
data: {
labels: labels,
datasets: [{
data: data,
backgroundColor: labels.map(() => '#' + (Math.random() * 0xFFFFFF << 0).toString(16))
}]
}
});
setTimeout(saveChartAsImage(), 2500);
}
function addSaveImageButton() {
const saveImageButton = document.createElement('button');
saveImageButton.textContent = 'Save Chart as Image';
saveImageButton.style.display = 'block';
saveImageButton.style.marginTop = '20px';
saveImageButton.onclick = saveChartAsImage;
document.body.insertBefore(saveImageButton, document.getElementById('tallyChart').nextSibling);
}
function saveChartAsImage() {
const canvas = document.getElementById('tallyChart');
const imgURL = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.download = 'chart.png'; // todo get username for filename
link.href = imgURL;
document.body.appendChild(link);
setTimeout(link.click(), 1000); // wait for 1.0s so page can load before downloading chart
document.body.removeChild(link);
}
function resetLocalStorageData() {
localStorage.removeItem('subforumTally');
location.reload();
}
function stopTallying() {
localStorage.removeItem('isRunning');
console.log('Tallying stopped. Current Tally:', subforumTally);
const canvas = document.createElement('canvas');
canvas.id = 'tallyChart';
canvas.width = 400;
canvas.height = 400;
document.body.insertBefore(canvas, document.body.firstChild);
// Load Chart.js externally
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/chart.js';
script.onload = displayPieChart;
document.head.appendChild(script);
}
function startTallying() {
localStorage.setItem('isRunning', 'true');
tallyPosts();
}
// Add control buttons
const startButton = document.createElement('button');
startButton.textContent = 'Start Tally';
startButton.onclick = startTallying;
const stopButton = document.createElement('button');
stopButton.textContent = 'Stop Tally';
stopButton.onclick = stopTallying;
const resetStorageButton = document.createElement('button');
resetStorageButton.textContent = 'Reset';
resetStorageButton.onclick = resetLocalStorageData;
const buttonContainer = document.createElement('div');
buttonContainer.style.position = 'fixed';
buttonContainer.style.top = '10px';
buttonContainer.style.right = '10px';
buttonContainer.style.zIndex = '9999';
buttonContainer.appendChild(startButton);
buttonContainer.appendChild(stopButton);
buttonContainer.appendChild(resetStorageButton);
document.body.appendChild(buttonContainer);
// Automatically start tallying
if (localStorage.getItem('isRunning') === 'true') {
setTimeout(startTallying, 2000);
}
// subforum list minimized TODO --- lolcow_cults | lolcow_salon | lolcow_farms | off_topic | autistic_thunderdome | lounges
const lolcow_cults=["Lolcows","Internationale Clique","Christian Weston Chandler","Ethan Ralph / #Killstream","Phil Burnell / DarkSydePhil",],lolcow_salon=["Beauty Parlour","Deathfats","Stinkditch","Off-Topic",],lolcow_farms=["Community Watch","Animal Control","Catboy Deconversion Therapy","Internet Famous","Internet Tough Guys","Rat Kings","Weeb Wars",],off_topic=["General Discussion","Art & Literature","The Bidness","Food","Games","Health & Fitness","Internet & Technology","Multimedia","Music","Q&A"],autistic_thunderdome=["Articles & News","Deep Thoughts","Happenings","Mass Debates",],lounges=["Top-Secret Inner Circle","Supporters",];
})();