在导航到链接URL之前如何对其进行修改

时间:2018-07-26 09:12:41

标签: python hyperlink pyqt5 qwebenginepage

我想在页面上单击一下,并在锚点链接中添加一些文本。

例如,如果用户单击具有href mysite.com/about的锚点,我想修改以下链接:mysite.com/about?myvar=value

我使用了QWebEngineUrlRequestInterceptor,但是未捕获到对链接的单击。

我做错了什么?

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
import os
import sys

class MyPage(QWebEnginePage):
    def __init__(self, parent=None):
        super(MyPage, self).__init__(parent)
    def triggerAction(self, action, checked=False):
        if action == QWebEnginePage.OpenLinkInNewWindow:
            self.createWindow(QWebEnginePage.WebBrowserWindow)

        return super(MyPage, self).triggerAction(action, checked)
    def acceptNavigationRequest(self, url, navtype, mainframe):
        if navtype == QWebEnginePage.NavigationTypeLinkClicked:
            # go somewhere else
            self.setUrl(QUrl('https://wikipedia.org'))
            # abort the current navigation
            return False
        return super(MyPage, self).acceptNavigationRequest(url, navtype, mainframe)

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.myPage = MyPage(self) #fab
        #self.setPage(self.myPage) #fab

        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl(WEBSITE_HOME))

        self.browser.loadFinished.connect(self.update_title)
        self.setCentralWidget(self.browser)

        navtb = QToolBar("Navigation")
        navtb.setIconSize(QSize(16, 16))
#        self.addToolBar(navtb)

        back_btn = QAction(QIcon(os.path.join('images', 'arrow-back.png')), "Back", self)
        back_btn.setStatusTip("Back to previous page")
        back_btn.triggered.connect(self.browser.back)
        navtb.addAction(back_btn)

        next_btn = QAction(QIcon(os.path.join('images', 'arrow-forward.png')), "Forward", self)
        next_btn.setStatusTip("Forward to next page")
        next_btn.triggered.connect(self.browser.forward)
        navtb.addAction(next_btn)

        reload_btn = QAction(QIcon(os.path.join('images', 'refresh.png')), "Reload", self)
        reload_btn.setStatusTip("Reload page")
        reload_btn.triggered.connect(self.browser.reload)
        navtb.addAction(reload_btn)

        home_btn = QAction(QIcon(os.path.join('images', 'home.png')), "Home", self)
        home_btn.setStatusTip("Go home")
        home_btn.triggered.connect(self.navigate_home)
        navtb.addAction(home_btn)

        stop_btn = QAction(QIcon(os.path.join('images', 'cross-circle.png')), "Stop", self)
        stop_btn.setStatusTip("Stop loading current page")
        stop_btn.triggered.connect(self.browser.stop)
        navtb.addAction(stop_btn)

        navtb.setContextMenuPolicy(Qt.PreventContextMenu)

        self.show()
        self.setWindowIcon(QIcon(os.path.join('images', 'ma-icon-64.png')))

    def update_title(self):
        title = self.browser.page().title()
        self.setWindowTitle("%s - Title" % title)

    def navigate_home(self):
        self.browser.setUrl(QUrl(WEBSITE_LOGIN))

    def createWindow(self, windowType):
        if windowType == QWebEnginePage.WebBrowserTab:
            self.webView = MyWindow()
            self.webView.setAttribute(Qt.WA_DeleteOnClose, True)
            self.webView.show()
            return self.webView
        return super(MyWindow, self).createWindow(windowType)

WEBSITE_HOME = "http://google.com"
WEBSITE_LOGIN = "http://google.com"

app = QApplication(sys.argv)
window = MainWindow()
app.exec_()

1 个答案:

答案 0 :(得分:0)

如果您只想在导航链接之前修改/替换URL,也许最简单的解决方案是重新实现acceptNavigationRequest

class MyPage(QWebEnginePage):    
    def acceptNavigationRequest(self, url, navtype, mainframe):
        if navtype == QWebEnginePage.NavigationTypeLinkClicked:
            # go somewhere else
            self.setUrl(QUrl('https://wikipedia.org'))
            # abort the current navigation
            return False
        return super(MyPage, self).acceptNavigationRequest(url, navtype, mainframe)