Skip to content
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

FireMonkey script fails to start on its own #649

Closed
wfaulk opened this issue Aug 7, 2024 · 8 comments
Closed

FireMonkey script fails to start on its own #649

wfaulk opened this issue Aug 7, 2024 · 8 comments

Comments

@wfaulk
Copy link

wfaulk commented Aug 7, 2024

I have a userscript that functions fine when I manually run it via icon->script->Run, but it doesn't ever start on its own. It matches the URL, the icon shows a "1", and the script is listed in the upper section after clicking the icon, but it doesn't ever seem to actually do anything at all.

I have actually written a test userscript that is just:

setInterval(() => { console.log("script running"); }, 500);

and it still doesn't run until I manually click the Run button. It feels like it's just never triggered. I believe that I have tried all the possible @run-at values.

The page, unfortunately, can only be run as a popup from another page (at least I haven't found a way to get it to function otherwise), and then it bogs down as soon as it's loaded, and it takes several seconds for me to be able to bring up the developer tools, which makes it that much more difficult to troubleshoot.

What can I try to do to run this down?

@erosman
Copy link
Owner

erosman commented Aug 7, 2024

Can you post a test script so that I can test it?

@wfaulk
Copy link
Author

wfaulk commented Aug 7, 2024

Okay, this is weird. This is my test script:

// ==UserScript==
// @name             UCS KVM run test
// @match            *://*/app/ucsm/kvm.html
// @version          1.0
// ==/UserScript==

setInterval(() => { console.log("script running"); }, 500);

This doesn't cause any logging to happen.

But just to make sure that I hadn't screwed something up, I changed the match:

// ==UserScript==
// @name             UCS KVM run test
// @match            *://*/*
// @version          1.0
// ==/UserScript==

setInterval(() => { console.log("script running"); }, 500);

I then checked that I was getting logs on any random web page, and I was. But before I changed it back, I checked my original page, and it was running there, too.

So I changed the match back, and it failed to run again. But the icon showed that it matched. So then I changed it back to match everything and told it to log window.location:

// ==UserScript==
// @name             UCS KVM run test
// @match            *://*/*
// @version          1.0
// ==/UserScript==

setInterval(() => { console.log(window.location); }, 500);

I then went back to my original page and it's logging the location over and over and over:

https://a.b.c.d/app/ucsm/kvm.html

(My real internal IP address redacted.)

The real location's "hostname" is a bare IP address. Is that relevant?

@erosman
Copy link
Owner

erosman commented Aug 8, 2024

  • Try testing with a larger interval as logging twice per second can cause complications
  • Try testing with a simple logging without setInterval

@wfaulk
Copy link
Author

wfaulk commented Aug 8, 2024

Like I said, the page I'm wanting to affect with this userscript is only accessible as a new tab/window from another page, and once that window loads, it's so immediately bogged down with whatever it's doing that it takes a very long time to get a console up: probably well over 5 seconds, by which time I would miss seeing a log entry that's only run once.

Oh, but I can have it alert(). Okay, I've done that. The new test script is this:

// ==UserScript==
// @name             UCS KVM run test
// @match            *://*/*
// @version          1.0
// ==/UserScript==

window.alert(window.location);

That works on basically every page, including the page I'm interested in. (Oddly, it does not work on https://www.yahoo.com/.)

If I change that @match to *://*/app/ucsm/kvm.html, it no longer works on the page I'm interested in, but the FireMonkey UI still shows that it's matching. If I set the @match to the exact URL (whose host, again, is a bare IPv4 address in this case), it still doesn't work on the page I'm interested in.

What I just discovered is that if I set the @match to *://*/app/ucsm/kvm.html* (with a trailing wildcard) then it does work (and doesn't match other pages). So I changed the alert to dump window.location.href as hexadecimal, to see if there was some invisible character at the end, and I found that it produced exactly what I'd expect. It ended in:

002f006100700070002f007500630073006d002f006b0076006d002e00680074006d006c

I chopped off the beginning in order to protect my internal IP address, but that definitely ends with just /kvm.html.

Maybe there is some trailing data in what's being checked and window.location.href isn't the correct thing for me to test.

I guess this means that I have a workaround, but I'd like to understand why it's not working with the un-wildcarded path.

@erosman
Copy link
Owner

erosman commented Aug 8, 2024

If I change that @match to *://*/app/ucsm/kvm.html, it no longer works on the page I'm interested in, but the FireMonkey UI still shows that it's matching. If I set the @match to the exact URL (whose host, again, is a bare IPv4 address in this case), it still doesn't work on the page I'm interested in.

Check from Web Developer Tools under Debugger tab and see if the userscript is loaded.

I guess this means that I have a workaround, but I'd like to understand why it's not working with the un-wildcarded path.

As you have mentioned, there might be some invisible trailing characters in the path.

Another scenario is that ...

  • the page loads an interim page with some trailing path
  • the page then uses JavaScript to navigate to the final page and changes the URL with JavaScript

JavaScript navigation is a problem for content script & userscript injection. Many sites are using it (e.g. Github) and the userscript injection API doesn't fire on JavaScript navigation (as it is not a real navigation & simply replaces the content of page).

See also:

@wfaulk
Copy link
Author

wfaulk commented Aug 8, 2024

Check from Web Developer Tools under Debugger tab and see if the userscript is loaded.

I guess I don't know how to do that. I have set it back to match every page and it's definitely sending the alert, but I don't see anywhere in the Debugger tab where it shows the script.


I think I found the problem, and I don't know if it's a bug or not.

It turns out that the URL that's being requested is:

https://a.b.c.d/app/ucsm/kvm.html?&kvmIpAddr=w.x.y.z

If I set the @match to that whole URL, then it works. But I feel like it should be ignoring query strings when matching to see if it should run. Maybe that's not the spec, though.

I confirmed that if I set the match to https://www.hp.com/us-en/home.html and load that page, then I get an alert, but if I then load https://www.hp.com/us-en/home.html?test=1, then I don't get the alert, so that's almost certainly the problem I am running into.

I guess something on the page I'm trying to get this script to run on is stripping the query string from the URL shown in the URL bar, so I didn't even know it was there until I did this browser.link.open_newwindow trick.

@erosman
Copy link
Owner

erosman commented Aug 8, 2024

AFA matching, the following are not the same. Many sites use the search property to point to different pages.

https://www.hp.com/us-en/home.html
https://www.hp.com/us-en/home.html?test=1

@wfaulk
Copy link
Author

wfaulk commented Aug 8, 2024

Ugh, I guess you're right. It's possible that a query-less URL could be distinct from the same URL with a query.

Lesson learned: Every @match that has a URL with a specified path should have a twin with the same URL plus ?*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants