mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Move dark mode script generation into own function
This commit is contained in:
parent
a4672db7bc
commit
26b8d40b12
@ -235,6 +235,41 @@ def uniq(vals):
|
|||||||
return tuple(x for x in vals if x not in seen and not seen_add(x))
|
return tuple(x for x in vals if x not in seen and not seen_add(x))
|
||||||
|
|
||||||
|
|
||||||
|
def create_dark_mode_script():
|
||||||
|
dark_mode_css = P('dark_mode.css', data=True, allow_user_override=False).decode('utf-8')
|
||||||
|
return create_script('dark-mode.js', '''
|
||||||
|
(function() {
|
||||||
|
var settings = JSON.parse(navigator.userAgent.split('|')[1]);
|
||||||
|
var dark_css = CSS;
|
||||||
|
|
||||||
|
function apply_body_colors(event) {
|
||||||
|
if (document.documentElement) {
|
||||||
|
if (settings.bg) document.documentElement.style.backgroundColor = settings.bg;
|
||||||
|
if (settings.fg) document.documentElement.style.color = settings.fg;
|
||||||
|
}
|
||||||
|
if (document.body) {
|
||||||
|
if (settings.bg) document.body.style.backgroundColor = settings.bg;
|
||||||
|
if (settings.fg) document.body.style.color = settings.fg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function apply_css() {
|
||||||
|
var css = '';
|
||||||
|
if (settings.link) css += 'html > body :link, html > body :link * { color: ' + settings.link + ' !important; }';
|
||||||
|
if (settings.is_dark_theme) { css += dark_css; }
|
||||||
|
var style = document.createElement('style');
|
||||||
|
style.textContent = css;
|
||||||
|
document.documentElement.appendChild(style);
|
||||||
|
apply_body_colors();
|
||||||
|
}
|
||||||
|
|
||||||
|
apply_body_colors();
|
||||||
|
document.addEventListener("DOMContentLoaded", apply_css);
|
||||||
|
})();
|
||||||
|
'''.replace('CSS', json.dumps(dark_mode_css), 1),
|
||||||
|
injection_point=QWebEngineScript.InjectionPoint.DocumentCreation)
|
||||||
|
|
||||||
|
|
||||||
def create_profile():
|
def create_profile():
|
||||||
ans = getattr(create_profile, 'ans', None)
|
ans = getattr(create_profile, 'ans', None)
|
||||||
if ans is None:
|
if ans is None:
|
||||||
@ -246,42 +281,11 @@ def create_profile():
|
|||||||
compile_editor()
|
compile_editor()
|
||||||
js = P('editor.js', data=True, allow_user_override=False)
|
js = P('editor.js', data=True, allow_user_override=False)
|
||||||
cparser = P('csscolorparser.js', data=True, allow_user_override=False)
|
cparser = P('csscolorparser.js', data=True, allow_user_override=False)
|
||||||
dark_mode_css = P('dark_mode.css', data=True, allow_user_override=False).decode('utf-8')
|
|
||||||
|
|
||||||
insert_scripts(ans,
|
insert_scripts(ans,
|
||||||
create_script('csscolorparser.js', cparser),
|
create_script('csscolorparser.js', cparser),
|
||||||
create_script('editor.js', js),
|
create_script('editor.js', js),
|
||||||
create_script('dark-mode.js', '''
|
create_dark_mode_script(),
|
||||||
(function() {
|
|
||||||
var settings = JSON.parse(navigator.userAgent.split('|')[1]);
|
|
||||||
var dark_css = CSS;
|
|
||||||
|
|
||||||
function apply_body_colors(event) {
|
|
||||||
if (document.documentElement) {
|
|
||||||
if (settings.bg) document.documentElement.style.backgroundColor = settings.bg;
|
|
||||||
if (settings.fg) document.documentElement.style.color = settings.fg;
|
|
||||||
}
|
|
||||||
if (document.body) {
|
|
||||||
if (settings.bg) document.body.style.backgroundColor = settings.bg;
|
|
||||||
if (settings.fg) document.body.style.color = settings.fg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function apply_css() {
|
|
||||||
var css = '';
|
|
||||||
if (settings.link) css += 'html > body :link, html > body :link * { color: ' + settings.link + ' !important; }';
|
|
||||||
if (settings.is_dark_theme) { css += dark_css; }
|
|
||||||
var style = document.createElement('style');
|
|
||||||
style.textContent = css;
|
|
||||||
document.documentElement.appendChild(style);
|
|
||||||
apply_body_colors();
|
|
||||||
}
|
|
||||||
|
|
||||||
apply_body_colors();
|
|
||||||
document.addEventListener("DOMContentLoaded", apply_css);
|
|
||||||
})();
|
|
||||||
'''.replace('CSS', json.dumps(dark_mode_css), 1),
|
|
||||||
injection_point=QWebEngineScript.InjectionPoint.DocumentCreation)
|
|
||||||
)
|
)
|
||||||
url_handler = UrlSchemeHandler(ans)
|
url_handler = UrlSchemeHandler(ans)
|
||||||
ans.installUrlSchemeHandler(QByteArray(FAKE_PROTOCOL.encode('ascii')), url_handler)
|
ans.installUrlSchemeHandler(QByteArray(FAKE_PROTOCOL.encode('ascii')), url_handler)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user