Skip to content

Message Sending Guide

jainmnit edited this page Jul 13, 2015 · 2 revisions

Sending Message From Content Script to Extension:

In ContentScript.js , Wherever you want send some message to Extension , Just add following piece of code:

chrome.runtime.sendMessage({ message : { "gesture": 'openAddContributionPage', "options": {} } });

This will send Message to Extension and we can pass whatever argument value we want into the options . gesture will recognize the method name that will be called in MainCtrl.js

So above sending message will call openAddContributionPage method with empty options.

Sending message from Extension to Content Script

Then this will be done through PostMessageService.js file.

Here we can create as many as function we need in following code base

this.gesture

Sample function in this is as below:

showIframe: function(option) { self.sendGesture("showIframe", option); }

So this call the following method in this file itself

this.port.postMessage({ "gesture": gestureName, "options": opt });

And this postMessage will then call the ContentScript message listner

chrome.runtime.onMessage.addListener(function(msg) { if (msg.gesture && msg.gesture in GESTURES) { GESTURES[msg.gesture](msg.options) } });

Clone this wiki locally