I want to inject custom code to the web page after the user clicks on my chrome extension icon or clicks on a button inside the chrome extension popup. I have seen this feature been done in **loom **(Screen Recorder) in which when the user clicks on the loom icon, it shows a popup for loading and then injects its own custom code to the user's web page in the image, it injects its own code inside the DOM to start recording
I also want to do something like this, but not sure how exactly I should go about this,
I try this :-
manifest.json
{
"manifest_version": 3,
"name": "My Extension",
"version": "1.0",
"description": "Description of my extension",
"permissions": ["activeTab"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"background": {
"service_worker": "background.js"
}
}
background.js
chrome.action.onClicked.addListener(async function(tab) {
chrome.tabs.executeScript({
file: "content.js"
});
});
content.js
const overlayButton = document.createElement("button");
overlayButton.innerText = "Record";
// Add a click event listener to the button
overlayButton.addEventListener("click", function() {
// Code to handle button click event
});
// Add the overlay button to the page
document.body.appendChild(overlayButton);
But I got this error in the chrome extension :-
I am not really sure what is best practise to inject my own code inside the user's web page. Also how should I do it without writing the .innerHTML and the whole html inside it
Thank you :)