Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb06b9fdf1 |
No files matched your search
@@ -1,22 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ThemeHelper
|
||||
def javascript_inline_tag(path)
|
||||
entry = InlineScriptManager.instance.file(path)
|
||||
|
||||
# Only add hash if we don't allow arbitrary includes already, otherwise it's going
|
||||
# to break the React Tools browser extension or other inline scripts
|
||||
unless Rails.env.development? && request.content_security_policy.dup.script_src.include?("'unsafe-inline'")
|
||||
request.content_security_policy = request.content_security_policy.clone.tap do |policy|
|
||||
values = policy.script_src
|
||||
values << "'sha256-#{entry[:digest]}'"
|
||||
policy.script_src(*values)
|
||||
end
|
||||
end
|
||||
|
||||
content_tag(:script, entry[:contents], type: 'text/javascript')
|
||||
end
|
||||
|
||||
def theme_style_tags(theme)
|
||||
if theme == 'system'
|
||||
''.html_safe.tap do |tags|
|
||||
|
||||
+9
-4
@@ -1,11 +1,16 @@
|
||||
(function (element) {
|
||||
const {colorScheme, contrast} = element.dataset;
|
||||
const { colorScheme, contrast } = element.dataset;
|
||||
|
||||
const colorSchemeMediaWatcher = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
const colorSchemeMediaWatcher = window.matchMedia(
|
||||
'(prefers-color-scheme: dark)',
|
||||
);
|
||||
const contrastMediaWatcher = window.matchMedia('(prefers-contrast: more)');
|
||||
|
||||
const updateColorScheme = () => {
|
||||
const useDarkMode = colorScheme === 'auto' ? colorSchemeMediaWatcher.matches : colorScheme === 'dark';
|
||||
const useDarkMode =
|
||||
colorScheme === 'auto'
|
||||
? colorSchemeMediaWatcher.matches
|
||||
: colorScheme === 'dark';
|
||||
|
||||
element.dataset.colorScheme = useDarkMode ? 'dark' : 'light';
|
||||
};
|
||||
@@ -14,7 +19,7 @@
|
||||
const useHighContrast = contrast === 'high' || contrastMediaWatcher.matches;
|
||||
|
||||
element.dataset.contrast = useHighContrast ? 'high' : 'default';
|
||||
}
|
||||
};
|
||||
|
||||
colorSchemeMediaWatcher.addEventListener('change', updateColorScheme);
|
||||
contrastMediaWatcher.addEventListener('change', updateContrast);
|
||||
@@ -1,31 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'singleton'
|
||||
|
||||
class InlineScriptManager
|
||||
include Singleton
|
||||
include ActionView::Helpers::TagHelper
|
||||
include ActionView::Helpers::JavaScriptHelper
|
||||
|
||||
def initialize
|
||||
@cached_files = {}
|
||||
end
|
||||
|
||||
def file(name)
|
||||
@cached_files[name] ||= load_file(name)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_file(name)
|
||||
path = Pathname.new(name).cleanpath
|
||||
raise ArgumentError, "Invalid inline javascript path: #{path}" if path.to_s.start_with?('..')
|
||||
|
||||
path = Rails.root.join('app', 'javascript', 'inline', path)
|
||||
|
||||
contents = javascript_cdata_section(path.read)
|
||||
digest = Digest::SHA256.base64digest(contents)
|
||||
|
||||
{ contents:, digest: }
|
||||
end
|
||||
end
|
||||
@@ -20,12 +20,12 @@
|
||||
- if use_mask_icon?
|
||||
%link{ rel: 'mask-icon', href: frontend_asset_path('images/logo-symbol-icon.svg'), color: '#6364FF' }/
|
||||
%link{ rel: 'manifest', href: manifest_path(format: :json) }/
|
||||
= javascript_inline_tag 'theme-selection.js'
|
||||
= theme_color_tags color_scheme
|
||||
%meta{ name: 'mobile-web-app-capable', content: 'yes' }/
|
||||
|
||||
%title= html_title
|
||||
|
||||
= vite_typescript_tag 'theme-selection.ts', crossorigin: 'anonymous'
|
||||
= theme_style_tags current_theme
|
||||
= vite_client_tag
|
||||
= vite_react_refresh_tag
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
!!!
|
||||
%html{ lang: I18n.locale }
|
||||
%html{ lang: I18n.locale, 'data-contrast': 'auto', 'data-color-scheme': 'auto' }
|
||||
%head
|
||||
%meta{ 'content' => 'text/html; charset=UTF-8', 'http-equiv' => 'Content-Type' }/
|
||||
%meta{ charset: 'utf-8' }/
|
||||
@@ -8,6 +8,7 @@
|
||||
= vite_client_tag
|
||||
= vite_react_refresh_tag
|
||||
= vite_polyfills_tag
|
||||
= vite_typescript_tag 'theme-selection.ts', crossorigin: 'anonymous'
|
||||
= theme_style_tags Setting.default_settings['theme']
|
||||
= vite_typescript_tag 'error.ts', crossorigin: 'anonymous'
|
||||
%body.error
|
||||
|
||||
@@ -32,7 +32,7 @@ RSpec.describe 'Content-Security-Policy' do
|
||||
img-src 'self' data: blob: #{local_domain}
|
||||
manifest-src 'self' #{local_domain}
|
||||
media-src 'self' data: #{local_domain}
|
||||
script-src 'self' #{local_domain} 'wasm-unsafe-eval' 'sha256-Z5KW83D+6/pygIQS3h9XDpF52xW3l3BHc7JL9tj3uMs='
|
||||
script-src 'self' #{local_domain} 'wasm-unsafe-eval'
|
||||
style-src 'self' #{local_domain} 'nonce-ZbA+JmE7+bK8F5qvADZHuQ=='
|
||||
worker-src 'self' blob: #{local_domain}
|
||||
CSP
|
||||
|
||||
Reference in new issue
Block a user