6

I am new to Firefox addon development.

I need a way to call a contentscript function from main.js in firefox addon.

I have injected contentscript xyz.js on every opening webpage.

I want to call function abc() present in my contentscript xyz.js from my main.js on click of a button which i have place in navigation toolbar.

Below is my code.

Main.js

..
function addToolbarButton() {
    var document = mediator.getMostRecentWindow('navigator:browser').document;        
    var navBar = document.getElementById('nav-bar');
    if (!navBar) {
        return;
    }
    var btn = document.createElement('toolbarbutton');  
    btn.setAttribute('id', 'mybutton-id');
    btn.setAttribute('type', 'button');
    btn.setAttribute('class', 'toolbarbutton-1');
    btn.setAttribute('image', data.url('icon_16.png'));
    btn.setAttribute('orient', 'vertical');
        btn.setAttribute('label', 'Test');
        btn.addEventListener('click', function() {
            tabs.activeTab.attach({
            //

                abc()     //here i want to call the function present in my contentscript 

            //
        });
        }, false)
    navBar.appendChild(btn);
}

..

xyz.js

..

function abc(){
//here is my code logic
}

..

I came to know that message passing is way to do so but unable to implement in firefox.

Please help me i have got stuckd.

2 Answers 2

4

You cannot call the function directly, you need to send a message to the content script. Meaning something like that:

var worker = tabs.activeTab.attach({
  ...
});

// Some time later
worker.postMessage("doABC");

And in the content script:

self.on("message", function(message) {
  if (message == "doABC")
    abc();
});

For more information on communicating with content scripts see documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

This is a very common question on SO - should we start linking to a canonical answer instead of re-answering?
@canuckistani: Is there a canonical answer? My answer here is just an expanded RTFM. Most of the time however it is people who just cannot wrap their mind around the messaging approach of the SDK. I would be all for closing these questions as duplicates but there is never a question that's similar enough (probably an indication of "too localized" actually).
I reviewed the recent history of these questions and agree - all are just different enough. Thanks for having the patience to point them in the right direction tho.
1

According to documentation it should work this way;

However I have similar question Accessing pre-loaded content script from ActionButton not yet resolved.

// main.js
function handleClick(state) {
    var myWorker = tabs.activeTab.attach({

    });   
    myWorker.port.emit("initialize", "Message from the add-on");
}

// content.js
/*BEGIN Listen events coming from Add-on script*/
self.port.on("initialize", function () {
    alert('self.port.on("initialize")');
    return;   
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.