Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emit javascript debugger; from Script Sharp

Tags:

script#

I want to emit just debugger; to javascript from c# script sharp code. I realize I could write Script.Literal("debugger"). I'd rather not use Script.Literal unless absolutely needed. I'd prefer to write something like Debugger.Break(); in c# with the emitted javascript being just debugger; Is there some sort of attribute that will let me do this if I create an Imported Library?

like image 500
DTig Avatar asked Feb 25 '26 14:02

DTig


1 Answers

This is quite a hack, but it works. You gotta get creative when what you want to do isn't officially supported, right?

It's based on the way Script.Alert works and makes use of the [ScriptAlias] attribute to include a JavaScript comment. Luckily ScriptAliasAttribute passes along the value provided exactly and doesn't sanitize it in any way.

namespace MyNamespace
{

    [GlobalMethods]
    [IgnoreNamespace]
    [Imported]
    public static class Debugger
    {
        [ScriptAlias("debugger;//")]//must comment out ();
        public static void Break()
        {
            //this outputs -  debugger;//();
        }
    }
}

So, using the above code, you can write Debugger.Break(); in your C# code, and you'll get debugger;//(); in the JavaScript emitted. A little ugly, but it works.

like image 147
jamauss Avatar answered Feb 28 '26 13:02

jamauss