I am coding a simple extension to save the html code of a certain class in a website when clicked from the extensions menu in Chrome. Everything works successfully when I tested my code in the Chrome Developer Console, however when I try to fit it to an extension, it doesn’t work.
Error handling response: TypeError: Error in invocation of scripting.executeScript(scripting.ScriptInjection injection, optional function callback): No matching signature.
My code is below:
manifest.json
{
"name": "AOPS Saver",
"version": "1.1",
"manifest_version": 3,
"description": "Saves current AOPS problem to file",
"action": {
"default_title": "???",
"default_popup": "popup.html"
},
"permissions": [
"scripting",
"activeTab"
]
}
popup.html
<!DOCTYPE html>
<html>
<head>
<title>My Extension</title>
<script src="popup.js"></script>
</head>
<body>
<button id="execute">Execute</button>
</body>
</html>
popup.js
function saveToFile(text, filename) {
var blob = new Blob([text], {type: 'text/plain'});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
}
// Get the current tab's URL
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
let tabID = tabs[0].id;
let tabURL = tabs[0].url
// Get the tab's content
if (tabURL == 'https://artofproblemsolving.com/alcumus/problem'){
chrome.scripting.executeScript( {target: {tabId: tabID}}, {
code: 'document.documentElement.outerHTML'}, function(result) {
// Check for elements with the class "alc-solution-text"
let doc = new DOMParser().parseFromString(result[0], 'text/html');
let elements = doc.getElementsByClassName('alc-solution-text');
var str = elements[0]
var str = str.innerHTML
var container = $('<div>').html(str);
// replace LaTeX images with alt text
container.find('img').replaceWith(function() { return this.alt; })
var strAlt = container.html();
var container = document.getElementById("page-wrapper");
var div = document.createElement("div");
div.className = "my-class";
div.innerHTML = strAlt;
container.appendChild(div);
// Save to file
saveToFile(div.innerText,'problems.txt')
},
)};
});