[Servers] Add webkitgtk-6.0 support

Continues !195. WebKitGTK 2.40 series (including 2.39.x development
releases and 2.40.0 stable release) introduced API 6.0 and dropped
support for API 5.0.

Since WebKitGTK 2.40 finally got a stable release for the new API, I
guess it's time to support it. Note that this MR may looks different
with !195, it's because that MR was created using the API from an
unstable 2.39.x release.

Related discussion in Nixpkgs: https://github.com/NixOS/nixpkgs/pull/220440#issuecomment-1484040684
This commit is contained in:
Chuang Zhu 2023-03-27 00:22:43 +08:00
parent e3d9c4de15
commit c9a09817ac
2 changed files with 31 additions and 17 deletions

View File

@ -8,16 +8,21 @@ import platform
gi.require_version('Adw', '1')
gi.require_version('Gtk', '4.0')
gi.require_version('WebKit2', '5.0')
gi.require_version('Soup', '3.0')
try:
gi.require_version('WebKit', '6.0')
from gi.repository import WebKit
except ValueError:
gi.require_version('WebKit2', '5.0')
from gi.repository import WebKit2 as WebKit
from gi.repository import Adw
from gi.repository import Gdk
from gi.repository import GLib
from gi.repository import Gsk
from gi.repository import Gtk
from gi.repository import Soup
from gi.repository import WebKit2
from komikku.models.database import VERSION as DB_VERSION
from komikku.utils import check_cmdline_tool
@ -120,7 +125,7 @@ class DebugInfo:
info += f'- GLib: {GLib.MAJOR_VERSION}.{GLib.MINOR_VERSION}.{GLib.MICRO_VERSION}\n'
info += f'- GTK: {Gtk.MAJOR_VERSION}.{Gtk.MINOR_VERSION}.{Gtk.MICRO_VERSION}\n'
info += f'- Awaita: {Adw.MAJOR_VERSION}.{Adw.MINOR_VERSION}.{Adw.MICRO_VERSION}\n'
info += f'- WebKitGTK: {WebKit2.MAJOR_VERSION}.{WebKit2.MINOR_VERSION}.{WebKit2.MICRO_VERSION}\n'
info += f'- WebKitGTK: {WebKit.MAJOR_VERSION}.{WebKit.MINOR_VERSION}.{WebKit.MICRO_VERSION}\n'
info += f'- Soup: {Soup.MAJOR_VERSION}.{Soup.MINOR_VERSION}.{Soup.MICRO_VERSION}\n'
info += '\n'
@ -128,7 +133,7 @@ class DebugInfo:
info += f'- GLib: {GLib.glib_version[0]}.{GLib.glib_version[1]}.{GLib.glib_version[2]}\n'
info += f'- GTK: {Gtk.get_major_version()}.{Gtk.get_minor_version()}.{Gtk.get_micro_version()}\n'
info += f'- Awaita: {Adw.get_major_version()}.{Adw.get_minor_version()}.{Adw.get_micro_version()}\n'
info += f'- WebKitGTK: {WebKit2.get_major_version()}.{WebKit2.get_minor_version()}.{WebKit2.get_micro_version()}\n'
info += f'- WebKitGTK: {WebKit.get_major_version()}.{WebKit.get_minor_version()}.{WebKit.get_micro_version()}\n'
info += f'- Soup: {Soup.get_major_version()}.{Soup.get_minor_version()}.{Soup.get_micro_version()}\n'
info += '\n'

View File

@ -12,13 +12,19 @@ import platform
import requests
import time
gi.require_version('WebKit2', '5.0')
try:
gi.require_version('WebKit', '6.0')
from gi.repository import WebKit
IS_WEBKITGTK6 = True
except ValueError:
gi.require_version('WebKit2', '5.0')
from gi.repository import WebKit2 as WebKit
IS_WEBKITGTK6 = False
from gi.repository import Gio
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import WebKit2
from komikku.servers.exceptions import CfBypassError
from komikku.utils import get_cache_dir
@ -49,17 +55,20 @@ class Webview(Gtk.ScrolledWindow):
self.get_hscrollbar().set_visible(False)
self.get_vscrollbar().set_visible(False)
self.webkit_webview = WebKit2.WebView()
self.webkit_webview = WebKit.WebView()
self.set_child(self.webkit_webview)
self.web_context = self.webkit_webview.get_context()
self.web_context.set_cache_model(WebKit2.CacheModel.DOCUMENT_VIEWER)
self.web_context.set_tls_errors_policy(WebKit2.TLSErrorsPolicy.IGNORE)
self.web_context.get_cookie_manager().set_persistent_storage(
self.webkit_webview.get_context().set_cache_model(WebKit.CacheModel.DOCUMENT_VIEWER)
if IS_WEBKITGTK6:
self.session_or_context = self.webkit_webview.get_network_session()
else:
self.session_or_context = self.webkit_webview.get_context()
self.session_or_context.set_tls_errors_policy(WebKit.TLSErrorsPolicy.IGNORE)
self.session_or_context.get_cookie_manager().set_persistent_storage(
os.path.join(get_cache_dir(), 'WebKitPersistentStorage.sqlite'),
WebKit2.CookiePersistentStorage.SQLITE
WebKit.CookiePersistentStorage.SQLITE
)
self.web_context.get_cookie_manager().set_accept_policy(WebKit2.CookieAcceptPolicy.ALWAYS)
self.session_or_context.get_cookie_manager().set_accept_policy(WebKit.CookieAcceptPolicy.ALWAYS)
self.window.stack.add_named(self, 'webview')
@ -183,7 +192,7 @@ def bypass_cf(func):
nonlocal cf_reload_count
nonlocal error
if event != WebKit2.LoadEvent.REDIRECTED and '__cf_chl_tk' in webview.webkit_webview.get_uri():
if event != WebKit.LoadEvent.REDIRECTED and '__cf_chl_tk' in webview.webkit_webview.get_uri():
# Challenge has been passed
# Disable images auto-load
@ -192,7 +201,7 @@ def bypass_cf(func):
# Exit from webview
webview.navigate_back(None)
if event != WebKit2.LoadEvent.FINISHED:
if event != WebKit.LoadEvent.FINISHED:
return
cf_reload_count += 1
@ -239,7 +248,7 @@ def bypass_cf(func):
return
logger.debug(f'{server.id}: Page loaded, getting cookies...')
cookie_manager = webview.web_context.get_cookie_manager()
cookie_manager = webview.session_or_context.get_cookie_manager()
cookie_manager.get_cookies(server.base_url, None, on_get_cookies_finish, None)
def on_get_cookies_finish(cookie_manager, result, user_data):
@ -321,7 +330,7 @@ def get_page_html(url, user_agent=None, settings=None, wait_js_code=None):
webview.close()
def on_load_changed(_webkit_webview, event):
if event != WebKit2.LoadEvent.FINISHED:
if event != WebKit.LoadEvent.FINISHED:
return
if wait_js_code: