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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
|
// » header
/* global SafariBrowserTab, Services, XPCOMUtils */
// for background page only
(function() {
'use strict';
self.vAPI = self.vAPI || {};
// «
if (self.chrome) {
// » crx
var chrome = self.chrome;
vAPI.chrome = true;
vAPI.storage = chrome.storage.local;
vAPI.tabs = {
registerListeners: function() {
if (typeof this.onNavigation === 'function') {
chrome.webNavigation.onCommitted.addListener(this.onNavigation);
}
if (typeof this.onUpdated === 'function') {
chrome.tabs.onUpdated.addListener(this.onUpdated);
}
if (typeof this.onClosed === 'function') {
chrome.tabs.onRemoved.addListener(this.onClosed);
}
if (typeof this.onPopup === 'function') {
chrome.webNavigation.onCreatedNavigationTarget.addListener(this.onPopup);
}
},
get: function(tabId, callback) {
if (tabId === null) {
chrome.tabs.query(
{
active: true,
currentWindow: true
},
function(tabs) {
callback(tabs[0]);
}
);
}
else {
chrome.tabs.get(tabId, callback);
}
},
/*open: function(details) {
// to keep incognito context?
chrome.windows.getCurrent(function(win) {
details.windowId = win.windowId;
chrome.tabs.create(details);
});
},*/
open: function(details) {
if (!details.url) {
return null;
}
// extension pages
else if (!details.url.match(/^\w{2,20}:/)) {
details.url = vAPI.getURL(details.url);
}
// dealing with Chrome's asynhronous API
var wrapper = function() {
if (details.active === undefined) {
details.active = true;
}
var subWrapper = function() {
var _details = {
url: details.url,
active: !!details.active
};
if (details.tabId) {
// update doesn't accept index, must use move
chrome.tabs.update(details.tabId, _details, function(tab) {
// if the tab doesn't exist
if (chrome.runtime.lastError) {
chrome.tabs.create(_details);
}
else if (details.index !== undefined) {
chrome.tabs.move(tab.id, {index: details.index});
}
});
}
else {
if (details.index !== undefined) {
_details.index = details.index;
}
chrome.tabs.create(_details);
}
};
if (details.index === -1) {
vAPI.tabs.get(null, function(tab) {
if (tab) {
details.index = tab.index + 1;
}
else {
delete details.index;
}
subWrapper();
});
}
else {
subWrapper();
}
};
if (details.select) {
// note that currentWindow may be even the window of Developer Tools
// so, test with setTimeout...
chrome.tabs.query({currentWindow: true}, function(tabs) {
var url = details.url.replace(rgxHash, '');
// this is questionable
var rgxHash = /#.*/;
tabs = tabs.some(function(tab) {
if (tab.url.replace(rgxHash, '') === url) {
chrome.tabs.update(tab.id, {active: true});
return true;
}
});
if (!tabs) {
wrapper();
}
});
}
else {
wrapper();
}
},
close: chrome.tabs.remove.bind(chrome.tabs),
injectScript: function(tabId, details, callback) {
if (!callback) {
callback = function(){};
}
if (tabId) {
chrome.tabs.executeScript(tabId, details, callback);
}
else {
chrome.tabs.executeScript(details, callback);
}
}
};
// Must read: https://code.google.com/p/chromium/issues/detail?id=410868#c8
// https://github.com/gorhill/uBlock/issues/19
// https://github.com/gorhill/uBlock/issues/207
// Since we may be called asynchronously, the tab id may not exist
// anymore, so this ensures it does still exist.
vAPI.setIcon = function(tabId, img, badge) {
var onIconReady = function() {
if ( chrome.runtime.lastError ) {
return;
}
chrome.browserAction.setBadgeText({ tabId: tabId, text: badge });
if ( badge !== '' ) {
chrome.browserAction.setBadgeBackgroundColor({ tabId: tabId, color: '#666' });
}
};
chrome.browserAction.setIcon({ tabId: tabId, path: img }, onIconReady);
};
vAPI.messaging = {
ports: {},
listeners: {},
listen: function(listenerName, callback) {
this.listeners[listenerName] = callback;
},
setup: function(connector) {
if (this.connector) {
return;
}
this.connector = function(port) {
var onMessage = function(request) {
var callback = function(response) {
if (chrome.runtime.lastError || response === undefined) {
return;
}
if (request.requestId) {
port.postMessage({
requestId: request.requestId,
portName: request.portName,
msg: response
});
}
};
var listener = connector(request.msg, port.sender, callback);
if (listener === null) {
listener = vAPI.messaging.listeners[request.portName];
if (typeof listener === 'function') {
listener(request.msg, port.sender, callback);
} else {
console.error('µBlock> messaging > unknown request: %o', request);
}
}
};
var onDisconnect = function(port) {
port.onDisconnect.removeListener(onDisconnect);
port.onMessage.removeListener(onMessage);
delete vAPI.messaging.ports[port.name];
};
port.onDisconnect.addListener(onDisconnect);
port.onMessage.addListener(onMessage);
vAPI.messaging.ports[port.name] = port;
};
chrome.runtime.onConnect.addListener(this.connector);
},
broadcast: function(message) {
message = {
broadcast: true,
msg: message
};
for (var portName in this.ports) {
this.ports[portName].postMessage(message);
}
}
};
vAPI.net = {
registerListeners: function() {
var listeners = [
'onBeforeRequest',
'onBeforeSendHeaders',
'onHeadersReceived'
];
for (var i = 0; i < listeners.length; ++i) {
chrome.webRequest[listeners[i]].addListener(
this[listeners[i]].callback,
{
'urls': this[listeners[i]].urls || ['<all_urls>'],
'types': this[listeners[i]].types || []
},
this[listeners[i]].extra
);
}
}
};
vAPI.contextMenu = {
create: function(details, callback) {
this.menuId = details.id;
this.callback = callback;
chrome.contextMenus.create(details);
chrome.contextMenus.onClicked.addListener(this.callback);
},
remove: function() {
chrome.contextMenus.onClicked.removeListener(this.callback);
chrome.contextMenus.remove(this.menuId);
}
};
// «
} else if (self.safari) {
// » safariextz
vAPI.safari = true;
// addContentScriptFromURL allows whitelisting,
// so load sitepaching this way, instead of adding it to the Info.plist
safari.extension.addContentScriptFromURL(
safari.extension.baseURI + 'js/sitepatch-safari.js',
[
'http://www.youtube.com/*',
'https://www.youtube.com/*',
'http://www.youtube-nocookie.com/*',
'https://www.youtube-nocookie.com/*'
]
);
safari.extension.settings.addEventListener('change', function(e) {
if (e.key === 'open_prefs') {
vAPI.tabs.open({url: 'dashboard.html', active: true});
}
}, false);
vAPI.storage = {
_storage: safari.extension.settings,
QUOTA_BYTES: 52428800, // copied from Info.plist
get: function(keys, callback) {
if (typeof callback !== 'function') {
return;
}
var i, value, result = {};
if (keys === null) {
for (i in this._storage) {
value = this._storage[i];
if (typeof value === 'string') {
result[i] = JSON.parse(value);
}
}
}
else if (typeof keys === 'string') {
value = this._storage[keys];
if (typeof value === 'string') {
result[keys] = JSON.parse(value);
}
}
else if (Array.isArray(keys)) {
for ( i = 0; i < keys.length; ++i) {
value = this._storage[i];
if (typeof value === 'string') {
result[keys[i]] = JSON.parse(value);
}
}
}
else if (typeof keys === 'object') {
for (i in keys) {
value = this._storage[i];
if (typeof value === 'string') {
result[i] = JSON.parse(value);
}
else {
result[i] = keys[i];
}
}
}
callback(result);
},
set: function(details, callback) {
for (var key in details) {
this._storage.setItem(key, JSON.stringify(details[key]));
}
if (typeof callback === 'function') {
callback();
}
},
remove: function(keys) {
if (typeof keys === 'string') {
keys = [keys];
}
for (var i = 0; i < keys.length; ++i) {
this._storage.removeItem(keys[i]);
}
},
clear: function(callback) {
this._storage.clear();
callback();
},
getBytesInUse: function(keys, callback) {
var key, size = 0;
if (keys === null) {
for (key in this._storage) {
size += (this._storage[key] || '').length;
}
}
else {
if (typeof keys === 'string') {
keys = [keys];
}
for (key = 0; key < keys.length; ++key) {
size += (this._storage[keys[key]] || '').length;
}
}
callback(size);
}
};
vAPI.tabs = {
stack: {},
stackID: 1,
registerListeners: function() {
var onNavigation = this.onNavigation;
if (typeof onNavigation === 'function') {
this.onNavigation = function(e) {
// e.url is not present for local files or data URIs,
// or probably for those URLs which we don't have access to
if (!e.target || !e.target.url) {
return;
}
onNavigation({
frameId: 0,
tabId: vAPI.tabs.getTabId(e.target),
url: e.target.url
});
};
safari.application.addEventListener('navigate', this.onNavigation, true);
}
// ??
/* if (typeof this.onUpdated === 'function') { } */
// onClosed handled in the main tab-close event
// onPopup is handled in window.open on web-pages?
/* if (typeof onPopup === 'function') { } */
},
getTabId: function(tab) {
for (var i in vAPI.tabs.stack) {
if (vAPI.tabs.stack[i] === tab) {
return +i;
}
}
return -1;
},
get: function(tabId, callback) {
var tab;
if (tabId === null) {
tab = safari.application.activeBrowserWindow.activeTab;
tabId = this.getTabId(tab);
}
else {
tab = this.stack[tabId];
}
if (!tab) {
callback();
return;
}
callback({
id: tabId,
index: tab.browserWindow.tabs.indexOf(tab),
windowId: safari.application.browserWindows.indexOf(tab.browserWindow),
active: tab === tab.browserWindow.activeTab,
url: tab.url,
title: tab.title
});
},
open: function(details) {
if (!details.url) {
return null;
}
// extension pages
else if (!details.url.match(/^\w{2,20}:/)) {
details.url = vAPI.getURL(details.url);
}
// properties of the details object:
// url: 'URL', // the address that will be opened
// tabId: 1, // the tab is used if set, instead of creating a new one
// index: -1, // undefined: end of the list, -1: following tab, or after index
// active: false, // opens the tab in background - true and undefined: foreground
// select: true // if a tab is already opened with that url, then select it instead of opening a new one
var curWin, tab;
if (details.select) {
tab = safari.application.browserWindows.some(function(win) {
var rgxHash = /#.*/;
// this is questionable
var url = details.url.replace(rgxHash, '');
for (var i = 0; i < win.tabs.length; ++i) {
if (win.tabs[i].url.replace(rgxHash, '') === url) {
win.tabs[i].activate();
return true;
}
}
});
if (tab) {
return;
}
}
if (details.active === undefined) {
details.active = true;
}
curWin = safari.application.activeBrowserWindow;
// it must be calculated before opening a new tab,
// otherwise the new tab will be the active tab here
if (details.index === -1) {
details.index = curWin.tabs.indexOf(curWin.activeTab) + 1;
}
tab = details.tabId && this.stack[details.tabId]
|| curWin.openTab(details.active ? 'foreground' : 'background');
if (details.index !== undefined) {
curWin.insertTab(tab, details.index);
}
tab.url = details.url;
},
close: function(tab) {
if (!(tab instanceof SafariBrowserTab)) {
tab = this.stack[tab];
}
if (tab) {
tab.close();
}
},
injectScript: function(tabId, details, callback) {
var tab = tabId ? this.stack[tabId] : safari.application.activeBrowserWindow.activeTab;
if (details.file) {
var xhr = new XMLHttpRequest;
xhr.overrideMimeType('application/x-javascript;charset=utf-8');
xhr.open('GET', details.file, false);
xhr.send();
details.code = xhr.responseText;
}
tab.page.dispatchMessage('broadcast', {
portName: 'vAPI',
msg: {
cmd: 'runScript',
details: details
}
});
if (typeof callback === 'function') {
setTimeout(callback, 13);
}
}
};
// bind tabs to unique IDs
(function() {
var wins = safari.application.browserWindows, i = wins.length, j;
var tabs = [];
while (i--) {
j = wins[i].tabs.length;
while (j--) {
tabs.push(wins[i].tabs[j]);
}
}
return tabs;
})().forEach(function(tab) {
vAPI.tabs.stack[vAPI.tabs.stackID++] = tab;
});
safari.application.addEventListener('open', function(e) {
// ignore windows
if (e.target instanceof SafariBrowserTab) {
vAPI.tabs.stack[vAPI.tabs.stackID++] = e.target;
}
}, true);
safari.application.addEventListener('close', function(e) {
// ignore windows
if (!(e.target instanceof SafariBrowserTab)) {
return;
}
var tabId = vAPI.tabs.getTabId(e.target);
if (tabId > -1) {
// to not add another listener, put this here
// instead of vAPI.tabs.registerListeners
if (typeof vAPI.tabs.onClosed === 'function') {
vAPI.tabs.onClosed(tabId);
}
delete vAPI.tabIcons[tabId];
delete vAPI.tabs.stack[tabId];
}
}, true);
// update badge when tab is activated
safari.application.addEventListener('activate', function(e) {
// hide popover, since in some cases won't close by itself
var items = safari.extension.toolbarItems;
for (var i = 0; i < items.length; ++i) {
if (items[i].browserWindow === safari.application.activeBrowserWindow) {
if (items[i].popover) {
items[i].popover.hide();
}
break;
}
}
// ignore windows
if (!(e.target instanceof SafariBrowserTab)) {
return;
}
// update the badge, when tab is selected
vAPI.setIcon();
}, true);
// reload the popup when that is opened
safari.application.addEventListener('popover', function(e) {
e.target.contentWindow.document.body.textContent = '';
e.target.contentWindow.location.reload();
}, true);
vAPI.tabIcons = { /*tabId: {badge: 0, img: dict}*/ };
vAPI.setIcon = function(tabId, img, badge) {
var curTabId = vAPI.tabs.getTabId(safari.application.activeBrowserWindow.activeTab);
// from 'activate' event
if (tabId === undefined) {
tabId = curTabId;
}
else {
vAPI.tabIcons[tabId] = {
badge: badge || 0/*,
img: img*/
};
}
// if the selected tab has the same ID, then update the badge too,
// or always update it when changing tabs ('activate' event)
if (tabId === curTabId) {
var items = safari.extension.toolbarItems, i = items.length;
while (i--) {
if (items[i].browserWindow === safari.application.activeBrowserWindow) {
if (vAPI.tabIcons[tabId]) {
items[i].badge = vAPI.tabIcons[tabId].badge;
// items[i].img = vAPI.tabIcons[tabId].img;
}
else {
items[i].badge = 0;
}
return;
}
}
}
};
vAPI.messaging = {
listeners: {},
listen: function(listenerName, callback) {
this.listeners[listenerName] = callback;
},
setup: function(connector) {
if (this.connector) {
return;
}
this.connector = function(request) {
var callback = function(response) {
if (request.message.requestId && response !== undefined) {
request.target.page.dispatchMessage(
request.name,
{
requestId: request.message.requestId,
portName: request.message.portName,
msg: response
}
);
}
};
var sender = {
tab: {
id: vAPI.tabs.getTabId(request.target)
}
};
var listener = connector(request.message.msg, sender, callback);
if (listener === null) {
listener = vAPI.messaging.listeners[request.message.portName];
if (typeof listener === 'function') {
listener(request.message.msg, sender, callback);
} else {
console.error('µBlock> messaging > unknown request: %o', request.message);
}
}
};
// the third parameter must stay false (bubbling), so later
// onBeforeRequest will use true (capturing), where we can invoke
// stopPropagation() (this way this.connector won't be fired)
safari.application.addEventListener('message', this.connector, false);
},
broadcast: function(message) {
message = {
broadcast: true,
msg: message
};
for (var tabId in vAPI.tabs.stack) {
vAPI.tabs.stack[tabId].page.dispatchMessage('broadcast', message);
}
}
};
vAPI.net = {
registerListeners: function() {
var onBeforeRequest = this.onBeforeRequest;
if (typeof onBeforeRequest.callback === 'function') {
if (!Array.isArray(onBeforeRequest.types)) {
onBeforeRequest.types = [];
}
onBeforeRequest = onBeforeRequest.callback;
this.onBeforeRequest.callback = function(e) {
var block;
if (e.name !== 'canLoad') {
return;
}
// no stopPropagation if it was called from beforeNavigate event
if (e.stopPropagation) {
e.stopPropagation();
}
if (e.message.isWhiteListed) {
block = µBlock.URI.hostnameFromURI(e.message.isWhiteListed);
block = µBlock.URI.domainFromHostname(block) || block;
e.message = !!µBlock.netWhitelist[block];
return e.message;
}
// blocking unwanted pop-ups
if (e.message.type === 'popup') {
if (typeof vAPI.tabs.onPopup === 'function') {
e.message.type = 'main_frame';
e.message.sourceTabId = vAPI.tabs.getTabId(e.target);
if (vAPI.tabs.onPopup(e.message)) {
e.message = false;
return;
}
}
e.message = true;
return;
}
block = vAPI.net.onBeforeRequest;
if (block.types.indexOf(e.message.type) < 0) {
return true;
}
e.message.tabId = vAPI.tabs.getTabId(e.target);
block = onBeforeRequest(e.message);
// truthy return value will allow the request,
// except when redirectUrl is present
if (block && typeof block === 'object') {
if (block.cancel) {
e.message = false;
}
else if (e.message.type === 'script'
&& typeof block.redirectUrl === "string") {
e.message = block.redirectUrl;
}
else {
e.message = true;
}
}
else {
e.message = true;
}
return e.message;
};
safari.application.addEventListener('message', this.onBeforeRequest.callback, true);
}
}
};
vAPI.contextMenu = {
create: function(details, callback) {
var contexts = details.contexts;
var menuItemId = details.id;
var menuTitle = details.title;
if (Array.isArray(contexts) && contexts.length) {
contexts = contexts.indexOf('all') === -1 ? contexts : null;
}
else {
// default in Chrome
contexts = ['page'];
}
this.onContextMenu = function(e) {
var uI = e.userInfo;
if (uI && /^https?:\/\//i.test(uI.pageUrl)) {
if (contexts) {
var invalidContext = true;
for (var i = 0; i < contexts.length; ++i) {
if (contexts[i] === 'frame') {
if (uI.insideFrame) {
invalidContext = false;
break;
}
}
else if (contexts[i] === 'link') {
if (uI.linkHref) {
invalidContext = false;
break;
}
}
else if (contexts[i] === 'image') {
if (uI.srcUrl) {
invalidContext = false;
break;
}
}
else if (contexts[i] === 'audio' || contexts[i] === 'video') {
if (uI.srcUrl && uI.tagName === contexts[i]) {
invalidContext = false;
break;
}
}
else if (contexts[i] === 'editable') {
if (uI.editable) {
invalidContext = false;
break;
}
}
else if (contexts[i] === 'page') {
if (!(uI.insideFrame || uI.linkHref || uI.mediaType || uI.editable)) {
invalidContext = false;
break;
}
}
}
if (invalidContext) {
return;
}
}
e.contextMenu.appendContextMenuItem(menuItemId, menuTitle);
}
};
this.onContextMenuCommand = function(e) {
if (e.command === menuItemId) {
var tab = e.currentTarget.activeBrowserWindow.activeTab;
e.userInfo.menuItemId = menuItemId;
callback(e.userInfo, tab ? {
id: vAPI.tabs.getTabId(tab),
url: tab.url
} : undefined);
}
};
safari.application.addEventListener('contextmenu', this.onContextMenu);
safari.application.addEventListener("command", this.onContextMenuCommand);
},
remove: function() {
safari.application.removeEventListener('contextmenu', this.onContextMenu);
safari.application.removeEventListener("command", this.onContextMenuCommand);
this.onContextMenu = null;
this.onContextMenuCommand = null;
}
};
// «
}
// » footer
if (!self.chrome) {
self.chrome = { runtime: { lastError: null } };
}
})();
// «
|