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

Side by Side Diff: safari/common.js

Issue 16067002: Added Safari Support (Closed)
Patch Set: Created Oct. 21, 2013, 8:11 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 (function() {
2 /* Events */
3
4 WrappedEventTarget = function(target, eventName, capture) {
5 this._listeners = [];
6 this._wrappedListeners = [];
7
8 this._target = target;
9 this._eventName = eventName;
10 this._capture = capture;
11 };
12 WrappedEventTarget.prototype = {
13 addListener: function(listener) {
14 var wrappedListener = this._wrapListener(listener);
15
16 this._listeners.push(listener);
17 this._wrappedListeners.push(wrappedListener);
18
19 this._target.addEventListener(
20 this._eventName,
21 wrappedListener,
22 this._capture
23 );
24 },
25 removeListener: function(listener) {
26 var idx = this._listeners.indexOf(listener);
27
28 if (idx != -1) {
29 this._target.removeEventListener(
30 this._eventName,
31 this._wrappedListeners[idx],
32 this._capture
33 );
34
35 this._listeners.splice(idx, 1);
36 this._wrappedListeners.splice(idx, 1);
37 }
38 }
39 };
40
41
42 MessageEventTarget = function(target) {
43 WrappedEventTarget.call(this, target, "message", false);
44 };
45 MessageEventTarget.prototype = {
46 __proto__: WrappedEventTarget.prototype,
47 _wrapListener: function(listener) {
48 return function(event) {
49 if (event.name.indexOf("request-") != 0)
50 return;
51
52 var sender = {};
53 var dispatcher;
54
55 if (event.target instanceof SafariBrowserTab) {
56 dispatcher = event.target.page;
57 sender.tab = new Tab(event.target);
58 } else {
59 dispatcher = event.target.tab;
60 sender.tab = null;
61 }
62
63 listener(event.message, sender, function(message) {
64 dispatcher.dispatchMessage("response-" + event.name.substr(8), message );
65 });
66 };
67 }
68 };
69
70
71 /* Message passing */
72
73 var requestCounter = 0;
74
75 sendMessage = function(message, responseCallback) {
76 var requestId = ++requestCounter;
77
78 if (responseCallback) {
79 var eventTarget = this._eventTarget;
80 var responseListener = function(event) {
81 if (event.name == "response-" + requestId) {
82 eventTarget.removeEventListener("message", responseListener, false);
83 responseCallback(event.message);
84 }
85 };
86 eventTarget.addEventListener("message", responseListener, false);
87 }
88
89 this._messageDispatcher.dispatchMessage("request-" + requestId, message);
90 };
91
92
93 /* I18n */
94
95 var I18n = function() {
96 this._localeCandidates = this._getLocaleCandidates();
97 this._uiLocale = this._localeCandidates[0];
98 };
99 I18n.prototype = {
100 _getLocaleCandidates: function() {
101 var bits, i, locale;
102 var candidates = [];
103 var default_locale = "en_US";
104
105 for (i = (bits = navigator.language.split("-")).length; i > 0; i--) {
106 locale = bits.slice(0, i).join("_");
107 candidates.push(locale);
108
109 if (locale == default_locale)
110 return candidates;
111 }
112
113 candidates.push(default_locale);
114 return candidates;
115 },
116 _getCatalog: function(locale) {
117 var xhr = new XMLHttpRequest();
118
119 xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messag es.json", false);
120
121 try {
122 xhr.send();
123 } catch (e) {
124 return null;
125 }
126
127 return JSON.parse(xhr.responseText);
128 },
129 getMessage: function(msgId, substitutions) {
130 if (msgId == "@@ui_locale")
131 return this._uiLocale;
132
133 for (var i = 0; i < this._localeCandidates.length; i++) {
134 var catalog = this._getCatalog(this._localeCandidates[i]);
135 if (!catalog) {
136 // if there is no catalog for this locale
137 // candidate, dont"t try to load it again
138 this._localeCandidates.splice(i--, 1);
139 continue;
140 }
141
142 var msg = catalog[msgId];
143 if (!msg)
144 continue;
145
146 var msgstr = msg.message;
147 if (!msgstr)
148 continue;
149
150 for (var placeholder in msg.placeholders) {
151 var placeholderDetails = msg.placeholders[placeholder];
152 if (!placeholderDetails || !placeholderDetails.content)
153 continue;
154 if (placeholderDetails.content.indexOf("$") != 0)
155 continue;
156
157 var placeholderIdx = parseInt(placeholderDetails.content.substr(1));
158 if (isNaN(placeholderIdx) || placeholderIdx < 1)
159 continue;
160
161 var placeholderValue;
162 if (Object.prototype.toString.call(substitutions) == "[object Array]")
163 placeholderValue = substitutions[placeholderIdx - 1];
164 else if (placeholderIdx == 1)
165 placeholderValue = substitutions;
166
167 msgstr = msgstr.replace("$" + placeholder + "$", placeholderValue || " ");
168 }
169
170 return msgstr;
171 }
172
173 return "";
174 }
175 };
176
177
178 /* API */
179
180 ext = {
181 getURL: function(path) {
182 return safari.extension.baseURI + path;
183 },
184 i18n: new I18n()
185 };
186 })();
OLDNEW
« chrome/background.js ('K') | « safari/background.js ('k') | safari/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld