MrOtherGuy / fx-autoconfig

Load custom javascript in browser context
Mozilla Public License 2.0
173 stars 11 forks source link

How get the current file name ? #6

Closed Mitezuss closed 2 years ago

Mitezuss commented 2 years ago

it is posible ?

MrOtherGuy commented 2 years ago

The loader script doesn't provide any way to do that. I'm not sure if Firefox internal methods would give you a way to do that either, but I suppose it's possible.

Anyway it's not trivial to do because custom scripts may or may not run in same execution context.

What would you use that info for anyway? You are in control of you custom script and its file name so you could just hardcode those.

Mitezuss commented 2 years ago

Just wanna check if the script is running (specifically: if was launched and now is running -> So, i do some task depending). There are a few methods to do that (i did, hardcoding the name or using another functions or flags or mod on userChromeJS).

But, i was curious about get the name. I can not find info on google. But if the console get the name, when are a error/warn/info/log so should be a possible to get the name, at least if the script call to another function (on another js or jsm).

Anyway, thank for the reply.. (sry my english)

MrOtherGuy commented 2 years ago

You can check the current state of each script using _ucUtils.getScriptData() which returns an array of script descriptors for each script the loader knows about. Each entry will have a isRunning property with true or false indicating if it is running or not. Note that isRunning means the script body has been executed at least once, not if the script is actively doing something (you probably can't even figure that out in any way).

Anyway, one way to get the current filename is to generate a new Error object. This example would log the uri of the current file after the window has been loaded.

_ucUtils.windowIsReady(window)
.then(()=>{
  let e = new Error();
  console.log(e.fileName)
})