Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: ext/common.js

Issue 4731979438227456: Issue 1663 - Emulate background page and implement proper message responder (Closed)
Patch Set: Created Dec. 17, 2014, 6:32 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ext/background.js ('k') | ext/content.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ext/common.js
===================================================================
--- a/ext/common.js
+++ b/ext/common.js
@@ -12,16 +12,62 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
(function(global)
{
+ if (!global.ext)
+ global.ext = {};
+
+ /* Message passing */
+
+ global.ext.onMessage =
+ {
+ addListener: function(listener)
+ {
+ listener.__wrapper = function(event)
+ {
+ if (event.data.type != "message")
+ return;
+
+ var message = event.data.payload;
+ var messageId = event.data.messageId;
+ var sender = {
+ sendMessage: function(message)
+ {
+ event.source.postMessage({
+ type: "message",
+ messageId: -1,
+ payload: message
+ }, "*");
+ }
+ };
+ var callback = function(message)
+ {
+ event.source.postMessage({
+ type: "response",
+ messageId: messageId,
+ payload: message
+ }, "*");
+ };
+ listener(message, sender, callback);
+ };
+ window.addEventListener("message", listener.__wrapper, false);
+ },
+
+ removeListener: function(listener)
+ {
+ if ("__wrapper" in listener)
+ window.removeEventListener("message", listener.__wrapper, false);
+ }
+ };
+
/* I18n */
var getLocaleCandidates = function(selectedLocale)
{
var candidates = [];
var defaultLocale = "en-US";
// e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second
@@ -105,19 +151,16 @@
var rawCatalog = JSON.parse(xhr.responseText);
for (var msgId in rawCatalog)
{
if (!(msgId in catalog))
catalog[msgId] = parseMessage(rawCatalog[msgId]);
}
};
- if (!global.ext)
- global.ext = {};
-
global.ext.i18n = {
getMessage: function(msgId, substitutions)
{
while (true)
{
var message = catalog[msgId];
if (message)
{
« no previous file with comments | « ext/background.js ('k') | ext/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld