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

Add new API for extensions (required for cosmetic filtering + scriptlet injection) #7630

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alkim0
Copy link

@alkim0 alkim0 commented Mar 19, 2023

As per #7629 (comment), I've separated out the API changes required by #7629 into another PR. The proposed API changes are, however, general enough that they can be used for other components/extensions. For example, per-domain user stylesheets (#3854) would be very simple to implement as a separate component/extension using the newly suggested api.hook.before_loaded and api.Tab.add_dynamic_css. I believe the proposed changes should also be enough to support something like Dark Reader in qutebrowser (#3780).

API Changes

api.hook.before_loaded

A hook that is guaranteed to be notified before a page loads. Components which need to do something before a page loads should use this hook. Note the hook may be called multiple times before a page loads. The api.Tab object which is about to load and the url which we are about to load are passed in as arguments.

api.hook.load_started

A hook that is called after the page loads. Components which need to do something after a page starts loading should use this hook. The api.Tab object which is starting to load is passed in as an argument.

api.hook.load_finished

A hook that is called after the page finishes loading. Components which need to do something after a page load finishes should use this hook. The api.Tab object which just finished loading and the success flag of whether the load was done successfully is passed in as arguments.

api.Tab.add_dynamic_css

Adds css which will get applied to every page the api.Tab object loads. This has lower precedence than the user-specified stylesheet in the user config. This css is expected to change often, hence the "dynamic".

api.Tab.remove_dynamic_css

Removes the aforementioned css which will get applied to every page the api.Tab object loads.

api.Tab.add_web_script

Adds a snippet of javascript to be executed during the api.Tab object's page load. The specific moment in which the web script executes can be controlled with api.usertypes.InjectionPoint. These "web scripts" are intentionally separated from the greasemonkey scripts. Greasemonkey scripts are added and managed by the end user, but these scripts are meant to be used by the components/extensions of qutebrowser.

api.Tab.remove_web_script

Removes the aforementioned snippet of javascript to be executed during the api.Tab object's page load.

api.usertypes.InjectionPoint

A type representing the point at which to execute an injected web script.

Non-API changes to core implementation

Emitting api.Tab.before_loaded signal multiple times

In addition to being emitted during _load_url_prepare, the api.Tab.before_load_started signal is now also emitted during _on_navigation_request. The reason for this is that _load_url_prepare is not always called before a new page loads. For example, this can happen when a non-clickable object is clicked to open in the background. In this case, TabbedBrowser.tabopen will be called with url=None, so load_url and _load_url_prepare are not called. However, _on_navigation_request is always called, and the url is always known by this point. For a reliable api.hook.before_loaded hook, it is essential we emit before_load_started during _on_navigation_request as well. We don't want to remove the emit from _load_url_prepare either, since that gets called before _on_navigation_request, and we need the hook to fire as soon as possible.

Other changes

  • _update_stylesheet now also includes stylesheet.js in its injected script (wrapped in the global wrapper) because sometimes asynchronous code runs before injected scripts, and stylesheet.set_css will obviously fail if window._qutebrowser.stylesheet has not been specified.
  • Added additional javascript logging to scripts to denote when certain scripts are run (only if the current logging level is DEBUG).

@alkim0 alkim0 changed the title Add new API for extensions (required for adblock) Add new API for extensions (required for cosmetic filtering + scriptlet injection) Mar 19, 2023
@The-Compiler The-Compiler moved this from Backlog to Inbox in Pull request backlog Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

1 participant