简体   繁体   English

使用C#将BHO注入javascript-如何正确转义字符串

[英]Injecting javascript with BHO using C# - how to escape strings properly

I have the following code in C# that I'm able to attach to IE, and it runs through fine until I hit the JSON, which I receive a javascript error complaining about the syntax. 我在C#中有以下代码,可以将其附加到IE,并且可以正常运行,直到遇到JSON(我收到一个抱怨语法的JavaScript错误)为止。 How exactly should I escape javscript code within C# ? 我到底应该如何在C#中转义javscript代码?

                string jsonStr = @"[ 
                                     { \'name\': \'Obj1\', \'description\': \'Test description...\', \'url\':\'http://www.test.com\' },
                                     { \'name\': \'Obj2\', \'description\': \'Testing...\', \'url\':\'http://www.test.com\' },
                                     { \'name\': \'Obj3\', \'description\': \'Welp...\', \'url\':\'http://www.test.com\' }
                                   ]";

                IHTMLScriptElement scriptObject = (IHTMLScriptElement)document.createElement("script");
                scriptObject.type = @"text/javascript";
                scriptObject.text = @"function test() { 
                                        var Edit = 'document.getElementById(\'tTest\').innerHTML = \'<h2 class=\'label3\'><span>Foo</span></h2><ol class=\'container-list\'>';
                                        var json = '" + jsonStr + @"';
                                        $.each(json, function (index, x) {
                                                                    Edit += '<li class=\'test1\'><h3><a href=\'#\'><b>' + x.name + '</b> 1</a></h3><div class=\'url\'><cite>' + x.url + '</cite></div><div class=\'creative\'>' + x.description + '</div></li>';
                                        });
                                     Edit += '</ol>\';
                                     eval('Edit');
                                     }";

                ((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);


                IHTMLDocument2 doc = (IHTMLDocument2)this._webBrowser2.Document;
                IHTMLWindow2 parentWindow = doc.parentWindow;
                if (parentWindow != null)
                    parentWindow.execScript("test();", "javascript");

The c# code is fine, I'm just having trouble wrapping my head about injecting the javascript code with all the quotations, single quotes, etc to eliminate the javascript error. C#代码很好,我只是想把所有的引号,单引号等注入javascript代码以消除javascript错误的麻烦。 Any help is GREATLY appreciated! 任何帮助是极大的赞赏!

When using verbatim string literals prefixed with @, it means that the enclosed string is treated as literal. 当使用以@开头的逐字字符串文字时,表示将封闭的字符串视为文字。 So basically no backslash '\\' escaping. 因此,基本上没有反斜杠“ \\”转义。 To escape double quote ("), just double it (""). 要转义双引号(“),只需将其双引号(”“)。

string jsonStr = @"[ 
  {""name"": ""Obj1"", ""description"": ""Test description..."", ""url"":""http://www.test.com"" },
  { ""name"": ""Obj2"", ""description"": ""Testing..."", ""url"":""http://www.test.com"" },
  { ""name"": ""Obj3"", ""description"": ""Welp..."", ""url"":""http://www.test.com"" }
]";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM