Skip to content

Commit db7f54d

Browse files
committed
Add support to launch element picker in embedded frames
Related issue: - #1744 A new context menu entry, "Block element in frame...", will be present when right-clicking on a frame element. When this entry is clicked, uBO's element picker will be launched from within the embedded frame and function the same way as when launched from within the page.
1 parent 4b921f1 commit db7f54d

File tree

8 files changed

+56
-20
lines changed

8 files changed

+56
-20
lines changed

platform/chromium/vapi-background.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ vAPI.messaging = {
10101010
case 'extendClient':
10111011
vAPI.tabs.executeScript(tabId, {
10121012
file: '/js/vapi-client-extra.js',
1013+
frameId: portDetails.frameId,
10131014
}).then(( ) => {
10141015
callback();
10151016
});

src/_locales/en/messages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,10 @@
10271027
"message": "bytes",
10281028
"description": ""
10291029
},
1030+
"contextMenuBlockElementInFrame": {
1031+
"message": "Block element in frame...",
1032+
"description": "An entry in the browser's contextual menu"
1033+
},
10301034
"contextMenuTemporarilyAllowLargeMediaElements": {
10311035
"message": "Temporarily allow large media elements",
10321036
"description": "A context menu entry, present when large media elements have been blocked on the current site"

src/css/epicker-ui.css

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ html#ublock0-epicker,
1515
#ublock0-epicker aside {
1616
background-color: var(--default-surface);
1717
border: 1px solid var(--default-surface-border);
18-
bottom: 4px;
18+
bottom: 2px;
1919
box-sizing: border-box;
2020
cursor: default;
2121
display: none;
22+
max-height: calc(100vh - 4px);
2223
max-width: 36rem;
2324
min-width: 24rem;
25+
overflow-y: auto;
2426
padding: 4px;
2527
position: fixed;
26-
right: 4px;
27-
width: calc(40% - 4px);
28+
right: 2px;
29+
width: calc(40% - 2px);
2830
}
2931
#ublock0-epicker.paused:not(.zap) aside {
3032
display: block;
@@ -89,6 +91,8 @@ html#ublock0-epicker,
8991
box-sizing: border-box;
9092
font: 11px monospace;
9193
height: 8em;
94+
max-height: 50vh;
95+
min-height: 1em;
9296
padding: 2px;
9397
width: 100%;
9498
}

src/js/commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ vAPI.commands.onCommand.addListener(async command => {
165165
µb.epickerArgs.mouse = false;
166166
µb.elementPickerExec(
167167
tab.id,
168+
0,
168169
undefined,
169170
command === 'launch-element-zapper'
170171
);

src/js/contextmenu.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ const onBlockElement = function(details, tab) {
5858
}
5959

6060
µBlock.epickerArgs.mouse = true;
61-
µBlock.elementPickerExec(tab.id, tagName + '\t' + src);
61+
µBlock.elementPickerExec(tab.id, 0, `${tagName}\t${src}`);
62+
};
63+
64+
/******************************************************************************/
65+
66+
const onBlockElementInFrame = function(details, tab) {
67+
if ( tab === undefined ) { return; }
68+
if ( /^https?:\/\//.test(details.frameUrl) === false ) { return; }
69+
µBlock.epickerArgs.mouse = false;
70+
µBlock.elementPickerExec(tab.id, details.frameId);
6271
};
6372

6473
/******************************************************************************/
@@ -76,25 +85,33 @@ const onEntryClicked = function(details, tab) {
7685
if ( details.menuItemId === 'uBlock0-blockElement' ) {
7786
return onBlockElement(details, tab);
7887
}
88+
if ( details.menuItemId === 'uBlock0-blockElementInFrame' ) {
89+
return onBlockElementInFrame(details, tab);
90+
}
7991
if ( details.menuItemId === 'uBlock0-temporarilyAllowLargeMediaElements' ) {
8092
return onTemporarilyAllowLargeMediaElements(details, tab);
8193
}
8294
};
8395

8496
/******************************************************************************/
8597

86-
const menuEntries = [
87-
{
98+
const menuEntries = {
99+
blockElement: {
88100
id: 'uBlock0-blockElement',
89101
title: vAPI.i18n('pickerContextMenuEntry'),
90102
contexts: ['all'],
91103
},
92-
{
104+
blockElementInFrame: {
105+
id: 'uBlock0-blockElementInFrame',
106+
title: vAPI.i18n('contextMenuBlockElementInFrame'),
107+
contexts: ['frame'],
108+
},
109+
temporarilyAllowLargeMediaElements: {
93110
id: 'uBlock0-temporarilyAllowLargeMediaElements',
94111
title: vAPI.i18n('contextMenuTemporarilyAllowLargeMediaElements'),
95112
contexts: ['all'],
96113
}
97-
];
114+
};
98115

99116
/******************************************************************************/
100117

@@ -115,10 +132,11 @@ const update = function(tabId = undefined) {
115132
currentBits = newBits;
116133
let usedEntries = [];
117134
if ( newBits & 0x01 ) {
118-
usedEntries.push(menuEntries[0]);
135+
usedEntries.push(menuEntries.blockElement);
136+
usedEntries.push(menuEntries.blockElementInFrame);
119137
}
120138
if ( newBits & 0x02 ) {
121-
usedEntries.push(menuEntries[1]);
139+
usedEntries.push(menuEntries.temporarilyAllowLargeMediaElements);
122140
}
123141
vAPI.contextMenu.setEntries(usedEntries, onEntryClicked);
124142
};

src/js/epicker-ui.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,8 @@ const onStartMoving = (( ) => {
599599

600600
const move = ( ) => {
601601
timer = undefined;
602-
const r1 = Math.min(Math.max(r0 - mx1 + mx0, 4), rMax);
603-
const b1 = Math.min(Math.max(b0 - my1 + my0, 4), bMax);
602+
const r1 = Math.min(Math.max(r0 - mx1 + mx0, 2), rMax);
603+
const b1 = Math.min(Math.max(b0 - my1 + my0, 2), bMax);
604604
dialog.style.setProperty('right', `${r1}px`);
605605
dialog.style.setProperty('bottom', `${b1}px`);
606606
};
@@ -646,8 +646,8 @@ const onStartMoving = (( ) => {
646646
r0 = parseInt(style.right, 10);
647647
b0 = parseInt(style.bottom, 10);
648648
const rect = dialog.getBoundingClientRect();
649-
rMax = pickerRoot.clientWidth - 4 - rect.width ;
650-
bMax = pickerRoot.clientHeight - 4 - rect.height;
649+
rMax = pickerRoot.clientWidth - 2 - rect.width ;
650+
bMax = pickerRoot.clientHeight - 2 - rect.height;
651651
dialog.classList.add('moving');
652652
if ( isTouch ) {
653653
self.addEventListener('touchmove', moveAsync, { capture: true });

src/js/messaging.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ const onMessage = function(request, sender, callback) {
154154
case 'launchElementPicker':
155155
// Launched from some auxiliary pages, clear context menu coords.
156156
µb.epickerArgs.mouse = false;
157-
µb.elementPickerExec(request.tabId, request.targetURL, request.zap);
157+
µb.elementPickerExec(request.tabId, 0, request.targetURL, request.zap);
158158
break;
159159

160160
case 'gotoURL':

src/js/ublock.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,21 +414,29 @@ const matchBucket = function(url, hostname, bucket, start) {
414414

415415
/******************************************************************************/
416416

417-
µBlock.elementPickerExec = async function(tabId, targetElement, zap = false) {
417+
µBlock.elementPickerExec = async function(
418+
tabId,
419+
frameId,
420+
targetElement,
421+
zap = false,
422+
) {
418423
if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
419424

420425
this.epickerArgs.target = targetElement || '';
421426
this.epickerArgs.zap = zap;
422427

423428
// https://github.com/uBlockOrigin/uBlock-issues/issues/40
424429
// The element picker needs this library
425-
vAPI.tabs.executeScript(tabId, {
426-
file: '/lib/diff/swatinem_diff.js',
427-
runAt: 'document_end',
428-
});
430+
if ( zap !== true ) {
431+
vAPI.tabs.executeScript(tabId, {
432+
file: '/lib/diff/swatinem_diff.js',
433+
runAt: 'document_end',
434+
});
435+
}
429436

430437
await vAPI.tabs.executeScript(tabId, {
431438
file: '/js/scriptlets/epicker.js',
439+
frameId,
432440
runAt: 'document_end',
433441
});
434442

0 commit comments

Comments
 (0)