Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWebEnginePage: Solutions for running JavaScript and returning values from HTML document?

QWebEnginePage: Solutions for running JavaScript and returning values from HTML document?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 503 Views
  • 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.
  • D Offline
    D Offline
    desun_evan
    wrote on 6 Nov 2023, 01:38 last edited by
    #1

    I have been using QWebEnginePage::runJavaScript();
    with this being executed after loading a web page with QWebEnginePage::setUrl(); and calling a slot after QWebEnginPage::loadFinished();

    I am running into a few issues.

    • 1: runJavaScript returns values that change after being quickly called a second time. Usually this amounts to more elements found by querySelectorAll();..eventually the actual amount of elements the HTML page contains are returned... but sometimes this isn't true.

    • 2: Certain pages do not return large groups of elements that querySelectorAll(); would normally return.

    Is there a more sophisticated way to run and manage JavaScripts with QWebEnginePage? Or a better way altogether? Is this a injection point or worldid issue?

    I am aware of QWebEngineScript class, although I'm not sure how to use this to execute a JavaScript. I have been introduced to Greasemonkey but don't know where to begin with chromium.

    E J 3 Replies Last reply 6 Nov 2023, 06:52
    1
    • D desun_evan
      6 Nov 2023, 01:38

      I have been using QWebEnginePage::runJavaScript();
      with this being executed after loading a web page with QWebEnginePage::setUrl(); and calling a slot after QWebEnginPage::loadFinished();

      I am running into a few issues.

      • 1: runJavaScript returns values that change after being quickly called a second time. Usually this amounts to more elements found by querySelectorAll();..eventually the actual amount of elements the HTML page contains are returned... but sometimes this isn't true.

      • 2: Certain pages do not return large groups of elements that querySelectorAll(); would normally return.

      Is there a more sophisticated way to run and manage JavaScripts with QWebEnginePage? Or a better way altogether? Is this a injection point or worldid issue?

      I am aware of QWebEngineScript class, although I'm not sure how to use this to execute a JavaScript. I have been introduced to Greasemonkey but don't know where to begin with chromium.

      E Offline
      E Offline
      enjoysmath
      wrote on 6 Nov 2023, 06:52 last edited by enjoysmath 11 Jun 2023, 06:55
      #2

      @desun_evan

      This seems like you want to use the QWebChannel api.

      I did that one time following their API docs, and here is the result:

      https://github.com/enjoysmath/Marked-KaTeX-example-cpp

      It's a Markdown editor + KaTeX (for math type-setting) using a QWebEngineWidget and QWebChannel to pass data between. The host language is C++.

      https://github.com/enjoysmath
      https://math.stackexchange.com/users/26327/exercisingmathematician

      1 Reply Last reply
      1
      • D desun_evan
        6 Nov 2023, 01:38

        I have been using QWebEnginePage::runJavaScript();
        with this being executed after loading a web page with QWebEnginePage::setUrl(); and calling a slot after QWebEnginPage::loadFinished();

        I am running into a few issues.

        • 1: runJavaScript returns values that change after being quickly called a second time. Usually this amounts to more elements found by querySelectorAll();..eventually the actual amount of elements the HTML page contains are returned... but sometimes this isn't true.

        • 2: Certain pages do not return large groups of elements that querySelectorAll(); would normally return.

        Is there a more sophisticated way to run and manage JavaScripts with QWebEnginePage? Or a better way altogether? Is this a injection point or worldid issue?

        I am aware of QWebEngineScript class, although I'm not sure how to use this to execute a JavaScript. I have been introduced to Greasemonkey but don't know where to begin with chromium.

        E Offline
        E Offline
        enjoysmath
        wrote on 12 Nov 2023, 19:54 last edited by
        #3

        @desun_evan Did you get solutions to this problem?

        https://github.com/enjoysmath
        https://math.stackexchange.com/users/26327/exercisingmathematician

        1 Reply Last reply
        0
        • D desun_evan
          6 Nov 2023, 01:38

          I have been using QWebEnginePage::runJavaScript();
          with this being executed after loading a web page with QWebEnginePage::setUrl(); and calling a slot after QWebEnginPage::loadFinished();

          I am running into a few issues.

          • 1: runJavaScript returns values that change after being quickly called a second time. Usually this amounts to more elements found by querySelectorAll();..eventually the actual amount of elements the HTML page contains are returned... but sometimes this isn't true.

          • 2: Certain pages do not return large groups of elements that querySelectorAll(); would normally return.

          Is there a more sophisticated way to run and manage JavaScripts with QWebEnginePage? Or a better way altogether? Is this a injection point or worldid issue?

          I am aware of QWebEngineScript class, although I'm not sure how to use this to execute a JavaScript. I have been introduced to Greasemonkey but don't know where to begin with chromium.

          J Offline
          J Offline
          JonB
          wrote on 12 Nov 2023, 21:27 last edited by JonB 11 Dec 2023, 21:29
          #4

          @desun_evan
          @enjoysmath's QWebChannel may well be the correct way to go.

          I have a recollection of using QWebEnginePage::runJavaScript() and QWebEnginPage::loadFinished() which just might be relevant.

          I think page load finished comes before JS document ready event. I used runJavaScipt() to set document.onReady (or whatever it is, or jQuery ready()) to execute code which needed to be sure the document tree had been fully constructed. Worth a try for your case?

          1 Reply Last reply
          1
          • D Offline
            D Offline
            desun_evan
            wrote on 12 Dec 2023, 23:48 last edited by
            #5

            I will try these suggestions. Thank you very much!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved