Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a content script function from main.js in firefox addon

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.

like image 558
Shashank.gupta40 Avatar asked Oct 04 '12 04:10

Shashank.gupta40


People also ask

How do I run a script in Firefox?

Firefox 89 and laterSelect Settings menu (3 horizontal lines ≡ icon in the upper right corner) More Tools -> Web Developer Tools -> Console tab. Type JavaScript code after the web console prompt » and press Enter .

How can background scripts & Content scripts communicate?

Communication between extensions and their content scripts works by using message passing. Either side can listen for messages sent from the other end, and respond on the same channel.

How do you send data from content script to popup in HTML?

The popup can contain any HTML contents that you like, and it's automatically sized to fit its contents. To add a popup to your browser action, create an HTML file with the popup's contents. Specify the HTML file in the default_popup field of browser_action in the manifest, or call the browserAction. setPopup method.

What is content scripts in Manifest JSON?

Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.


2 Answers

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.

like image 117
Wladimir Palant Avatar answered Sep 19 '22 12:09

Wladimir Palant


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;   
});
like image 20
Teoman shipahi Avatar answered Sep 20 '22 12:09

Teoman shipahi