MrOtherGuy / fx-autoconfig

Load custom javascript in browser context
Mozilla Public License 2.0
164 stars 10 forks source link

Proper method of injecting JavaScript and CSS into content #42

Open black7375 opened 10 months ago

black7375 commented 10 months ago

To explain my use case, I want to create a table of contents on the right in the about:preferences.

image

To do this, I have to inject JavaScript and CSS into the about:preferences page. I think it's possible with AutoConfig, but I don't know the right way. (I hope there is no effect on browser.xhtml)

It may also be associated with #33

MrOtherGuy commented 10 months ago

Accessing content documents in general is very tricky and something I try to stay away from.

But if the content document you are trying to access is in the parent process (like about:preferences and about:addons) then you can access them trivially - just include their address in the script header:

// ==UserScript==
// @include       about:preferences
// ==/UserScript==
console.log(document.location.href)

This script should only be executed in about:preferences - not the main window which is the default.

Note that scripts are evaluated during DOMContentLoaded event, so chances are that the page has not yet been fully constructed at that point.

black7375 commented 10 months ago

Oh thank you. I'll try and share the code if I can!!

florinsdistortedvision commented 2 months ago

@MrOtherGuy sorry to bother, but it seems on the latest Nightly (127) the scripts targeting such pages as about:preferences don't seem to work for me, even that console.log example you have provided. Can you replicate this issue on your end?

MrOtherGuy commented 2 months ago

Hmm. Seems I can indeed replicate that. Do you know the exact date when this started to happen?

florinsdistortedvision commented 2 months ago

Unfortunately I don't, i have tried your example last time on 115ESR which worked fine, and haven't tried since. I was working on something today for the latest Nightly and it just nothing seemed to work.

MrOtherGuy commented 2 months ago

All right, I also tried in Firefox 125 and there it seems to work (although fx-autoconfig version I have on my release install is older, but I kinda doubt that's the issue).

What seems to be happening is that loading about:preferences is not firing observer with topic "domwindowopened". That is the one and only method the loader uses to determine when a new "injection target" is being loaded.

florinsdistortedvision commented 2 months ago

Interesting, not sure what Mozilla might've broke since 125. The reason I was using Nightly was to prep for 128ESR release which is coming soon.

florinsdistortedvision commented 2 months ago

@MrOtherGuy found the issue, if I use the utils from 0.8.6 version everything is created fine and (console log). However it is broken in the latest utils version 0.9.0

MrOtherGuy commented 2 months ago

Yes, I just figured this same as well. So my initial assessment was wrong, scripts are not executed in about:preferences or equivalent not because domwindowopened was not observed (it never was), but because in newer versions the DOMWindowOpened event listener is removed after the first callback - which is called for the window itself. In earlier versions, the event listener stayed around and was then called for any (main process) document that was later opened inside that window.

I'm pretty sure I stumbled upon some problem that was caused by the old behavior because some script was running unexpectedly, but I cannot remember what that was.

It would be easy and I suppose pretty reasonable to add a new pref that essentially restores the old behavior - although that was never really intentional, it just accidentally worked.