5

I'm trying to convert my Chrome extension to a FireFox WebExtensions . I am having an issue to communicating between my webpage to the background script. From content script I am injecting some functions into browser page. That functions need to call browser extension and get response for that.

manifest.json

{
"manifest_version" : 2,
"name" : "Sample",
"description" : "Sample",
"version" : "1.0",

"icons" : {
    "48" : "icons/link-48.png"
},

"applications" : {
    "gecko" : {
        "id" : "sample@sss.com",
        "strict_min_version" : "48.0"
    }
},

"permissions" : ["notifications", "alarms", "storage"],

"background" : {
    "scripts" : [
        "js/jquery.js",
        "background-script.js"
    ]

},
"web_accessible_resources" : ["js/content.js"], // Not working in firefox
"externally_connectable" : {
    "matches" : [
        "http://localhost/*",]
},
"content_scripts" : [{
        "matches" : ["<all_urls>"],
        "js" : ["js/jquery.js",
            "js/script.js",
            "content-script.js"
        ],
        "run_at" : "document_start"
    }
],
"default_locale" : "en"
}

content-script.js

var port = chrome.runtime.connect();

window.addEventListener("message", function(event) {

if (event.source != window)
    return;

if (event.data.type && (event.data.type == "FROM_PAGE")) {
    console.log("Content script received: " + event.data.text);
    chrome.runtime.sendMessage({
        hello: 1
    },function (response)
    {
        //Need to send response back to browser
    });
}
}, false);

background-script.js

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
    alert('bg');
    var response_Text = '-1';

    if (request.hello) {
        console.log('hello received');
        sendResponse("213");
    }
});

browser page

window.postMessage({ type: "FROM_PAGE", text: "Hello from the webpage!" },"*");

I am using the window.postMessage to send some content.

Is it possible to get callback value for this function or any method available to send message to extension from webpage and get response?

  • Please [edit] the question to be on-topic: include a **complete** [mcve] that duplicates the problem. Usually, including a *manifest.json*, some of the background *and* content scripts. Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Oct 20 '16 at 14:36
  • More information added – Sharavanakumaar Murugesan Oct 21 '16 at 05:55
  • Thank you for adding information. However, we really need a *manifest.json* file.That file defines many aspects of the extension. It contains information which is often needed to determine what is going on with an extension. We are also going to need a bit more context for the code that is in the browser page. When and how is that line of code included in the page and run? Ideally, you will include everything needed to duplicate the problem. It is often a good idea for you to try copying the code directly from the question (and adding nothing else) to see if it duplicates the issue. – Makyen Oct 21 '16 at 06:29
  • The reason that a [mcve] is required is that we want to help. It is **much** easier to help if we don't have to recreate all the code needed to duplicate the problem. This is code that you already have. So, please help us to help you and provide a *complete* [mcve] that duplicates the problem with such questions. Without a [mcve] the amount of effort required to even begin to help you is **much** higher which *significantly* reduces the number of people willing/able to help you. Even if we put out the extra effort, we have to *guess* at significant portions of what your problem might be. – Makyen Oct 21 '16 at 06:29
  • You said you're trying to convert an extension; how did it work in Chrome? – Xan Oct 21 '16 at 09:10
  • I used 'web_accessible_resources' param to call my extension from js using chrome.runtime.sendMessage api – Sharavanakumaar Murugesan Oct 21 '16 at 09:19
  • I think my answer at the dupe should give you an idea how to implement this; if you think this does not apply, ping me and I'll remove the dupe. – Xan Oct 21 '16 at 09:30
  • The question is I need to retrieve back the data from content script to browser by using a callback. Having different answer for this. @Xan Can you remove duplicate so I can post my answer. – Sharavanakumaar Murugesan Oct 21 '16 at 11:08
  • Okay, I shall withdraw the dupe. For the record, I think it's relevant, so I'll leave a link: http://stackoverflow.com/questions/33991572/how-to-pass-data-from-content-script-to-page-level – Xan Oct 21 '16 at 11:10
  • Thanks @Xan, In that example we need to register a event handler. I am searching for method to avoid registering event handlers in browser side – Sharavanakumaar Murugesan Oct 21 '16 at 11:19

1 Answers1

0

Browser Page

Convert your callback to String and attach with detail parameter

var callbackstring=callback.toString(); *

and send like

var evt = document.createEvent("CustomEvent");
evt.initCustomEvent("eventname", true, true, {callback:callbackstring});
window.dispatchEvent(evt);

Content Script

Retrieve the callbackstring from event object like

var callbackstring=event.detail.callback;

Use eval function and convert the string to function

eval("var callback="+callbackstring);

Now you can trigger your callback easily in the listener

callback(response);
  • Browser developer call content script ( intermediate ). so that one handle require security and saftely. So content script message is safe call for extension script – Ayyappan murugesan Oct 21 '16 at 11:25
  • 2
    @SharavanakumaarMurugesan, and Ayyappan Murugesan: This allows the web page to run arbitrary code in the content script context. Doing so is a *large* security issue. If you include it in a a Firefox WebExtension it should never pass review by AMO. – Makyen Dec 18 '16 at 01:42