can floating-menu show word count like popclip did?

I've recently been trying to use BTT to replace PopClip, and it's been quite successful so far. However, there's an extension in PopClip that can automatically count the number of words in selected text. Is it possible to achieve a similar function in BTT?

here is a simple word count widget you can drag into your text selection menu:

word_count.bttfloatingmenuitems (4.3 KB)

It uses a very basic approach to count words:

async function itemScript(itemUUID) {
	let text = await get_string_variable("BTTTextSelection");

	// Trim and split on whitespace, filter out empties
	let words = text.trim().split(/\s+/).filter(w => w.length > 0);
	let wordCount = text.trim() === "" ? 0 : words.length;

	let content = {
		BTTMenuItemBackgroundColor : "50,70,150,166.991",
		BTTMenuItemText: wordCount.toString()
	};
	
	return JSON.stringify(content);
}
1 Like

Thank you very much. Based on this version, I have modified it to support Chinese word count and posted it below for those who need it.

async function itemScript(itemUUID) {
	const text = (await get_string_variable("BTTTextSelection")).trim();
	const matches = text.match(/[\u4e00-\u9fff]|[a-zA-Z]+/g);
	const count = matches ? matches.length : 0;

	return JSON.stringify({
		BTTMenuItemText: `${count}字`
	});
}