inject code to webpage using QWebEnginePage::runJavaScript()

为君一笑 提交于 2020-01-17 01:33:10

问题


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 *


class WebEngineView(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_())

来源:https://stackoverflow.com/questions/56619358/inject-code-to-webpage-using-qwebenginepagerunjavascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!