Accessing chrome.i18n from a sandboxed page

241 views
Skip to first unread message

Jake Holman

unread,
Aug 24, 2012, 9:07:31 PM8/24/12
to chromi...@chromium.org
I've started refactoring a bunch of my extension code to bring it inline with the new security changes being slowly phased in.

Because of the new, stricter regulations my use of Handlebars has been rendered incompatible. I found a good article which explains the process of sandboxing, and thus opening up the ability to use Handlebars: https://developer.chrome.com/trunk/extensions/sandboxingEval.html

Unfortunately, the most critical part of my use of Handlebars doesn't seem to be possible. 

Here's a Handlebars helper I created so I can easily use i18n in my templates:

Handlebars.registerHelper('t', function(key) {
  return new Handlebars.SafeString(
    chrome.i18n.getMessage(key)
  )
});

This means I can use {{t 'some_i18n_key'}} in my templates, which Handlebars will expand to the relevant localized string.

However, this responds with a "Cannot call method 'getMessage' of undefined".

This is because the sandbox has no access to chrome.i18n (in fact it has no access to the extension at all)

Can anyone think of a good way around this?


Mihai Parparita

unread,
Aug 27, 2012, 3:47:27 PM8/27/12
to Jake Holman, chromi...@chromium.org
You could make the chrome.i18n.getMessage call in your regular extension page, and include the results in the context data that you postMessage to your sandboxed page, and then have the template use them directly.

If you wanted to automate this (so that you wouldn't have to add a new variable every time you added a message), here's a sketch of what could work:

var xhr = new XMLHttpRequest();
xhr.onload = function() {
  var messages = JSON.parse(xhr.responseText);
  var translatedMessages = {}
  for (var messageId in messages) {
    translatedMessages[messageId] = chrome.i18n.getMessage(mesageId);
  }
  
  // do the postMessage to the sandboxed page with your other template parameters, and include translatedMessages as one of the context variables
}
xhr.open("GET", "_locales/en/messages.json", true);
xhr.send();

Then your 't' helper function would instead look up the message in the translatedMessages map that you sent over.

Mihai



--
You received this message because you are subscribed to the Google Groups "Chromium Apps" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msg/chromium-apps/-/wYaelFRNe38J.
To post to this group, send email to chromi...@chromium.org.
To unsubscribe from this group, send email to chromium-app...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-apps/?hl=en.

Paul Kinlan

unread,
Aug 28, 2012, 2:07:12 AM8/28/12
to Mihai Parparita, Jake Holman, chromi...@chromium.org
The other side of this is that if you pre-compile your handlebars templates and functions/  This means you can use your templates out side of the sandboxed area of apps.

P
--
Paul Kinlan
Developer Advocate @ Google for Chrome and HTML5
Skype: paul.kinlan

Reply all
Reply to author
Forward
0 new messages