Skip to content

How to inject scripts "faster" ? #549

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

Closed
Write opened this issue Oct 7, 2023 · 1 comment
Closed

How to inject scripts "faster" ? #549

Write opened this issue Oct 7, 2023 · 1 comment

Comments

@Write
Copy link

Write commented Oct 7, 2023

Hello,

I've already read issues regarding fast injection, and understand it's not possible.

I'm not sure if my issue is about "fast injection" or not, hence why i'm writing this, I'm also looking for advices.

I have a project that grab an external theme (.css) for Mastodon, and inject it inside the page.
I use MutationObserver's API on Firefox, and it just works fine.

Mastodon CSP's rules doesn't allow for raw injection of <style> tag, I'm forced to wait for meta header 'style-nonce', and get it's value to inject the page with the style's url.

I used to be able, during load time, to set <html> background value asap (it doesn't abid to CSP rules) so it wouldn't "flash" (ie : you're in dark mode and normal mastodon theme in light mode) as tag can be changed as soon as possible, we don't need to wait for the tag to exist.
document.querySelector('html').style.setProperty("background", backColor, "important");

However, that's not enough, as of right now, to avoid flash on Mastodon, one needs to override <body> background value, instantly. I need to be able to detect and fetch the body tag as soon as possible, because the tag need to exist, unlike <html>.

For both Chrome and Safari, MutationObserver is rarely fired outside of "Cmd+Ctrl+R" (command that clear the cache).
MutationObserver is really reliable to fetch any tag in the <head>, I don't understand why.

I have a workaround which use requestAnimation frame to detect elements, but I can't manage to override fast enough to avoid the weird flash.

You can look at my code here : https://github.com/Write/TangerineUI-Userscript/blob/main/TangerineUI.user.js

I hope I'm just missing something and there's a better way or API.

Thanks a lot for reading me.

EDIT :

I just wanted to add that I have other userscripts for other website, with no CSP protection, and injecting my custom css (embedded inside the userscript itself, this time) with :

    function addGlobalStyle(css) {
        var head, style;
        head = document.getElementsByTagName('head')[0];
        if (!head) { return; }
        style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = css.replace(/;/g, ' !important;');
        head.appendChild(style);
    }

and it does the flashing too. Which mean it's not injecting the style fast enough, it does work great on firefox though.
Apparently Darkreader has a way around that since applying dark theme doesn't do any flashing. I really don't know what I'm missing here.

@ACTCD
Copy link
Collaborator

ACTCD commented Oct 8, 2023

I think this should be posted in Discussions instead of Issues.

I'm not sure what you're looking for, but I think Mastodon has its own dark theme, so I'm not really sure how the FOUC you're talking about happens.

A good practice is to provide feedback to the project or the site itself for a first-party resolution, which is technically the easiest.

User script has greater latency than the extension itself because the extension needs to deal with user script metadata and a complex injection process.

We would like user script injection to be faster, but currently we are limited in what we can do.

Due to duplication and unclear question, I will close the issue, but you can continue to comment.

duplicate #184 #459

@ACTCD ACTCD closed this as completed Oct 8, 2023
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