Put your code in content.js, add a new background.js like this:
chrome.action.onClicked.addListener(tab => {
chrome.scripting.executeScript({
target: {tabId:
tab.id},
files: ['content.js'],
});
});
and declare background.js in manifest.json:
"background": {
"service_worker": "background.js"
}