[PyQt] inject code to webpage using QWebEnginePage::runJavaScript()

Zhao Lee redstone-cold at 163.com
Sun Jun 16 16:44:53 BST 2019




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_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190616/01986810/attachment.html>


More information about the PyQt mailing list