MrOtherGuy / fx-autoconfig

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

_ucUtils.updateStyleSheet() doesn't work #34

Open EmperorEntropy opened 11 months ago

EmperorEntropy commented 11 months ago

Hello. I just installed the manager, and I can confirm that it works since the console log message for test.uc.js gets printed in the browser console. However, _ucUtils.updateStyleSheet() doesn't seem to be working all since it keeps returning false even though I have a userChrome.css folder inside the chrome folder. I also tried placing a copy in the resources folder, but I still get false. I even tried _ucUtils.updateStyleSheet("userChrome.css"), but I get false as well.

The script I want to run that uses this function is

_ucUtils.registerHotkey({
  id: "updateCSShotkey",
  modifiers: "shift",
  key: "F1"
},()=>_ucUtils.updateStyleSheet());

console.log("Testing!");

Testing gets printed, but _ucUtils.updateStyleSheet() doesn't do anything. How can I make the function recognize my userChrome.css? Also, does the function automatically reload userContent.css as well?

If it helps, my OS is a mac, and my Firefox version is 116. Thanks.

MrOtherGuy commented 11 months ago

Hmm... I don't know what could be going wrong there. Your script seems to work correctly on Windows, and I don't have any mac system to test it with.

The scenarios where that return false (without any errors in console) would be if:

I suspect it's the last one, but it's just a guess. Your best option would be to use browser toolbox to add a breakpoint inside updateStyleSheet() and see what it ends up doing.

EmperorEntropy commented 11 months ago

Thanks for the reply. How would I exactly add a breakpoint to the function? If it also helps, _ucUtils.updateStyleSheet("userChrome.au.css","author") and _ucUtils.updateStyleSheet("userChrome.ag.css","agent") works with the pre-installed stylesheets but not with my created stylesheet. My userchrome.css is a CSS file that is currently empty.

MrOtherGuy commented 11 months ago

Handling of userChrome.css is totally different than any author or agent styles. First, make sure you have named the file userChrome.css and that it is inside <profile_dir>/chrome/ directory - otherwise Firefox is not going to load it in the first place. Second, to update/refresh userChrome.css, you need to call _ucUtils.updateStyleSheet() either without any arguments or with _ucUtils.updateStyleSheet("../userChrome.css")

EmperorEntropy commented 11 months ago

I did just that, and I still get false. Even though userChrome.css exists within the chrome directory and is correctly named, _ucUtils.updateStyleSheet() and _ucUtils.updateStyleSheet("../userChrome.css") both give me false. I can load userChrome.css and have confirmed it, but the function just doesn't recognize it.

EmperorEntropy commented 11 months ago

I'm not sure if this helps, but I have been trying alternate methods to reload the userChrome.css. Sadly, none of them work for me. The other method I tried was defining a .uc.js file with the contents found in https://gist.github.com/jscher2000/ad268422c3187dbcbc0d15216a3a8060. It initially worked for about half an hour, and then it suddenly stopped working for no reason. I tried getting it working again, but I failed. I'm not sure why the script would suddenly stop working after a while, and no error message appears. Hopefully, this piece of information helps narrow down the issue.

I have the 64-bit of Firefox 116.0.2 on a M2 Mac. Right now, I'm not sure if there's a problem with the scripts I'm using or a problem with my installation of Firefox. Thanks.

MrOtherGuy commented 11 months ago

I don't know how things worked four years ago, but nowadays at least userChrome.css is not loaded via style sheet service. I don't really know, but I think the script you linked would register it on top of the already existing style and you would end up in situation where you have both the old and new versions being loaded simultaneously - which I would imagine isn't healthy.

I still think your best hope is to use the browser toolbox debugger. It works just like website devtools debugger. Just find utils.sys.mjs in the Sources tab (it's in Main thread > chrome:// > content). Then add a breakpoint somewhere, for example here and then call the method. The execution should then pause at that line and you can use the control in the debugger to continue step-by-step to find out what the method ends up doing. Just remove the breakpoint after you are done.

EmperorEntropy commented 11 months ago

Thanks for your response! I did what you said, and I think the issue is the following line:

let target = sheets.find(sheet => sheet.href === entryFilePath);

The variable entryFilePath correctly finds the location of my userChrome.css, but target is undefined. As a result, the overall function returns false.

I’m not exactly sure how to resolve this problem. The sheets variable has a total of 54 CSS Style Sheets, one of them is a path to my userChrome.css and the other is a path to my userContent.css. However, it appears the function cannot find the sheet it is looking for.

Edit 1: I think I know why it fails. entryFilePath is defined as

file:////Users/kl/Library/Application Support/Firefox/Profiles/6mrmbkei.default-release/chrome/userChrome.css

However, the href attribute of my userChrome.css sheet is

file:///Users/kl/Library/Application%20Support/Firefox/Profiles/6mrmbkei.default-release/chrome/userChrome.css

The space %20 in Application Support, which is a part of macOS's system, ruins this. As a result, target is undefined, and the function can't find my userChrome.css file. entryFilePath also has an extra \ which messes things up too.

I think resolving these two would fix the overall issue.

Edit 2: I did a temporary fix by modifying the code slightly, and it works! This was indeed the issue, and now _ucUtils.updateStyleSheet() returns true and works properly! Thank you so much!

MrOtherGuy commented 11 months ago

Good findings, great work!

This is actually fixed in my development version already because the update function doesn't use that manually constructed entryFilePath variable anymore - though I didn't anticipate that would have been the issue.

MrOtherGuy commented 11 months ago

So, if the issue here is what I think it is, then the latest version should work fine - provided that I have not broken something else.