I am developing a chrome extension with manifest v2 but now google requires manifest v3. So when I try to migrate from v2 to v3, the extension is not working. But with v2 the extension was working fine.
I'm trying for the last 2 weeks but none of my ideas is working correctly. So please help and describe your ideas. Thanks, I'm a beginner in English so please don't mind that.
manifest.json v2
{
"manifest_version": 2,
"name": "Text Tool",
"description": "A professional chrome extension for word and character count with and without spaces from selected text from any website.",
"version": "1.0",
"permissions": ["activeTab", "downloads"],
"browser_action": {
"default_title": "Text Tool",
"default_popup": "popup.html"
},
"icons": {
"16": "icons/icon16.png",
"32": "icons/icon32.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
}
popup.js
function updateCounts(selectedText) {
if (!selectedText) {
return;
}
var words = selectedText.match(/\b[-?(\w+)?]+\b/gi);
var uniqueWords = [...new Set(words)];
var wordCount = words?.length;
var uniqueWordCount = uniqueWords?.length;
var charCountWithSpaces = selectedText.length;
var charCountWithoutSpaces = selectedText.replace(/\s+/g, '').length;
var sentenceCount = selectedText.split(/[.!?]+/g).filter(Boolean).length;
var paragraphCount = selectedText.split(/\n+/g).filter(Boolean).length;
var textSize = (selectedText.length / 1024).toFixed(2) + ' KB';
var totalWordLength = words.reduce((total, word) => total + word.length, 0);
var avgWordLength = (totalWordLength / wordCount).toFixed(2);
var avgSentenceLengthWords = (wordCount / sentenceCount).toFixed(2);
var avgSentenceLengthChars = (charCountWithSpaces / sentenceCount).toFixed(2);
document.getElementById('word-count').innerHTML = wordCount;
document.getElementById('unique-words').innerHTML = uniqueWordCount;
document.getElementById('char-count-with-spaces').innerHTML = charCountWithSpaces;
document.getElementById('char-count-without-spaces').innerHTML = charCountWithoutSpaces;
document.getElementById('sentence-count').innerHTML = sentenceCount;
document.getElementById('paragraph-count').innerHTML = paragraphCount;
document.getElementById('text-size').innerHTML = textSize;
document.getElementById('avg-word-length').innerHTML = avgWordLength;
document.getElementById('avg-sentence-length-words').innerHTML = avgSentenceLengthWords;
document.getElementById('avg-sentence-length-chars').innerHTML = avgSentenceLengthChars;
// Calculate reading time (based on 275 words per minute)
var readingTime = Math.ceil(wordCount / 275);
document.getElementById('reading-time').innerHTML = readingTime + ' minute(s)';
// Calculate speaking time (based on 150 words per minute)
var speakingTime = Math.ceil(wordCount / 150);
document.getElementById('speaking-time').innerHTML = speakingTime + ' minute(s)';
// Calculate handwriting time (based on 20 words per minute)
var handwritingTime = Math.ceil(wordCount / 20);
document.getElementById('handwriting-time').innerHTML = handwritingTime + ' minute(s)';
}
document.addEventListener("DOMContentLoaded", function() {
chrome.tabs.executeScript({
code: "window.getSelection().toString();"
}, function(selection) {
const selectedText = selection[0];
document.getElementById("selectedText").textContent = selectedText;
updateCounts(selectedText);
});
document.getElementById("copyButton").addEventListener("click", function() {
const textarea = document.getElementById("selectedText");
textarea.select();
document.execCommand("copy");
});
document.getElementById("exportButton").addEventListener("click", function() {
const selectedText = document.getElementById("selectedText").textContent;
const blob = new Blob([selectedText], { type: "text/plain;charset=utf-8" });
const url = URL.createObjectURL(blob);
chrome.downloads.download({
url: url,
filename: "selectedText.txt",
saveAs: true
});
});
});