Javascript injection - WPF CefSharp 57.0.0

123 views
Skip to first unread message

Michael Muehr

unread,
Sep 26, 2017, 4:45:38 AM9/26/17
to CefSharp
Hi all,

I have a simple WebPage (like: http://www.bbzsl.eu/bbzse/Vertretungsplaene/intern/klassemorgen/subst_001.htm ) and I want to inject some JavaScript code to manipulate the DOM. I tried to execute a simple alert, but either I get an error "Unable to execute javascript at this time.." or the script is not executed at all. Where can I put the ExecuteScriptAsync function to inject my JavaScript ?


private void InitWebControl()

 
{

 
WebControl.BrowserSettings = new BrowserSettings

 
{

 
ApplicationCache = CefState.Disabled,

 
};

 

 
//_webControl.LoadError += _webControl_LoadError;

 
//_webControl.ConsoleMessage += _webControl_ConsoleMessage;

 
//_webControl.FrameLoadStart += _webControl_FrameLoadStart;

 
WebControl.FrameLoadStart += (sender, args) =>

 
{

 
if (WebControl.CanExecuteJavascriptInMainFrame)

 
{

 
WebControl.ExecuteScriptAsync(

 
"var node = document.createElement('style'); node.innerHTML = 'html, body, iframe {overflow:hidden;}'; document.body.appendChild(node);");

 
}

 
};

 
WebControl.IsBrowserInitializedChanged += (sender, args) =>

 
{

 
};

 
WebControl.LoadingStateChanged += (sender, args) =>

 
{

 
if (WebControl.CanExecuteJavascriptInMainFrame)

 
{

 
WebControl.ExecuteScriptAsync("alert('All Resources Have Loaded');");

 
}

 
//Wait for the Page to finish loading

 
if (args.IsLoading == false)

 
{

 
//error

 
WebControl.ExecuteScriptAsync("alert('All Resources Have Loaded');");

 
}

 
};
 
 
WebControl.FrameLoadEnd += (sender, args) =>

 
{

 
//not fired

 
if (WebControl.CanExecuteJavascriptInMainFrame)

 
{

 
WebControl.ExecuteScriptAsync("alert('All Resources Have Loaded');");

 
}

 
WebControl.ExecuteScriptAsync("alert('All Resources Have Loaded');");

 
};



Reply all
Reply to author
Forward
0 new messages