From fa5dd242c7527e345bbad3450d78901d6e3b2c6d Mon Sep 17 00:00:00 2001 From: Skeletonxf Date: Sun, 10 Nov 2024 18:05:19 +0000 Subject: [PATCH] Implement (faulty) detection for when to hide/show the button Seems now blocked on the navigation.canGoBack API being implemented, so waiting on https://bugzilla.mozilla.org/show_bug.cgi?id=1777171 --- src/duplicate-tab.js | 25 ++++++++++++++++++++++++- src/get-history.js | 1 + src/page/main.js | 7 +++++-- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 src/get-history.js diff --git a/src/duplicate-tab.js b/src/duplicate-tab.js index c42154b..5959306 100644 --- a/src/duplicate-tab.js +++ b/src/duplicate-tab.js @@ -182,10 +182,33 @@ export default class DuplicateTab { return true } if (data.getPageData === true) { + let oldTabHasHistory = false + try { + let injectionResult = (await browser.scripting.executeScript({ + target: { + tabId: id + }, + // The same code from the file as a function doesn't + // seem able to return the last expression and is always + // undefined, so we need to use a file for this even + // though we just need to execute a single line + files: [ 'src/get-history.js' ] + }))[0] + // Length is 1 when history is empty due to current tab + // FIXME: This doesn't work properly if there are pages + // forward of the current page, as they are included + // in the history too even when we have nothing to go back + // to. `navigation.canGoBack` seems what we actually want + // but it has not been implemented at the time of writing + oldTabHasHistory = injectionResult.result > 1 + } catch (error) { + console.error('Error checking for history in original tab', error) + } return { url: url, oldTabIsIncognito: incognito, - allowedIncognitoAccess: await browser.extension.isAllowedIncognitoAccess() + allowedIncognitoAccess: await browser.extension.isAllowedIncognitoAccess(), + oldTabHasHistory: oldTabHasHistory, } } } catch (error) { diff --git a/src/get-history.js b/src/get-history.js new file mode 100644 index 0000000..f7a4683 --- /dev/null +++ b/src/get-history.js @@ -0,0 +1 @@ +window.history.length diff --git a/src/page/main.js b/src/page/main.js index e8ab9e9..bfee343 100644 --- a/src/page/main.js +++ b/src/page/main.js @@ -47,8 +47,7 @@ buttons.forEach((b) => b.deleter = new IdempotentElementDeleter(b.element)) type: 'page', getPageData: true }) - const { url } = response - const { oldTabIsIncognito } = response + const { url, oldTabIsIncognito, oldTabHasHistory } = response document.getElementById('page-url').textContent += (url) document.getElementById('page-url-suffix').classList.remove('hidden') @@ -89,6 +88,10 @@ buttons.forEach((b) => b.deleter = new IdempotentElementDeleter(b.element)) let navigateBackTabNav = document.querySelector('#normal-and-navigate-back-tab-name') navigateBackTabNav.textContent = 'Private Browsing' } + if (!oldTabHasHistory) { + // nothing to navigate back with so delete the option + buttons.get('normal-and-navigate-back').deleter.run() + } const { allowedIncognitoAccess } = response if (allowedIncognitoAccess === false) { // the only case that needs user notification is moving