How to execute content script from the sidebar?

I am trying to execute a content script when a button in the toolbar is pressed.

But I get an error: Failed to execute content script: Missing host permission for the tab

What am I missing? If anyone can point to an example extension that executes content scripts from the sidebar, then please let me know.

What permissions do you have? activeTab?

Here is the set permissions:

"permissions": [
    "<all_urls>",
    "activeTab",
    "storage",
    "notifications"
  ],

What exactly is it that you want to achieve? A toolbar button or a sidebar?

What tab are you trying to execute the content script in (as in, what site is loaded there)?

@freaktechnik

Thanks for asking the right questions.

I am building a form filler. In the sidebar I will have a button. When the button is clicked the form in the active tab tab should be filled. This is how my proof of concept extension look:

It is very basic and can be found on github:

Based on that it seems that you’re doing everything right, though the code is missing the interesting bits currently, I guess :wink:

Generally, you’d want your page to be a http/https page. Which it seems you are using, so you should be fine.

Generally the examples listed on https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/executeScript should apply for you. It doesn’t matter if you do something from a sidebar or the background page in terms of extension APIs.

Yes. The test page is here:
https://kjemmo.github.io/test-pages/

OK, so it seems I should use tabs.executeScript().

But from where? where does the code belong in the file system? in my sidebar.js, my content-script.js or background.js? Sorry, I am just a bit confused.

So how and where do I place the function that fills the form:

function fillForm( {
document.querySelector('[name="username"]').value="myusername"
}

And how do I execute my function from the sidebar button?

Wherever you handle the click, which would typically be in your sidebar.js

By running the function… Depending on if and how there is extra data that may of course vary. But you can both run JS code you define as a string or code from a js file with executeScript, so you should be able to find a solution that works for you.