Firefox-RunWith icon indicating copy to clipboard operation
Firefox-RunWith copied to clipboard

Feature request: "navigate" as a context

Open tobyink opened this issue 2 years ago • 11 comments

This would run the external command every time I navigate to a new page, not just when the command is selected from a menu.

tobyink avatar Apr 17 '23 09:04 tobyink

That is not a listed context for the context menus (see https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/menus/ContextType), so it'll probably need change at some other level. I will have to investigate.

waldner avatar Apr 17 '23 10:04 waldner

Yeah, it's not really the same kind of thing. I just want to be able to run an external script each time I visit a page on a certain domain.

Right now, the closest I've been able to get to is your Firefox extension, and manually running it from the "tab" context menu. Perhaps there's a different extension that would serve my needs better, but yours seems pretty close.

tobyink avatar Apr 17 '23 10:04 tobyink

It looks like the navigation flow is quite elaborate, do you have any preference regarding where you'd like to hook into? https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation

waldner avatar Apr 17 '23 10:04 waldner

Yet another option would be to use the webRequest API, which is slightly different (https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest)

waldner avatar Apr 17 '23 11:04 waldner

I don't have much of a preference at what stage it runs. I'm just looking for once per page load.

tobyink avatar Apr 17 '23 19:04 tobyink

Actually, onHistoryStateUpdated might be nice, as there are an increasing number of websites which use Javascript to replace the page content then update the address bar and history with a new URL.

tobyink avatar Apr 17 '23 19:04 tobyink

I'm doing some tests with both the webNavigation and webRequest APIs and I'm getting quite confused by the inconsistency of the results. Will keep experimenting.

waldner avatar Apr 18 '23 16:04 waldner

So using any of the webRequest API methods seems out of the question, as IIUC you want your command to run once when you navigate to a new page; since "going to a new page" usually implies making tens of HTTP requests (for assets, banners, javascripts, etc) and my understanding is that the webRequest API fires for each request, this results in your command being run multiple times. I'll try to find a working solution using the webNavigation API.

waldner avatar Apr 19 '23 22:04 waldner

Yeah true. I can't imagine many people would want dozens of external programs running for each navigation event.

tobyink avatar Apr 20 '23 07:04 tobyink

Can you try loading the uncompressed extension in debug mode from this branch: https://github.com/waldner/Firefox-RunWith/tree/feature/navigation ? (export your current config before doing that, if you want to preserve it for later import) Some notes for the new navigation context:

  • The command runs when the webNavigation.onCommitted event fires. After some tests, it seems to be a good compromise between "always fire" and "do not fire too late"
  • The command is NOT executed on pages like about:whatever or moz-extension:whatever
  • Some websites use subframes, which would cause multiple events to be fired. The specified command only runs when frameId is 0 (explicitly checked in the code), that is, for the top-level navigation context
  • In the configuration, the following items are ignored: Menu title (though something must be put there, if only for your reference) and Document URLs
  • The Target URLs filter DOES work, but unlike the normal menu entries, the filter is a regular expression (can be multiple, separated by commas); if the target navigation URL matches the expression, the command is run; as an exception for convenience, and since it's a pre-populated field, <all_urls> also works; it's converted internally to .*.
  • The built-in placeholders that can be used in the command to be replaced are different for the navigation context. The available fields are %%LINK%% (target url), %%TAB-ID%% and %%PROCESS-ID%%, with the hopefully obvious meaning.

Let me know what you think.

waldner avatar Apr 20 '23 13:04 waldner