Javascript no more working for local pages?

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Javascript no more working for local pages?

Postby AndyB » Sun May 07, 2017 9:46 am

In version 2987 i get an exception if i execute javascript code with EvaluateScriptAsync on web pages created at runtime.
This worked fine in version 2883.

I get this error:
Unable to execute javascript at this time, scripts can only be executed within a V8Context.
Use the IWebBrowser.CanExecuteJavascriptInMainFrame property to guard against this exception.

I use this code to add the page, so i can later load the page by URL:
Code: Select all
                Dim factory As IResourceHandlerFactory = m_innerBrowser.ResourceHandlerFactory
                If TypeOf factory Is DefaultResourceHandlerFactory Then
                    Dim xfactory As DefaultResourceHandlerFactory = DirectCast(factory, DefaultResourceHandlerFactory)
                    Dim blnOK As Boolean = xfactory.RegisterHandler("http://localhost/selectsite.htm", ResourceHandler.FromString(strHmtl))
                End If


I use CefSharp, and sorry for VB.NET sample code.
AndyB
Techie
 
Posts: 39
Joined: Sun Apr 16, 2017 3:00 pm

Re: Javascript no more working for local pages?

Postby HarmlessDave » Sun May 07, 2017 2:17 pm

It might be a timing issue. Just to check, are you making sure to wait until after the document object exists before executing? At least until after the loading state has changed to loaded?
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Javascript no more working for local pages?

Postby amaitland » Sun May 07, 2017 4:13 pm

Does your page have any JavaScript on it? When are you expecting your script?

People are under the incorrect assumption that it's safe to execute JavaScript at any point, it can only be executed/evaluated when a V8Context exists or after a page without JavaScript has sufficiently loaded it's possible to execute a script and a context will be created. It's all too common for people to execute a script before the page has even begun loading and wonder why it doesn't work.

If you believe the exception is thrown in error then bypass the extension method, which is what your using and use the method directly on the frame. Look at the WebBrowserExtensions class to see an example in the source.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1291
Joined: Wed Jan 14, 2015 2:35 am

Re: Javascript no more working for local pages?

Postby amaitland » Sun May 07, 2017 4:22 pm

Also the exception your seeing is a CefSharp specific one, this forum is really only for CEF questions.

See https://github.com/cefsharp/CefSharp#contact

A similar question has already been asked at http://stackoverflow.com/questions/4374 ... rs-constru
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1291
Joined: Wed Jan 14, 2015 2:35 am

Re: Javascript no more working for local pages?

Postby AndyB » Mon May 08, 2017 10:21 am

Sorry, i should have checked at 1st if it's no CefSharp specific issue.

I execute the script after the page has fully loaded.
Bypassing the extension method works.
If i call the extension method then it works if i add a dummy javascript to the page.

I noticed one odd thing with local sites. I hoped adding the dummy javascript may solve this, but thats not the case.
On "normal" websites it works fine to call click(); on links. If i do this in my runtime created html pages then the app freezes.
As workaround i call window.location.href = myLinkObj.href instead of myLinkObj.click()
AndyB
Techie
 
Posts: 39
Joined: Sun Apr 16, 2017 3:00 pm

Re: Javascript no more working for local pages?

Postby vaibhav » Thu Jul 13, 2017 9:05 am

Hello,
I am also facing this issue. Will you explain me in brief how you have solved this issue?

We have a WPF application, in that we have used cef chromium web browser but web page is not loading properly.
vaibhav
Newbie
 
Posts: 4
Joined: Thu Jul 13, 2017 8:51 am

Re: Javascript no more working for local pages?

Postby AndyB » Fri Jul 14, 2017 1:44 pm

I have no fix, only a workaround.

Either bypass the extension method by calling
myWpfBrowserControl.GetBrowser().MainFrame.EvaluateScriptAsync(...)
or add a dummy script to the local pages:
.......
<head>
<script>
function myDummyFunction() {};
</script>
</head>
.......

for the link issue, call
window.location.href = myLinkObj.href;
instead of
myLinkObj.click();
AndyB
Techie
 
Posts: 39
Joined: Sun Apr 16, 2017 3:00 pm

Re: Javascript no more working for local pages?

Postby vaibhav » Mon Jul 17, 2017 8:34 am

Thank you Andy for your replay.

I have tried the solution given by you but it is not working for me.

Actually when I run the WPF app from visual studio 2013 then it works fine but when I try from bin/app.exe
then it loads/render partial website.

I have downloaded the nuget package for CefSharp.Wpf.

What is the reason for this behavior. Please suggest me solution.
vaibhav
Newbie
 
Posts: 4
Joined: Thu Jul 13, 2017 8:51 am

Re: Javascript no more working for local pages?

Postby AndyB » Tue Jul 18, 2017 6:33 am

I do the the web site automation (execute Javascript) in the LoadingStateChanged event.

First thing i do in this event is to update the UI to show if the browser is loading and enable/disable Back/Forwards buttons:
Code: Select all
       Me.InvokeOnUiThreadAsync(Sub() SetIsLoading(Not blnCanReload))


Actually calling myLinkObj.click(); works for me, maybe because i always perform now InvokeAsync if the current thread is not the UI thread:

Code: Select all
    Public Sub InvokeOnUiThreadAsync(action As Action)
        Try
            If Me.Dispatcher.CheckAccess() Then
                action.Invoke()
            Else
                If Not Me.Dispatcher.HasShutdownStarted Then
                    Me.Dispatcher.InvokeAsync(action, System.Windows.Threading.DispatcherPriority.Background)
                End If
            End If
        Catch ex As Exception
            LogManager.LogMessage(LogType.Errors, ex)
        End Try
    End Sub


I don't execute any Javascript if the page is still loading:
Code: Select all
            If args.IsLoading Then
                Exit Sub
            End If


Next thing, i don't execute Javascript directly in this Event.
I start a timer which executes the script with 200 milliseconds delay (some sites need a bit more)... to give the browser some time to execute all the Javascript onload code (if any), and to not block the UI.

Don't know if that helps.
I guess your issue is CefSharp specific, so this forum may not be the right place. Please check the CefSharp site for contact options if you need further help.
AndyB
Techie
 
Posts: 39
Joined: Sun Apr 16, 2017 3:00 pm


Return to Support Forum

Who is online

Users browsing this forum: Majestic-12 [Bot] and 49 guests