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: Properly convert Subscription objects as notification arguments Created Dec. 17, 2014, 10:15 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
Index: ext/common.js
===================================================================
--- a/ext/common.js
+++ b/ext/common.js
@@ -12,16 +12,76 @@
* 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 =
+ {
+ equals: function(page)
+ {
+ return page._source == this._source;
+ },
+
+ sendMessage: function(message)
+ {
+ this._source.postMessage({
+ type: "message",
+ messageId: -1,
+ payload: message
+ }, "*");
+ }
+ }
+
+ /* Message passing */
+
+ global.ext.onMessage =
+ {
+ addListener: function(listener)
+ {
+ listener.__wrapper = function(event)
Thomas Greiner 2014/12/18 17:32:06 Two underscores are easy to get wrong and detect w
Wladimir Palant 2014/12/18 22:04:44 Done.
+ {
+ 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.__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 +165,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)
{

Powered by Google App Engine
This is Rietveld