[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyQt] inject code to webpage using QWebEnginePage::runJavaScript()
From:       "Zhao Lee" <redstone-cold () 163 ! com>
Date:       2019-06-16 15:44:53
Message-ID: 193942b8.76f7.16b60f6755a.Coremail.redstone-cold () 163 ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]

[Attachment #4 (text/plain)]




I also posted the question here.

When QWebEngineView starts loading a web page , I want to inject a web page progress \
bar to it, my code as the following , but I cannot see the progress bar during \
loading , so what's wrong ?

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWebEngineCore import *
from PyQt5.QtCore import *


classWebEngineView(QWebEngineView):def__init__(self, parent=None):
        super().__init__(parent)

        self.webPage = self.page()
        self.webPage.runJavaScript('''
                                    var script = document.createElement('script');
                                    var link = document.createElement('link');
                                    script.type = 'text/javascript';
                                    script.src = 'static/pace/pace.min.js';
                                    link.rel = 'stylesheet';
                                    link.href = \
'static/pace/pace-theme-barber-shop.css';  document.head.appendChild(script);
                                    document.head.appendChild(link);
                             ''')
        self.webPage.load(QUrl('https://doc.qt.io/qt-5/qwebenginepage.html'))


if __name__ == "__main__":

    app = QApplication(sys.argv)
    app.setAttribute(Qt.AA_UseSoftwareOpenGL)
    webEngineView = WebEngineView()
    webEngineView.show()
    sys.exit(app.exec_())


[Attachment #5 (text/html)]

<div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><!--StartFragment--><div><br></div><p \
style="box-sizing: border-box; margin: 0px 0px 10px; color: rgb(51, 51, 51); \
font-family: &quot;Titillium Web&quot;; font-size: 14px; font-style: normal; \
font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; \
letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; \
text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; \
-webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); \
text-decoration-style: initial; text-decoration-color: initial;"><em \
style="box-sizing: border-box;"><span>I&nbsp;</span>also posted the \
question<span>&nbsp;</span><a \
href="https://stackoverflow.com/questions/56619358/inject-code-to-webpage-using-qwebenginepagerunjavascript" \
rel="nofollow" style="box-sizing: border-box; background-color: transparent; color: \
rgb(51, 122, 183); text-decoration: none; -webkit-tap-highlight-color: \
transparent;">here</a>.</em></p><p style="box-sizing: border-box; margin: 0px 0px \
10px; color: rgb(51, 51, 51); font-family: &quot;Titillium Web&quot;; font-size: \
14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; \
font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: \
0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; \
-webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); \
text-decoration-style: initial; text-decoration-color: initial;">When QWebEngineView \
starts loading a web page , I want to inject<span>&nbsp;</span><a \
href="https://github.com/HubSpot/pace#example" rel="nofollow" style="box-sizing: \
border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration: \
none; -webkit-tap-highlight-color: transparent;">a web page progress \
bar</a><span>&nbsp;</span>to it, my code as the following , but I cannot see the \
progress bar during loading , so what's wrong ?</p><pre class="markdown-highlight" \
style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, \
&quot;Courier New&quot;, monospace; font-size: 13px; display: block; padding: 9.5px; \
margin: 1rem 0px 10px; line-height: 1.42857; word-break: break-all; overflow-wrap: \
break-word; color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); border: \
none; border-radius: 0px; max-height: 350px; font-style: normal; \
font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; \
letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; \
text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; \
text-decoration-style: initial; text-decoration-color: initial;"><code class="hljs \
python" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, \
&quot;Courier New&quot;, monospace; font-size: inherit; padding: 0.5em; color: black; \
background: white; border-radius: 0px; white-space: pre; display: inline-block; \
overflow-x: auto; overflow-wrap: normal; min-width: 100%;"><span class="hljs-keyword" \
style="box-sizing: border-box; color: rgb(0, 0, 136);">import</span> sys

<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(0, 0, \
136);">from</span> PyQt5.QtWidgets <span class="hljs-keyword" style="box-sizing: \
border-box; color: rgb(0, 0, 136);">import</span> * <span class="hljs-keyword" \
style="box-sizing: border-box; color: rgb(0, 0, 136);">from</span> \
PyQt5.QtWebEngineWidgets <span class="hljs-keyword" style="box-sizing: border-box; \
color: rgb(0, 0, 136);">import</span> * <span class="hljs-keyword" style="box-sizing: \
border-box; color: rgb(0, 0, 136);">from</span> PyQt5.QtWebEngineCore <span \
class="hljs-keyword" style="box-sizing: border-box; color: rgb(0, 0, \
136);">import</span> * <span class="hljs-keyword" style="box-sizing: border-box; \
color: rgb(0, 0, 136);">from</span> PyQt5.QtCore <span class="hljs-keyword" \
style="box-sizing: border-box; color: rgb(0, 0, 136);">import</span> *


<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" \
style="box-sizing: border-box; color: rgb(0, 0, 136);">class</span> <span \
class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, \
102);">WebEngineView</span><span class="hljs-params" style="box-sizing: border-box; \
color: rgb(102, 0, 102);">(QWebEngineView)</span>:</span>  <span \
class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" \
style="box-sizing: border-box; color: rgb(0, 0, 136);">def</span> <span \
class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, \
102);">__init__</span><span class="hljs-params" style="box-sizing: border-box; color: \
rgb(102, 0, 102);">(self, parent=None)</span>:</span>  super().__init__(parent)

        self.webPage = self.page()
        self.webPage.runJavaScript(<span class="hljs-string" style="box-sizing: \
                border-box; color: rgb(0, 136, 0);">'''
                                    var script = document.createElement('script');
                                    var link = document.createElement('link');
                                    script.type = 'text/javascript';
                                    script.src = 'static/pace/pace.min.js';
                                    link.rel = 'stylesheet';
                                    link.href = \
'static/pace/pace-theme-barber-shop.css';  document.head.appendChild(script);
                                    document.head.appendChild(link);
                             '''</span>)
        self.webPage.load(QUrl(<span class="hljs-string" style="box-sizing: \
border-box; color: rgb(0, 136, \
0);">'https://doc.qt.io/qt-5/qwebenginepage.html'</span>))


<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(0, 0, \
136);">if</span> __name__ == <span class="hljs-string" style="box-sizing: border-box; \
color: rgb(0, 136, 0);">"__main__"</span>:

    app = QApplication(sys.argv)
    app.setAttribute(Qt.AA_UseSoftwareOpenGL)
    webEngineView = WebEngineView()
    webEngineView.show()
    sys.exit(app.exec_())</code></pre><!--EndFragment--></div><br><br><span \
title="neteasefooter"><p>&nbsp;</p></span>


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic