Skip to content

Message Sending Guide

Ore Landau edited this page Jul 13, 2015 · 2 revisions

Sending Messages from Content Script to Extension:

In ContentScript.js, wherever you want to send a 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 Messages 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 listener

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