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: Using Services.vc Created Dec. 19, 2014, 5:45 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') | messageResponder.js » ('J')
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,73 @@
* 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 = {};
+
+ function Page(source)
+ {
+ this._source = source;
+ }
+ Page.prototype =
+ {
+ sendMessage: function(message)
+ {
+ this._source.postMessage({
+ type: "message",
+ messageId: -1,
+ payload: message
+ }, "*");
+ }
+ };
+
+ global.ext.Page = Page;
+
+ /* Message passing */
+
+ global.ext.onMessage =
+ {
+ addListener: function(listener)
+ {
+ listener._extWrapper = function(event)
+ {
+ if (event.data.type != "message")
+ return;
+
+ var message = event.data.payload;
+ var messageId = event.data.messageId;
+ var sender = {
+ page: new Page(event.source)
+ };
+ var callback = function(message)
+ {
+ event.source.postMessage({
+ type: "response",
+ messageId: messageId,
+ payload: message
+ }, "*");
+ };
+ listener(message, sender, callback);
+ };
+ window.addEventListener("message", listener._extWrapper, false);
+ },
+
+ removeListener: function(listener)
+ {
+ if ("_extWrapper" in listener)
+ window.removeEventListener("message", listener._extWrapper, 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 +162,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') | messageResponder.js » ('J')

Powered by Google App Engine
This is Rietveld