Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arousle/e903f6153ba7c32b5b6843a08e37f79b to your computer and use it in GitHub Desktop.
Save arousle/e903f6153ba7c32b5b6843a08e37f79b to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name F-List Profile Word Counter
// @namespace arousle:f-list-word-counter
// @version 2024-04-05
// @description Counts the number of unique words on an F-List profile.
// @author Arousle
// @match https://www.f-list.net/c/*
// @require https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js
// @icon https://www.google.com/s2/favicons?sz=64&domain=f-list.net
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to count unique words
function countUniqueWords(text) {
// Convert text to lowercase and split into words
let words = text.toLowerCase().match(/\b[\w'’-]+\b/g); // Include apostrophe within word boundaries
// Remove duplicates using lodash
let uniqueWords = _.uniq(words);
// Return the count of unique words
console.log(uniqueWords);
return uniqueWords.length;
}
// Function to find elements with class "FormattedBlock" and concatenate their text
function concatenateFormattedBlockText() {
let formattedBlocks = document.querySelectorAll('.FormattedBlock');
let concatenatedText = '';
formattedBlocks.forEach(function(block) {
concatenatedText += ' ' + block.textContent;
});
return concatenatedText.trim(); // Remove leading/trailing whitespace
}
// Function to find <a> elements with class "Character_CustomFetish" and concatenate their "rel" attributes
function concatenateCustomFetishRelAttributes() {
let customFetishes = document.querySelectorAll('a.Character_CustomFetish[rel]');
let concatenatedText = '';
customFetishes.forEach(function(fetish) {
concatenatedText += ' ' + fetish.getAttribute('rel');
});
return concatenatedText.trim(); // Remove leading/trailing whitespace
}
// Function to find <div> elements with class "CollapseHeader" and concatenate their text content
function concatenateCollapseHeaderText() {
let collapseHeaders = document.querySelectorAll('div.CollapseHeader');
console.log(collapseHeaders);
console.log("foo");
let concatenatedText = '';
collapseHeaders.forEach(function(header) {
concatenatedText += ' ' + header.textContent;
});
return concatenatedText.trim(); // Remove leading/trailing whitespace
}
// Function to preprocess text by replacing special characters
function preprocessText(text) {
// Replace special characters like ’ with '
return text.replace(/’/g, "'");
}
// Function to count unique words across all elements
function countUniqueWordsAcrossAllElements() {
// Concatenate text from all elements
let formattedBlockText = concatenateFormattedBlockText();
let customFetishRelAttributes = concatenateCustomFetishRelAttributes();
let collapseHeaderText = concatenateCollapseHeaderText();
// Preprocess the text
let allText = preprocessText(formattedBlockText + ' ' + customFetishRelAttributes + ' ' + collapseHeaderText);
// Count unique words
let uniqueWordCountDesc = countUniqueWords(preprocessText(formattedBlockText + ' ' + collapseHeaderText));
let uniqueWordCountKinks = countUniqueWords(customFetishRelAttributes);
// Create a div element to display the count
let countElement = document.createElement('div');
countElement.textContent = 'Vocabulary (description/customs): ' + uniqueWordCountDesc + '/' + uniqueWordCountKinks;
countElement.setAttribute("style", "color: #deadbeef;");
// Find the first FormattedBlock element
let firstFormattedBlock = document.querySelector('.FormattedBlock');
// Insert the count element before the first FormattedBlock
if (firstFormattedBlock) {
firstFormattedBlock.parentNode.insertBefore(countElement, firstFormattedBlock);
} else {
// If no FormattedBlock elements found, append the count element to the body
document.body.appendChild(countElement);
}
}
// Call the function to count unique words across all elements
countUniqueWordsAcrossAllElements();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment