fx-autoconfig icon indicating copy to clipboard operation
fx-autoconfig copied to clipboard

Proper method of injecting JavaScript and CSS into content

Open black7375 opened this issue 9 months ago • 2 comments

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

  • https://tscanlin.github.io/tocbot/

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

black7375 avatar Sep 04 '23 10:09 black7375

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.

MrOtherGuy avatar Sep 09 '23 07:09 MrOtherGuy

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

black7375 avatar Sep 09 '23 15:09 black7375