This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
py-gtk-notes
Archived
1
0
Fork 0
py-gtk-notes/notes/protocal.py

38 lines
1.1 KiB
Python
Raw Permalink Normal View History

2023-07-31 16:19:19 +08:00
import os
from gi.repository import Gio
from .mimetypes import get_mimetype
def file_path(filepath):
root = os.path.dirname(os.path.realpath(__file__))
return os.path.join(root, filepath)
def resource_request_callback(req):
schema = req.get_scheme()
pathname = req.get_path()
ext = pathname.split('.')[-1]
print('----------------------------------------')
print(req.get_uri(),schema, pathname, ext, get_mimetype(ext))
print('----------------------------------------')
if schema == 'app':
data = open(file_path('./webview' + pathname)).read()
data = Gio.MemoryInputStream.new_from_data(data.encode())
# ------- 更多功能的reponse ----------------
# res = WebKit2.URISchemeResponse.new(data, -1)
# res.set_content_type(get_mimetype(ext))
# res.set_http_headers('text/html')
# res.set_status(200)
# req.finish_with_response(res)
# ----------------------------------------
# 简单的response
req.finish(data, -1, get_mimetype(ext))
def register_schenes(webview):
context = webview.get_context()
context.register_uri_scheme('app', resource_request_callback)