1

ページのクリックをキャッチし、アンカー リンクにテキストを追加したいと考えています。

たとえば、ユーザーが hrefmysite.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