开发者

Calling chrome.i18n.getMessage(...) from a content script

My Google Chome e开发者_如何转开发xtension makes use of a content script declared in its manifest via:

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["js/jquery-1.6.1.min.js", "js/content.js"]
    }
  ],

It also comes with a localization-related resource bundle in _locales/en.

Looking up a key using chrome.i18n.getMessage(...) works flawlessly from general extension code (i.e., in options.html), but it fails (i.e., does not return anything) when executed from js/content.js while that script is being run in the context of the regular web page.

Is this a general limitation (this Chrome bug may be related) or did someone manage to get this working?


You should be able to use chrome.i18n.getMessage(...) from content scripts. Try restarting the whole browser.

Currently running: Version 24.0.1297.0 dev-m

I was hit by this as well. I'm not sure about the original poster's scenario, but my problems were caused by the bug (http://code.google.com/p/chromium/issues/detail?id=53628), which was referenced in a reply in Thilo's bug report. The bug is that the reloading of messages.json is unreliable when reloading your extension from the Extensions manager page. I originally started with an empty messages.json and then added to it as I tried to make use of chrome.i18n.getMessage. I too received no errors, but didn't get my message as expected. As illogical as it sounds and being new to development in general, I thought this might be one of those APIs that can't run from content scripts. Luckily that wasn't the case.


I would say a bug report needs to be created.

It doesn't say anywhere that it is supposed to work, but it doesn't throw an error that chrome.i18n.getMessage isn't accessible from a content scripts either, as all other API calls do. So that's a bug already.

It would be a nice feature to have. I think they started implementing it but either forget or left it for better times. A bug report would be a good reminder.

Meanwhile, you can get localization strings from a background page through messaging. Perhaps send one request at the beginning of a content script requesting an array of all strings you will need.


The strings only get loaded once, when activating the extension.

When the function returns "" this just means, that the index wasn't found.

One just needs to go to "Chrome Extensions" disable and re-enable the extension.

Then the function returns the localized strings - just as expected @ Chromium 31.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜