omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Eval_js

    Pythonista
    4
    10
    4123
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • robertiii
      robertiii last edited by

      For some reasons the code does not run.

      newView = ui.WebView()
      			newView.name = 'song'
      			newView.frame = v['view1'].bounds
      			newView.content_mode = ui.CONTENT_SCALE_ASPECT_FIT
      			newView.load_url(displayItems[row]['itemURL'])
      			newView.eval_js("setSong('<h3>Title of the Song</h3></br><p>Joy to the world, the Lord is come.</p>')")
      			v['view1'].add_subview(newView)
      			v.song = newView
      

      And here is the Javascript.

      function setSong(element){
      	var div = document.getElementById("lyrics");
      	div.innerHTML = element;
      }
      
      1 Reply Last reply Reply Quote 0
      • robertiii
        robertiii last edited by

        If i call the function later with a button it works but not here...

        1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

          load_url does not wait for the page to load.. you need to wait until the doc is loaded/ready.

          1 Reply Last reply Reply Quote 0
          • robertiii
            robertiii last edited by

            How would I do that? I wondered if that was the case

            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by JonB

              One way would be to poll (sleep, check loop) document.ready via eval_js, though if you check it too early it might show ready before it starts the load,
              You could also sleep for a few seconds, and hope that is enough to handle varying network cinditions. The most robust way is to set webview_did_finish_load to set a threading.event.
              https://forum.omz-software.com/topic/2380/returning-execution-to-main-thread-with-ui-webview/3.
              for you, it would work a little differentlt, as you probably just want to wait to set the title, but don't necessarily need to stop execution. You could, to ensure that you set the song before presenting, but I guess it all depends on what you are doing.

              w=ui.Webview()
              w.ready_event=threading.Event() 
              w.webview_did_finish_load=set_title
              w.load_url(....)
              def set_song(sender):
                 self.webview_did_finish_load = None # maybe needed to prevent the evaljs from triggering this again
                 w.eval_js('setSong(...)..')
                 self.ready_event.set() #optional
                 
              w.ready_event.wait() #optional
              
              mikael 1 Reply Last reply Reply Quote 0
              • robertiii
                robertiii last edited by

                I have tried emplementing this and cannot get it to work.

                1 Reply Last reply Reply Quote 0
                • robertiii
                  robertiii last edited by

                  The file is actually a local file. Is there a way to edit the html file before adding it. Basically making the file a template

                  1 Reply Last reply Reply Quote 0
                  • robertiii
                    robertiii last edited by

                    I just used SimpleTemplate

                    1 Reply Last reply Reply Quote 0
                    • mikael
                      mikael @JonB last edited by

                      You don’t need threading for this. Instead:

                      <body onload="window.location.href='http://loaded/';">
                      

                      Then, in the webview_should_start_load method of the WebView’s delegate, check for the url and run any post-load evals.

                      if url.startswith('http://loaded/'):
                        # Do something
                        return False
                      return True
                      
                      1 Reply Last reply Reply Quote 0
                      • enceladus
                        enceladus last edited by

                        Using jquery could be one possible solution. Here is an example using jquery.

                        https://gist.github.com/619e923059d7643841e6f889655d8bf1

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors