Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: safari/ext/common.js

Issue 5088751004942336: Issue 370 - Right-clicked element is removed independent of created filter (Closed)
Patch Set: Rebase to rev 3c9cea80c481 Created July 18, 2014, 8:54 a.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 | « safari/ext/background.js ('k') | safari/ext/content.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: safari/ext/common.js
===================================================================
rename from safari/common.js
rename to safari/ext/common.js
--- a/safari/common.js
+++ b/safari/ext/common.js
@@ -1,6 +1,6 @@
/*
* This file is part of Adblock Plus <http://adblockplus.org/>,
- * Copyright (C) 2006-2013 Eyeo GmbH
+ * Copyright (C) 2006-2014 Eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
@@ -15,168 +15,131 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
-(function() {
- /* Events */
+(function()
+{
+ /* Message passing */
- WrappedEventTarget = function(target, eventName, capture)
+ var MessageProxy = ext._MessageProxy = function(messageDispatcher)
{
- this._listeners = [];
- this._wrappedListeners = [];
+ this._messageDispatcher = messageDispatcher;
+ this._responseCallbacks = {__proto__: null};
+ this._responseCallbackCounter = 0;
+ };
+ MessageProxy.prototype = {
+ _sendResponse: function(request, message)
+ {
+ var response = {};
+ for (var prop in request)
+ response[prop] = request[prop];
+ response.payload = message;
- this._target = target;
- this._eventName = eventName;
- this._capture = capture;
- };
- WrappedEventTarget.prototype = {
- addListener: function(listener)
+ this._messageDispatcher.dispatchMessage("response", response);
+ },
+ handleRequest: function(request, sender)
{
- var wrappedListener = this._wrapListener(listener);
+ var sendResponse;
+ if ("callbackId" in request)
+ sendResponse = this._sendResponse.bind(this, request);
+ else
+ sendResponse = function() {};
- this._listeners.push(listener);
- this._wrappedListeners.push(wrappedListener);
+ ext.onMessage._dispatch(request.payload, sender, sendResponse);
+ },
+ handleResponse: function(response)
+ {
+ var callbackId = response.callbackId;
+ var callback = this._responseCallbacks[callbackId];
+ if (callback)
+ {
+ delete this._responseCallbacks[callbackId];
+ callback(response.payload);
+ }
+ },
+ sendMessage: function(message, responseCallback, extra)
+ {
+ var request = {payload: message};
- this._target.addEventListener(
- this._eventName,
- wrappedListener,
- this._capture
- );
- },
- removeListener: function(listener)
- {
- var idx = this._listeners.indexOf(listener);
+ if (responseCallback)
+ {
+ request.callbackId = ++this._responseCallbackCounter;
+ this._responseCallbacks[request.callbackId] = responseCallback;
+ }
- if (idx != -1)
- {
- this._target.removeEventListener(
- this._eventName,
- this._wrappedListeners[idx],
- this._capture
- );
+ for (var prop in extra)
+ request[prop] = extra[prop];
- this._listeners.splice(idx, 1);
- this._wrappedListeners.splice(idx, 1);
- }
+ this._messageDispatcher.dispatchMessage("request", request);
}
};
-
- MessageEventTarget = function(target)
- {
- WrappedEventTarget.call(this, target, "message", false);
- };
- MessageEventTarget.prototype = {
- __proto__: WrappedEventTarget.prototype,
- _wrapListener: function(listener)
- {
- return function(event)
- {
- if (event.name.indexOf("request-") != 0)
- return;
-
- var sender = {};
- var dispatcher;
-
- if ("SafariBrowserTab" in window && event.target instanceof SafariBrowserTab)
- {
- dispatcher = event.target.page;
- sender.tab = new Tab(event.target);
- }
- else
- {
- dispatcher = event.target.tab;
- sender.tab = null;
- }
-
- listener(event.message, sender, function(message)
- {
- dispatcher.dispatchMessage("response-" + event.name.substr(8), message);
- });
- };
- }
- };
-
-
- /* Message passing */
-
- var requestCounter = 0;
-
- sendMessage = function(message, responseCallback)
- {
- var requestId = ++requestCounter;
-
- if (responseCallback)
- {
- var eventTarget = this._eventTarget;
- var responseListener = function(event)
- {
- if (event.name == "response-" + requestId)
- {
- eventTarget.removeEventListener("message", responseListener, false);
- responseCallback(event.message);
- }
- };
- eventTarget.addEventListener("message", responseListener, false);
- }
-
- this._messageDispatcher.dispatchMessage("request-" + requestId, message);
- };
+ ext.onMessage = new ext._EventTarget();
/* I18n */
- var I18n = function()
+ var localeCandidates = null;
+ var uiLocale;
+
+ var getLocaleCandidates = function()
{
- this._localeCandidates = this._getLocaleCandidates();
- this._uiLocale = this._localeCandidates[0];
+ var candidates = [];
+ var defaultLocale = "en_US";
+
+ // e.g. "ja-jp-mac" -> "ja", "jp", note that the part after the second
+ // dash is dropped, since we only support language and region
+ var [language, region] = navigator.language.split("-");
+
+ if (region)
+ candidates.push(language + "_" + region.toUpperCase());
+
+ candidates.push(language);
+
+ if (candidates.indexOf(defaultLocale) == -1)
+ candidates.push(defaultLocale);
+
+ return candidates;
};
- I18n.prototype = {
- _getLocaleCandidates: function()
+
+ var getCatalog = function(locale)
+ {
+ var xhr = new XMLHttpRequest();
+
+ xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messages.json", false);
+
+ try {
+ xhr.send();
+ }
+ catch (e)
{
- var candidates = [];
- var defaultLocale = "en_US";
+ return null;
+ }
- var bits, i;
- for (i = (bits = navigator.language.split("-")).length; i > 0; i--)
+ if (xhr.status != 200 && xhr.status != 0)
+ return null;
+
+ return JSON.parse(xhr.responseText);
+ };
+
+ ext.i18n = {
+ getMessage: function(msgId, substitutions)
+ {
+ if (!localeCandidates)
{
- var locale = bits.slice(0, i).join("_");
- candidates.push(locale);
-
- if (locale == defaultLocale)
- return candidates;
+ localeCandidates = getLocaleCandidates();
+ uiLocale = localeCandidates[0];
}
- candidates.push(defaultLocale);
- return candidates;
- },
- _getCatalog: function(locale)
- {
- var xhr = new XMLHttpRequest();
-
- xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messages.json", false);
+ if (msgId == "@@ui_locale")
+ return uiLocale;
- try {
- xhr.send();
- }
- catch (e)
+ for (var i = 0; i < localeCandidates.length; i++)
{
- return null;
- }
-
- return JSON.parse(xhr.responseText);
- },
- getMessage: function(msgId, substitutions)
- {
- if (msgId == "@@ui_locale")
- return this._uiLocale;
-
- for (var i = 0; i < this._localeCandidates.length; i++)
- {
- var catalog = this._getCatalog(this._localeCandidates[i]);
+ var catalog = getCatalog(localeCandidates[i]);
if (!catalog)
{
// if there is no catalog for this locale
// candidate, don't try to load it again
- this._localeCandidates.splice(i--, 1);
+ localeCandidates.splice(i--, 1);
continue;
}
@@ -201,7 +164,7 @@
continue;
var placeholderValue;
- if (Object.prototype.toString.call(substitutions) == "[object Array]")
+ if (typeof substitutions != "string")
placeholderValue = substitutions[placeholderIdx - 1];
else if (placeholderIdx == 1)
placeholderValue = substitutions;
@@ -217,13 +180,10 @@
};
- /* API */
+ /* Utils */
- ext = {
- getURL: function(path)
- {
- return safari.extension.baseURI + path;
- },
- i18n: new I18n()
+ ext.getURL = function(path)
+ {
+ return safari.extension.baseURI + path;
};
})();
« no previous file with comments | « safari/ext/background.js ('k') | safari/ext/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld