mirror of
https://github.com/allankoechke/pywebview.git
synced 2026-11-30 10:58:50 +00:00
* Update change_url.md * Update api.md * Update installation.md * Add Google Analytics * Fix installation * 2.4 info * [Docs] add information about window.pywebview availability * Update debuggig info about QTWebEngine * [Docs] Add information about app architecture * Update cef.md * [CEF] Fix JS logic not persisting after navigating to another page * #169 Frameless window (#313) * Merge PR #177: [Cocoa] Implement frameless windows * [All] Add skeleton for `frameless` parameter * [Cocoa] Add `frameless` kwarg to remove the window frame. - Implement a DragBar class to enable dragging the webview - Make the drag bar stick to the top and autoresize to the width of the view. * [WinForms] Implement frameless window + easy drag * Merge PR #177: [Cocoa] Implement frameless windows * [All] Add skeleton for `frameless` parameter * [Cocoa] Add `frameless` kwarg to remove the window frame. - Implement a DragBar class to enable dragging the webview - Make the drag bar stick to the top and autoresize to the width of the view. * [WinForms] Implement frameless window + easy drag * [GTK] partial implementation of frameless window * [QT] Partial implementation of frameless window * [QT] implement easy drag * [Cocoa] Hide standard buttons and make the entire window draggable * [GTK] implement easy move * Add frameless tests * Remove unused parameter * [Cocoa] Implement free drag * Revert WebBrowserInterops * Code cleanup * Update interop DLLs * Fix interop dll * Update docs; Fix tests * Add example * Update examples * [CEF]: Implement support for pyinstaller one-file mode * [CEF] Fix typo * [All] Change api_call timeout to 15 seconds * Update setup.py * [QT] Simplify QT installation * New API Cocoa implementation Revamped tests and examples * Fix web engine force setting * Rename destroy_window to destroy * Update documentation * Add documentation for 2.4 * Refactor project structure * [GTK] New API support * Rename confirm_quit to confirm_close Split lifecycle events to shown and loaded * [GTK] Implement new API Fix tests * [QT] New API implementation * Remove multiprocessing * [Windows/CEF] New API implementation * Implement CSRF token [All] Implement get_element * Update Flask app example * Rename get_element to get_elements. Bug fixes. * [GTK] Implement propert JS-Python bridge for WebKit2 >2.2 * Update documentation * [Windows/CEF] New API implementation * Add token test * [Winforms] Fix js api initialization * [Cocoa] Fix events * Debug: print webkit version * [GTK] Check for old webkit * [GTK] Fix event issues * Catch notimlementederror in tests * Update travis conf with qt from pip * Update .travis.yml * Update .travis.yml * Update .travis.yml * Update .travis.yml * Update .travis.yml * [Windows] Preliminary implementation of EdgeHTML based on PR from @HeavenVolkoff * [docs] Add information about EdgeHTML * EdgeHTML WIP * Remove windows.web import * EdgeHTML improvements. Fix MSHTML evaluate_js * [Docs] Add information about renderer limitations * [EdgeHTML] Fix bugs and tests * Remove window from windows list after it is destroyed * Fix window object deletion after window is closed via GUI * [Docs] Add information about renderer codes * Refactor and fix JS API Remove multiprocessing example [MSHTML] Add console.log debug support * [Travis] Change conf * [WinForms] Fix fullscreen * Fix dll installation * [Windows] Fix DLL bundling * Update changelog * [CEF]: Fix JS API * Update changelog * Built-in HTTP server implementation * Add http server test * Add http server testcd * Add http server testcd * [EdgeHTML] allow localhost urls * Add video example * Bug fixing * [Cocoa] Remove media prefs * Update .travis.yml * Update .travis.yml * Update .travis.yml * [Travis] Disable QT 5.12 builds * Update html_load.md
188 lines
5.1 KiB
Python
188 lines
5.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
(C) 2014-2019 Roman Sirokov and contributors
|
|
Licensed under BSD license
|
|
|
|
http://github.com/r0x0r/pywebview/
|
|
"""
|
|
|
|
import json
|
|
import logging
|
|
import os
|
|
import re
|
|
import sys
|
|
from platform import architecture
|
|
from threading import Thread
|
|
from uuid import uuid4
|
|
|
|
from .js import api, npo, dom
|
|
|
|
_token = uuid4().hex
|
|
|
|
default_html = '<!doctype html><html><head></head><body></body></html>'
|
|
|
|
logger = logging.getLogger('pywebview')
|
|
|
|
|
|
class WebViewException(Exception):
|
|
pass
|
|
|
|
|
|
def base_uri(relative_path=''):
|
|
""" Get absolute path to resource, works for dev and for PyInstaller """
|
|
try:
|
|
# PyInstaller creates a temp folder and stores path in _MEIPASS
|
|
base_path = sys._MEIPASS
|
|
except Exception:
|
|
if 'pytest' in sys.modules:
|
|
for arg in reversed(sys.argv):
|
|
path = os.path.realpath(arg.split('::')[0])
|
|
if os.path.exists(path):
|
|
base_path = path if os.path.isdir(path) else os.path.dirname(path)
|
|
break
|
|
else:
|
|
base_path = os.path.dirname(os.path.realpath(sys.argv[0]))
|
|
|
|
if not os.path.exists(base_path):
|
|
raise ValueError('Path %s does not exist' % base_path)
|
|
|
|
return 'file://%s' % os.path.join(base_path, relative_path)
|
|
|
|
|
|
def convert_string(string):
|
|
if sys.version < '3':
|
|
return unicode(string)
|
|
else:
|
|
return str(string)
|
|
|
|
|
|
def parse_file_type(file_type):
|
|
'''
|
|
:param file_type: file type string 'description (*.file_extension1;*.file_extension2)' as required by file filter in create_file_dialog
|
|
:return: (description, file extensions) tuple
|
|
'''
|
|
valid_file_filter = r'^([\w ]+)\((\*(?:\.(?:\w+|\*))*(?:;\*\.\w+)*)\)$'
|
|
match = re.search(valid_file_filter, file_type)
|
|
|
|
if match:
|
|
return match.group(1).rstrip(), match.group(2)
|
|
else:
|
|
raise ValueError('{0} is not a valid file filter'.format(file_type))
|
|
|
|
|
|
def parse_api_js(api_instance, platform):
|
|
def generate_func():
|
|
if api_instance:
|
|
return [str(f) for f in dir(api_instance) if callable(getattr(api_instance, f)) and str(f)[0] != '_']
|
|
else:
|
|
return []
|
|
|
|
func_list = generate_func()
|
|
js_code = npo.src + api.src % (_token, platform, func_list) + dom.src
|
|
return js_code
|
|
|
|
|
|
def js_bridge_call(window, func_name, param):
|
|
def _call():
|
|
result = json.dumps(func(func_params))
|
|
code = 'window.pywebview._returnValues["{0}"] = {{ isSet: true, value: {1}}}'.format(func_name, escape_line_breaks(result))
|
|
window.evaluate_js(code)
|
|
|
|
func = getattr(window.js_api, func_name, None)
|
|
|
|
if func is not None:
|
|
try:
|
|
func_params = param if not param else json.loads(param)
|
|
t = Thread(target=_call)
|
|
t.start()
|
|
except Exception:
|
|
logger.exception('Error occurred while evaluating function {0}'.format(func_name))
|
|
else:
|
|
logger.error('Function {}() does not exist'.format(func_name))
|
|
|
|
|
|
def escape_string(string):
|
|
return string\
|
|
.replace('\\', '\\\\') \
|
|
.replace('"', r'\"') \
|
|
.replace('\n', r'\n')\
|
|
.replace('\r', r'\r')
|
|
|
|
|
|
def transform_url(url):
|
|
if url and '://' not in url:
|
|
return base_uri(url)
|
|
else:
|
|
return url
|
|
|
|
|
|
def make_unicode(string):
|
|
"""
|
|
Python 2 and 3 compatibility function that converts a string to Unicode. In case of Unicode, the string is returned
|
|
unchanged
|
|
:param string: input string
|
|
:return: Unicode string
|
|
"""
|
|
if sys.version < '3' and isinstance(string, str):
|
|
return unicode(string.decode('utf-8'))
|
|
|
|
return string
|
|
|
|
|
|
def escape_line_breaks(string):
|
|
return string.replace('\\n', '\\\\n').replace('\\r', '\\\\r')
|
|
|
|
|
|
def inject_base_uri(content, base_uri):
|
|
pattern = '<%s(?:[\s]+[^>]*|)>'
|
|
base_tag = '<base href="%s">' % base_uri
|
|
|
|
match = re.search(pattern % 'base', content)
|
|
|
|
if match:
|
|
return content
|
|
|
|
match = re.search(pattern % 'head', content)
|
|
if match:
|
|
tag = match.group()
|
|
return content.replace(tag, tag + base_tag)
|
|
|
|
match = re.search(pattern % 'html', content)
|
|
if match:
|
|
tag = match.group()
|
|
return content.replace(tag, tag + base_tag)
|
|
|
|
match = re.search(pattern % 'body', content)
|
|
if match:
|
|
tag = match.group()
|
|
return content.replace(tag, base_tag + tag)
|
|
|
|
return base_tag + content
|
|
|
|
|
|
def interop_dll_path(dll_name):
|
|
if dll_name == 'WebBrowserInterop.dll':
|
|
dll_name = 'WebBrowserInterop.x64.dll' if architecture()[0] == '64bit' else 'WebBrowserInterop.x86.dll'
|
|
|
|
# Unfrozen path
|
|
dll_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib', dll_name)
|
|
if os.path.exists(dll_path):
|
|
return dll_path
|
|
|
|
# Frozen path, dll in the same dir as the executable
|
|
dll_path = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), dll_name)
|
|
if os.path.exists(dll_path):
|
|
return dll_path
|
|
|
|
try:
|
|
# Frozen path packed as onefile
|
|
dll_path = os.path.join(sys._MEIPASS, dll_name)
|
|
if os.path.exists(dll_path):
|
|
return dll_path
|
|
except Exception:
|
|
pass
|
|
|
|
raise Exception('Cannot find %s' % dll_name)
|
|
|