0

Is it possible to detect if the user opens the script file in the browser tab to view it or if the scripts load as <script> tag or a different way of loading scripts like ES Module import?

I've had this idea to add syntax highlighting and generate HTML output for script files (using Service Worker) when the user opens the script in a new tab, but I'm not sure if this is possible.

2
  • If user opens script as URL, it will not be executed by browser, just source code will be seen - so that's how you can "detect" it
    – Justinas
    Aug 22 at 11:30
  • @Justinas no, because on the service worker level both are just text. It's the same as detect on the server.
    – jcubic
    Aug 22 at 11:31

1 Answer 1

0

I think that I've found the difference:

When the script is loaded as a script the request got this header:

Accept: */*

and when open in a new tab it get this instead:

Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7

So you can detect if Accept header matches "text/html"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.