1

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

例如,如果用户单击带有 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_()
4

1 回答 1

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)
于 2018-08-02T09:53:55.720 回答