33 lines
840 B
Python
33 lines
840 B
Python
|
#!/usr/bin/env python3
|
||
|
# @author yutent<yutent.io@gmail.com>
|
||
|
# @date 2023/08/08 15:00:27
|
||
|
|
||
|
|
||
|
import os, gi
|
||
|
|
||
|
gi.require_version("WebKit2", "4.1")
|
||
|
|
||
|
from gi.repository import WebKit2
|
||
|
|
||
|
|
||
|
class Inject:
|
||
|
def __init__(self, webview):
|
||
|
|
||
|
self.manager = webview.get_user_content_manager()
|
||
|
|
||
|
script = open(self.abspath('./inject.js'), 'r').read()
|
||
|
frame = WebKit2.UserContentInjectedFrames.ALL_FRAMES
|
||
|
time = WebKit2.UserScriptInjectionTime.END
|
||
|
script = WebKit2.UserScript(script, frame, time, None, None)
|
||
|
|
||
|
self.manager.add_script(script)
|
||
|
|
||
|
|
||
|
def connect(self, callback):
|
||
|
self.manager.connect('script-message-received::app', callback)
|
||
|
self.manager.register_script_message_handler('app')
|
||
|
|
||
|
|
||
|
def abspath(self, filepath):
|
||
|
root = os.path.dirname(os.path.realpath(__file__))
|
||
|
return os.path.join(root, filepath)
|