mirror of https://github.com/mastodon/mastodon
Add ability to include inline javascript (#37459)
parent
9b5986b36e
commit
5b54cd7f76
@ -0,0 +1,23 @@
|
||||
(function (element) {
|
||||
const {userTheme} = element.dataset;
|
||||
|
||||
const colorSchemeMediaWatcher = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
const contrastMediaWatcher = window.matchMedia('(prefers-contrast: more)');
|
||||
|
||||
const updateColorScheme = () => {
|
||||
const useDarkMode = userTheme === 'system' ? colorSchemeMediaWatcher.matches : userTheme !== 'mastodon-light';
|
||||
element.dataset.mode = useDarkMode ? 'dark' : 'light';
|
||||
};
|
||||
|
||||
const updateContrast = () => {
|
||||
const useHighContrast = userTheme === 'contrast' || contrastMediaWatcher.matches;
|
||||
|
||||
element.dataset.contrast = useHighContrast ? 'high' : 'default';
|
||||
}
|
||||
|
||||
colorSchemeMediaWatcher.addEventListener('change', updateColorScheme);
|
||||
contrastMediaWatcher.addEventListener('change', updateContrast);
|
||||
|
||||
updateColorScheme();
|
||||
updateContrast();
|
||||
})(document.documentElement);
|
||||
@ -0,0 +1,31 @@
|
||||
# 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
|
||||
Loading…
Reference in New Issue