-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Insert & Execute Javascript code in HTML page? #19
Comments
Potentially, the following file will cover your desired use-case: |
Unfortunately no. |
I mean, how would you do in pure JS? You would
Does none of these methods work for you? |
I would
|
If the example looks like above (where you know the script's content) I would highly recommend using If you need to load the script from some external source then the way you describe is certainly the best one. Here AngleSharp does all the steps for you. So yes, that sounds good to me. One thing to be aware is the asynchronous execution. You probably won't have access to the result right away. This is something that will definitely improve in the near future, but right now its not optimal. |
I do this var jsGetTitle = javascriptService.Engine.GetJint(ashDocument).Execute("document.Title;").GetCompletionValue().AsString(); But I got exception |
Is |
I'm confused. OK for javascriptService.Engine.GetJint(ashDocument).Execute("if (typeof window === 'undefined'){console.log('object: window is not available...');}"); I got |
The thing is that you execute that stuff directly from Jint, without providing the right execution layer. The way JavaScript engines are built there are is a stack of so called contexts. They all provide a base context, which contains elementary JavaScript objects, such as AngleSharp provides this window for you. Therefore I can recommend two things:
This should be made more convenient I guess. Perhaps some more methods may be useful here. |
some progress :) javascriptService.Engine.Evaluate("document.title;", new ScriptOptions() { Context = ashDocument.DefaultView, Document = ashDocument }); But javascriptService.Engine.Evaluate("document.title;", new ScriptOptions() { Context = ashDocument.DefaultView, Document = ashDocument }).AsString(); |
POC In EngineInstance.cs, modiy public Jint.Native.JsValue RunScript(String source)
{
_engine.EnterExecutionContext(Lexicals, Variables, _window);
Jint.Native.JsValue jsResult = _engine.Execute(source).GetCompletionValue();
_engine.LeaveExecutionContext();
return jsResult;
} In JavaScriptEngine.cs, Add public Jint.Native.JsValue Execute(String source, ScriptOptions options)
{
var objectContext = options.Context;
var instance = default(EngineInstance);
if (_contexts.TryGetValue(objectContext, out instance) == false)
_contexts.Add(objectContext, instance = new EngineInstance(objectContext, _external));
return instance.RunScript(source);
} Test string jsGetTitle = javascriptService.Engine.Execute("document.title;", new ScriptOptions() { Context = ashDocument.DefaultView, Document = ashDocument }).AsString(); |
Nope, this won't happen. Jint will always let you access the last return value on the stack. Therefore, there is no need to make such changes. Instead, an extension method is more useful. The coupling to Jint should be as minimal as possible. Right now its already on the edge. |
Hi,
Is it possible to pass Javascript code back and execute JavaScript function/method from C#?
For example :
The text was updated successfully, but these errors were encountered: