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
|
/*******************************************************************************
uBlock Origin - a comprehensive, efficient content blocker
Copyright (C) 2014-present Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
'use strict';
/******************************************************************************/
import µb from './background.js';
import { i18n$ } from './i18n.js';
/******************************************************************************/
const contextMenu = (( ) => {
/******************************************************************************/
if ( vAPI.contextMenu === undefined ) {
return {
update: function() {}
};
}
/******************************************************************************/
const BLOCK_ELEMENT_BIT = 0b00001;
const BLOCK_RESOURCE_BIT = 0b00010;
const TEMP_ALLOW_LARGE_MEDIA_BIT = 0b00100;
const SUBSCRIBE_TO_LIST_BIT = 0b01000;
const VIEW_SOURCE_BIT = 0b10000;
/******************************************************************************/
const onBlockElement = function(details, tab) {
if ( tab === undefined ) { return; }
if ( /^https?:\/\//.test(tab.url) === false ) { return; }
let tagName = details.tagName || '';
let src = details.frameUrl || details.srcUrl || details.linkUrl || '';
if ( !tagName ) {
if ( typeof details.frameUrl === 'string' ) {
tagName = 'iframe';
} else if ( typeof details.srcUrl === 'string' ) {
if ( details.mediaType === 'image' ) {
tagName = 'img';
} else if ( details.mediaType === 'video' ) {
tagName = 'video';
} else if ( details.mediaType === 'audio' ) {
tagName = 'audio';
}
} else if ( typeof details.linkUrl === 'string' ) {
tagName = 'a';
}
}
µb.epickerArgs.mouse = true;
µb.elementPickerExec(tab.id, 0, `${tagName}\t${src}`);
};
/******************************************************************************/
const onBlockElementInFrame = function(details, tab) {
if ( tab === undefined ) { return; }
if ( /^https?:\/\//.test(details.frameUrl) === false ) { return; }
µb.epickerArgs.mouse = false;
µb.elementPickerExec(tab.id, details.frameId);
};
/******************************************************************************/
const onSubscribeToList = function(details) {
let parsedURL;
try {
parsedURL = new URL(details.linkUrl);
}
catch(ex) {
}
if ( parsedURL instanceof URL === false ) { return; }
const url = parsedURL.searchParams.get('location');
if ( url === null ) { return; }
const title = parsedURL.searchParams.get('title') || '?';
const hash = µb.selectedFilterLists.indexOf(parsedURL) !== -1
? '#subscribed'
: '';
vAPI.tabs.open({
url:
`/asset-viewer.html` +
`?url=${encodeURIComponent(url)}` +
`&title=${encodeURIComponent(title)}` +
`&subscribe=1${hash}`,
select: true,
});
};
/******************************************************************************/
const onTemporarilyAllowLargeMediaElements = function(details, tab) {
if ( tab === undefined ) { return; }
const pageStore = µb.pageStoreFromTabId(tab.id);
if ( pageStore === null ) { return; }
pageStore.temporarilyAllowLargeMediaElements(true);
};
/******************************************************************************/
const onViewSource = function(details, tab) {
if ( tab === undefined ) { return; }
const url = details.linkUrl || details.frameUrl || details.pageUrl || '';
if ( /^https?:\/\//.test(url) === false ) { return; }
µb.openNewTab({
url: `code-viewer.html?url=${self.encodeURIComponent(url)}`,
select: true,
});
};
/******************************************************************************/
const onEntryClicked = function(details, tab) {
if ( details.menuItemId === 'uBlock0-blockElement' ) {
return onBlockElement(details, tab);
}
if ( details.menuItemId === 'uBlock0-blockElementInFrame' ) {
return onBlockElementInFrame(details, tab);
}
if ( details.menuItemId === 'uBlock0-blockResource' ) {
return onBlockElement(details, tab);
}
if ( details.menuItemId === 'uBlock0-subscribeToList' ) {
return onSubscribeToList(details);
}
if ( details.menuItemId === 'uBlock0-temporarilyAllowLargeMediaElements' ) {
return onTemporarilyAllowLargeMediaElements(details, tab);
}
if ( details.menuItemId === 'uBlock0-viewSource' ) {
return onViewSource(details, tab);
}
};
/******************************************************************************/
const menuEntries = {
blockElement: {
id: 'uBlock0-blockElement',
title: i18n$('pickerContextMenuEntry'),
contexts: [ 'all' ],
documentUrlPatterns: [ 'http://*/*', 'https://*/*' ],
},
blockElementInFrame: {
id: 'uBlock0-blockElementInFrame',
title: i18n$('contextMenuBlockElementInFrame'),
contexts: [ 'frame' ],
documentUrlPatterns: [ 'http://*/*', 'https://*/*' ],
},
blockResource: {
id: 'uBlock0-blockResource',
title: i18n$('pickerContextMenuEntry'),
contexts: [ 'audio', 'frame', 'image', 'video' ],
documentUrlPatterns: [ 'http://*/*', 'https://*/*' ],
},
subscribeToList: {
id: 'uBlock0-subscribeToList',
title: i18n$('contextMenuSubscribeToList'),
contexts: [ 'link' ],
targetUrlPatterns: [ 'abp:*', 'https://subscribe.adblockplus.org/*' ],
},
temporarilyAllowLargeMediaElements: {
id: 'uBlock0-temporarilyAllowLargeMediaElements',
title: i18n$('contextMenuTemporarilyAllowLargeMediaElements'),
contexts: [ 'all' ],
documentUrlPatterns: [ 'http://*/*', 'https://*/*' ],
},
viewSource: {
id: 'uBlock0-viewSource',
title: i18n$('contextMenuViewSource'),
contexts: [ 'page', 'frame', 'link' ],
documentUrlPatterns: [ 'http://*/*', 'https://*/*' ],
},
};
/******************************************************************************/
let currentBits = 0;
const update = function(tabId = undefined) {
let newBits = 0;
if (
µb.userSettings.contextMenuEnabled &&
µb.userFiltersAreEnabled() &&
tabId !== undefined
) {
const pageStore = µb.pageStoreFromTabId(tabId);
if ( pageStore && pageStore.getNetFilteringSwitch() ) {
if ( pageStore.shouldApplySpecificCosmeticFilters(0) ) {
newBits |= BLOCK_ELEMENT_BIT;
} else {
newBits |= BLOCK_RESOURCE_BIT;
}
if ( pageStore.largeMediaCount !== 0 ) {
newBits |= TEMP_ALLOW_LARGE_MEDIA_BIT;
}
}
newBits |= SUBSCRIBE_TO_LIST_BIT;
}
if ( µb.hiddenSettings.filterAuthorMode ) {
newBits |= VIEW_SOURCE_BIT;
}
if ( newBits === currentBits ) { return; }
currentBits = newBits;
const usedEntries = [];
if ( (newBits & BLOCK_ELEMENT_BIT) !== 0 ) {
usedEntries.push(menuEntries.blockElement);
usedEntries.push(menuEntries.blockElementInFrame);
}
if ( (newBits & BLOCK_RESOURCE_BIT) !== 0 ) {
usedEntries.push(menuEntries.blockResource);
}
if ( (newBits & TEMP_ALLOW_LARGE_MEDIA_BIT) !== 0 ) {
usedEntries.push(menuEntries.temporarilyAllowLargeMediaElements);
}
if ( (newBits & SUBSCRIBE_TO_LIST_BIT) !== 0 ) {
usedEntries.push(menuEntries.subscribeToList);
}
if ( (newBits & VIEW_SOURCE_BIT) !== 0 ) {
usedEntries.push(menuEntries.viewSource);
}
vAPI.contextMenu.setEntries(usedEntries, onEntryClicked);
};
/******************************************************************************/
// https://github.com/uBlockOrigin/uBlock-issues/issues/151
// For unknown reasons, the currently active tab will not be successfully
// looked up after closing a window.
vAPI.contextMenu.onMustUpdate = async function(tabId = undefined) {
if ( µb.userSettings.contextMenuEnabled === false ) {
return update();
}
if ( tabId !== undefined ) {
return update(tabId);
}
const tab = await vAPI.tabs.getCurrent();
if ( tab instanceof Object === false ) { return; }
update(tab.id);
};
return { update: vAPI.contextMenu.onMustUpdate };
/******************************************************************************/
})();
/******************************************************************************/
export default contextMenu;
/******************************************************************************/
|