python3-webengine-gtk3/usr/lib/python3/dist-packages/webengine/gtk3/_inject.py

33 lines
856 B
Python

#!/usr/bin/env python3
# @author yutent<yutent.io@gmail.com>
# @date 2023/08/08 15:00:27
import os, gi, json
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_data = open(self.abspath('./inject.js'), 'r').read()
frame = WebKit2.UserContentInjectedFrames.ALL_FRAMES
time = WebKit2.UserScriptInjectionTime.END
script = WebKit2.UserScript(script_data, 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)
基于webkit2封装的webview库, 提供傻瓜式的定制, 和一系列的js方法的注入, 增加前端js直接与系统交互的能力
Python 68.1%
JavaScript 30.4%
Shell 1.5%