Accessing chrome.i18n from a sandboxed page

Showing 1-3 of 3 messages
Accessing chrome.i18n from a sandboxed page Jake Holman 8/24/12 6:07 PM
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?


Re: [cws-apps] Accessing chrome.i18n from a sandboxed page Mihai Parparita 8/27/12 12:47 PM
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.

Re: [cws-apps] Accessing chrome.i18n from a sandboxed page Paul Kinlan 8/27/12 11:07 PM
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
t: +447730517944
Skype: paul.kinlan