1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/* global addMessageListener, removeMessageListener, sendAsyncMessage */
// for non background pages
(function() {
'use strict';
window.vAPI = window.vAPI || {};
// since this is common across vendors
var messagingConnector = function(response) {
var channel, listener;
if (!response) {
return;
}
if (response.broadcast === true) {
for (channel in vAPI.messaging.channels) {
listener = vAPI.messaging.channels[channel].listener;
if (typeof listener === 'function') {
listener(response.msg);
}
}
return;
}
if (response.requestId) {
listener = vAPI.messaging.listeners[response.requestId];
}
if (!listener) {
channel = vAPI.messaging.channels[response.portName];
listener = channel && channel.listener;
}
if (typeof listener === 'function') {
// Safari bug
// Deleting the response.requestId below (only in some cases, probably
// when frames are present on the page) will remove it from all the
// future messages too, however with the following line it won't.
vAPI.safari && console.log;
delete vAPI.messaging.listeners[response.requestId];
delete response.requestId;
listener(response.msg);
}
};
if (window.chrome) {
vAPI.chrome = true;
vAPI.messaging = {
port: null,
requestId: 0,
listenerId: null,
listeners: {},
channels: {},
connector: messagingConnector,
setup: function() {
this.listenerId = 'uBlock:' + name + ':' + parseInt(Math.random() * 1e10, 10).toString(36);
this.port = chrome.runtime.connect({name: this.listenerId});
this.port.onMessage.addListener(this.connector);
},
close: function() {
if (this.port) {
this.port.disconnect();
this.port.onMessage.removeListener(this.connector);
this.channels = this.listeners = this.port = this.listenerId = null;
}
},
channel: function(name, callback) {
if (!name) {
return;
}
if (!this.listenerId) {
this.setup();
}
this.channels[name] = {
portName: name,
listener: typeof callback === 'function' ? callback : null,
send: function(message, callback) {
message = {
portName: this.portName,
msg: message
};
if (callback) {
message.requestId = ++vAPI.messaging.requestId;
vAPI.messaging.listeners[message.requestId] = callback;
}
vAPI.messaging.port.postMessage(message);
},
close: function() {
delete vAPI.messaging.channels[this.portName];
}
};
return this.channels[name];
}
};
} else if (window.safari) {
vAPI.safari = true;
// relevant?
// https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/MessagesandProxies/MessagesandProxies.html#//apple_ref/doc/uid/TP40009977-CH14-SW12
vAPI.messaging = {
requestId: 0,
listeners: {},
channels: {},
connector: messagingConnector,
setup: function() {
this._connector = function(msg) {
vAPI.messaging.connector(msg.message);
};
safari.self.addEventListener('message', this._connector, false);
this.channels['vAPI'] = {
listener: function(msg) {
if (msg.cmd === 'runScript' && msg.details.code) {
Function(msg.details.code).call(window);
}
}
};
},
close: function() {
if (this._connector) {
safari.self.removeEventListener('message', this._connector, false);
this.channels = this.listeners = null;
}
},
channel: function(name, callback) {
if (!name) {
return;
}
if (!this._connector) {
this.setup();
}
this.channels[name] = {
portName: name,
listener: typeof callback === 'function' ? callback : null,
send: function(message, callback) {
message = {
portName: this.portName,
msg: message
};
if (callback) {
message.requestId = ++vAPI.messaging.requestId;
vAPI.messaging.listeners[message.requestId] = callback;
}
if (safari.extension.globalPage) {
// popover content doesn't know messaging...
safari.extension.globalPage.contentWindow
.vAPI.messaging.connector({
name: 'message',
message: message,
target: {
page: {
dispatchMessage: function(name, msg) {
vAPI.messaging.connector(msg);
}
}
}
});
}
else {
safari.self.tab.dispatchMessage('message', message);
}
},
close: function() {
delete vAPI.messaging.channels[this.portName];
}
};
return this.channels[name];
}
};
if (location.protocol === "safari-extension:") {
return;
}
var linkHelper = document.createElement('a');
var onBeforeLoad = function(e, details) {
if (e.url && e.url.slice(0, 5) === 'data:') {
return;
}
linkHelper.href = details ? details.url : e.url;
if (!/^https?:/.test(linkHelper.protocol)) {
return;
}
if (details) {
details.url = linkHelper.href;
}
else {
details = {
url: linkHelper.href
};
switch (e.target.nodeName.toLowerCase()) {
case 'frame':
case 'iframe':
details.type = 'sub_frame';
break;
case 'script':
details.type = 'script';
break;
case 'img':
case 'input': // type=image
details.type = 'image';
break;
case 'object':
case 'embed':
details.type = 'object';
break;
case 'link':
var rel = e.target.rel.trim().toLowerCase();
if (rel.indexOf('icon') > -1) {
details.type = 'image';
break;
}
else if (rel === 'stylesheet') {
details.type = 'stylesheet';
break;
}
default:
details.type = 'other';
}
// This can run even before the first DOMSubtreeModified event fired
if (firstMutation) {
firstMutation();
}
}
// tabId is determined in the background script
// details.tabId = null;
details.frameId = 0;
details.parentFrameId = window === window.top ? -1 : 0;
details.timeStamp = Date.now();
var response = safari.self.tab.canLoad(e, details);
if (!response) {
e.preventDefault();
return false;
}
// local mirroring, response is a data: URL here
else if (typeof response === 'string' && details.type === 'script') {
// Content Security Policy with disallowed inline scripts may break things
e.preventDefault();
details = document.createElement('script');
details.textContent = atob(response.slice(response.indexOf(',', 20) + 1));
e.target.parentNode.insertBefore(details, e.target);
details.parentNode.removeChild(details);
}
};
document.addEventListener('beforeload', onBeforeLoad, true);
// blocking pop-ups and intercepting xhr requests
var firstMutation = function() {
document.removeEventListener('DOMSubtreeModified', firstMutation, true);
firstMutation = null;
var randomEventName = parseInt(Math.random() * 1e15, 10).toString(36);
var beforeLoadEvent = document.createEvent('Event');
beforeLoadEvent.initEvent('beforeload');
window.addEventListener(randomEventName, function(e) {
var result = onBeforeLoad(beforeLoadEvent, e.detail);
if (result === false) {
e.detail.url = false;
}
}, true);
// the extension context is unable to reach the page context,
// also this only works when Content Security Policy allows inline scripts
var tmpJS = document.createElement('script');
tmpJS.textContent = ["(function() {",
"var block = function(u, t) {",
"var e = document.createEvent('CustomEvent'),",
"d = {url: u, type: t};",
"e.initCustomEvent(",
"'" + randomEventName + "', !1, !1, d",
");",
"dispatchEvent(e);",
"return d.url === !1;",
"}, wo = open, xo = XMLHttpRequest.prototype.open;",
"open = function(u) {",
"return block(u, 'popup') ? null : wo.apply(this, [].slice.call(arguments));",
"};",
"XMLHttpRequest.prototype.open = function(m, u) {",
"return block(u, 'xmlhttprequest') ? null : xo.apply(this, [].slice.call(arguments));",
"};",
"})();"].join('');
document.head.removeChild(document.head.appendChild(tmpJS));
};
document.addEventListener('DOMSubtreeModified', firstMutation, true);
var onContextMenu = function(e) {
var details = {
tagName: e.target.tagName.toLowerCase(),
pageUrl: window.location.href,
insideFrame: window.top !== window
};
details.editable = details.tagName === 'textarea' || details.tagName === 'input';
if ('checked' in e.target) {
details.checked = e.target.checked;
}
if (details.tagName === 'a') {
details.linkUrl = e.target.href;
}
if ('src' in e.target) {
details.srcUrl = e.target.src;
if (details.tagName === 'img') {
details.mediaType = 'image';
}
else if (details.tagName === 'video' || details.tagName === 'audio') {
details.mediaType = details.tagName;
}
}
safari.self.tab.setContextMenuEventUserInfo(e, details);
};
window.addEventListener('contextmenu', onContextMenu, true);
}
})();
|