Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: "navigate" as a context #2

Open
tobyink opened this issue Apr 17, 2023 · 11 comments
Open

Feature request: "navigate" as a context #2

tobyink opened this issue Apr 17, 2023 · 11 comments

Comments

@tobyink
Copy link

tobyink commented Apr 17, 2023

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

@waldner
Copy link
Owner

waldner commented Apr 17, 2023

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.

@tobyink
Copy link
Author

tobyink commented Apr 17, 2023

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.

@waldner
Copy link
Owner

waldner commented Apr 17, 2023

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
Copy link
Owner

waldner commented Apr 17, 2023

My preference would be for webNavigation.onCreatedNavigationTarget or webNavigation.onBeforeNavigate

@waldner
Copy link
Owner

waldner commented Apr 17, 2023

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)

@tobyink
Copy link
Author

tobyink commented Apr 17, 2023

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

@tobyink
Copy link
Author

tobyink commented Apr 17, 2023

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.

@waldner
Copy link
Owner

waldner commented Apr 18, 2023

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
Copy link
Owner

waldner commented Apr 19, 2023

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.

@tobyink
Copy link
Author

tobyink commented Apr 20, 2023

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

@waldner
Copy link
Owner

waldner commented Apr 20, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants