Marcono1234 / known-sites-extension

Browser extension blocking websites the user has not opened before.
MIT License
0 stars 0 forks source link

Update unknown site page after "Open" was pressed #3

Open Marcono1234 opened 1 year ago

Marcono1234 commented 1 year ago

Problem

In some cases the unknown site extension page remains visible in Chrome and Firefox after pressing the "Open" button. Situations where this occurs are:

Possible solution

After pressing the "Open" button update the page and the tab title (?) to indicate that the site was opened / is being opened, for example by replacing the content with "Opened site \<domain>" (with domain having Unicode characters displayed?).

To account for the cases where Chrome and Firefox do actually display the target site, it might be good to add some delay to updating the blocked page content (a few hundred milliseconds) to prevent it from quickly flashing before the target site opens, and to be (in most cases) only visible if Chrome and Firefox do not display a different site.

Marcono1234 commented 1 year ago

Updating page title and clearing the body seems to be possible in extension/pages/blocked-unknown.js after the promise returned by browser.runtime.sendMessage was resolved:

.then(
  (_response) => {
    // If page is still open after some time, assume it was a direct download URL and
    // therefore browser did not change page; clear page content to avoid confusion
    setTimeout(() => {
      console.info(
        `Clearing page for URL ${urlValue} because content appears to not have changed`
      )
      document.title = urlValue
      document.body.innerHTML = ''
    }, 1000)
  },
  (rejected) => {
    console.error(
      `Failed sending message to open blocked URL ${urlValue}`,
      rejected
    )
  }
)

However, clearing the favicon might not be easily possible, and changing the URL without triggering a second page load might not be possible at all. window.history.replaceState throws an exception when trying to set the URL (probably due to protocol mismatch).