Skip to content

Commit

Permalink
Make the webNavigation permission optional
Browse files Browse the repository at this point in the history
Capture quality may be silently degraded if the permission
is not granted. No attempt to get active subframe is performed.
  • Loading branch information
maxnikulin committed Oct 29, 2024
1 parent dcfe08e commit 555d39c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,9 @@ Chrome: "Read your browsing history".
It is necessary to reliably restore tree of nested frames.
Consider the case when some element is focused in a subframe
and capture is invoked using keyboard shortcut.
Maybe I will add a less reliable fallback in future
to make this permission optional.
Another case is context menu invoked in a deeply nested frame.

You may grant this permission on the extension options page.[fn:3]

*** Access browser tabs (=tabs=)
:PROPERTIES:
Expand Down
17 changes: 17 additions & 0 deletions background/lr_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,23 @@ var lr_settings = lr_util.namespace(lr_settings, function lr_settings() {
host: true,
parent: "misc",
});
this.registerOption({
name: "permissions.webNavigation",
title: 'Access browser activity during navigation ("webNavigation")',
version: "0.4",
description: [
"Relevant to frames and embedded objects.",
"Without this permission the extension may be unable",
"to find active frame when invoked using",
"the toolbar button or the shortcut.",
"",
"Context menu is affected in less degree,",
"but capture info may still be incomplete",
"in the case of multiple level of nested frames."
],
type: "permission",
parent: "misc",
});
};

this.load = async function() {
Expand Down
29 changes: 24 additions & 5 deletions background/lr_tabframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ var lr_tabframe = lr_util.namespace(lr_tabframe, function lr_tabframe() {
};
}

lr_tabframe._hasWebNavigationPermission = async function _hasWebNavigationPermission() {
if (chrome.webNavigation?.getAllFrames === undefined) {
return false;
}
// Chromium-130 throws synchronously after `permissions.remove`
// chrome.webNavigation.getAllFrames({tabId: -1})
// Error: 'webNavigation.getAllFrames' is not available in this context.
//
// If the permission is not available
//
// TypeError: Error in invocation of webNavigation.getAllFrames(object details, function callback): Error at parameter 'details': Error at property 'tabId': Value must be at least 0.
try {
return await bapi.permissions.contains({ permissions: [ "webNavigation" ] });
} catch (ex) {
Promise.reject(ex);
}
return false;
}

Object.assign(this, {
FORMAT, VERSION,
makeCapture,
Expand Down Expand Up @@ -147,7 +166,7 @@ function focusedFrameChain(frameMap) {
async function lrGetAllFrames(tab) {
// Likely redundant protection, tab should be valid here
const tabId = tab != null ? tab.id : -1;
if (!(tabId >= 0)) {
if (!(tabId >= 0) || ! await lr_tabframe._hasWebNavigationPermission()) {
// May happen in Chromium-87 when context menu is invoked for a PDF file
// but should be handled by caller since <embed> element
// in Chrome created for PDF file has its own "tab".
Expand Down Expand Up @@ -515,12 +534,12 @@ async function lrGatherTabInfo(tab, clickData, activeTab, executor) {
// allows to avoid noise due to e.g. empty `<iframe>` somewhere on the page.
executor.step(
{ errorAction: lr_executor.ERROR_IS_WARNING },
function lrCheckFramePermissionsErrors(chain) {
async function lrCheckFramePermissionsErrors(chain) {
let count = 0;
let first = true;
let checkForActiveElement = await lr_tabframe._hasWebNavigationPermission();
for (const wrappedFrame of chain) {
if (first) {
first = false;
if (checkForActiveElement) {
checkForActiveElement = false;
const node = wrappedFrame?.summary?.activeElementNode?.toUpperCase?.();
if (node && ["IFRAME", "FRAME", "OBJECT", "EMBED"].indexOf(node) >= 0) {
// In the case of navigation `frame.src` does not match
Expand Down
4 changes: 2 additions & 2 deletions manifest-chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
"scripting",
"storage",
"activeTab",
"contextMenus",
"webNavigation"
"contextMenus"
],
"optional_permissions": [
"clipboardWrite",
"nativeMessaging",
"tabs",
"webNavigation",
"offscreen",
"tabGroups"
],
Expand Down
2 changes: 1 addition & 1 deletion manifest-common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ permissions:
- storage
- activeTab
- contextMenus
- webNavigation
optional_permissions:
# `clipboardWrite` allows to copy to clipboard any time.
# Added to reliably copy capture when preview page is disabled
Expand All @@ -63,3 +62,4 @@ optional_permissions:
- clipboardWrite
- nativeMessaging
- tabs
- webNavigation
2 changes: 1 addition & 1 deletion manifest-firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
"storage",
"activeTab",
"contextMenus",
"webNavigation",
"menus"
],
"optional_permissions": [
"clipboardWrite",
"nativeMessaging",
"tabs",
"webNavigation",
"<all_urls>"
],
"manifest_version": 2,
Expand Down
11 changes: 7 additions & 4 deletions pages/lrp_help.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 555d39c

Please sign in to comment.