ruby-on-rails 在Mastodon中向Rails 6添加jQuery

kcrjzv8t  于 2023-02-06  发布在  Ruby
关注(0)|答案(1)|浏览(183)
    • bounty将在5天后过期**。回答此问题可获得+50声望奖励。BlueDogRanch希望引起更多人对此问题的关注:如何将jQuery加载到Mastoton?

我放弃了通过Webpacker How do I add jQuery to Mastodon 4.02 (using Rails 6 and Webpacker 4)?将jQuery添加到Mastodon 4.0.2的尝试
因此,现在我只想在Mastodon头文件application.html.haml中添加一个指向jQuery CDN的链接。
我补充说
= javascript_pack_tag "https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"
application.html.haml,然后运行

RAILS_ENV=production bundle exec rake tmp:cache:clear
RAILS_ENV=production bundle exec rails assets:generate_static_pages
RAILS_ENV=production bundle exec rails assets:precompile

然后从root帐户退出并运行
systemctl restart mastodon-*

  • 但Mastodon崩溃并显示应用程序的一般错误屏幕。*
    • 编辑2/1/23**

使用
%script src: "https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"
也会撞上乳齿象。

    • 问题:**
  • ·如何简单地将lnk添加到Mastodon头中的jQuery CDN?*
  • ·我看到的错误是由重新编译Rails引起的吗?*
  • ·哪里有错误日志可以显示更多崩溃信息?*

这是Github上的application.html.hamlhttps://github.com/mastodon/mastodon/blob/a5a00d7f7adff5e0afbd23ac1e1b16120137509a/app/views/layouts/application.html.haml

!!! 5
%html{ lang: I18n.locale }
  %head
    %meta{ charset: 'utf-8' }/
    %meta{ name: 'viewport', content: 'width=device-width, initial-scale=1' }/

    - if cdn_host?
      %link{ rel: 'dns-prefetch', href: cdn_host }/
      %meta{ name: 'cdn-host', content: cdn_host }/

    - if storage_host?
      %link{ rel: 'dns-prefetch', href: storage_host }/

    %link{ rel: 'icon', href: '/favicon.ico', type: 'image/x-icon' }/

    - %w(16 32 48).each do |size|
      %link{ rel: 'icon', sizes: "#{size}x#{size}", href: asset_pack_path("media/icons/favicon-#{size}x#{size}.png"), type: 'image/png' }/

    - %w(57 60 72 76 114 120 144 152 167 180 1024).each do |size|
      %link{ rel: 'apple-touch-icon', sizes: "#{size}x#{size}", href: asset_pack_path("media/icons/apple-touch-icon-#{size}x#{size}.png") }/

    %link{ rel: 'mask-icon', href: asset_pack_path('media/images/logo-symbol-icon.svg'), color: '#6364FF' }/
    %link{ rel: 'manifest', href: manifest_path(format: :json) }/
    %meta{ name: 'theme-color', content: '#191b22' }/
    %meta{ name: 'apple-mobile-web-app-capable', content: 'yes' }/

    %title= content_for?(:page_title) ? safe_join([yield(:page_title).chomp.html_safe, title], ' - ') : title

    = stylesheet_pack_tag 'common', media: 'all', crossorigin: 'anonymous'
    = stylesheet_pack_tag current_theme, media: 'all', crossorigin: 'anonymous'
    = javascript_pack_tag 'common', crossorigin: 'anonymous'
    = javascript_pack_tag "locale_#{I18n.locale}", crossorigin: 'anonymous'

    = csrf_meta_tags
    %meta{ name: 'style-nonce', content: request.content_security_policy_nonce }

    = stylesheet_link_tag '/inert.css', skip_pipeline: true, media: 'all', id: 'inert-style'
    = stylesheet_link_tag custom_css_path, skip_pipeline: true, host: root_url, media: 'all'

    = yield :header_tags

  %body{ class: body_classes }
    = content_for?(:content) ? yield(:content) : yield

    .logo-resources{ 'tabindex' => '-1', 'inert' => true, 'aria-hidden' => true }
      = render_symbol :icon
      = render_symbol :wordmark
tjjdgumg

tjjdgumg1#

设法让mastodon在开发模式下以vagrant运行,但在生产中应该是一样的,你只需要预编译和重新启动。

网络包

$ yarn add jquery
Done in 999.72s # <= at least it worked on the first try

将其添加到插件:

  • 网址:http://www.example.comgithub.com/mastodon/mastodon/blob/v4.0.2/config/webpack/shared.js#L69*
// config/webpack/shared.js

plugins: [
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
  }),
  ...
  • 网址:http://www.example.comwebpack.js.org/plugins/provide-plugin/#usage-jquery*

这意味着在模块中会自动包含$jQuery。application.js中的console.log($);会显示以下内容:

ƒ jQuery(selector, context) {                                 application.js?2e28:4 
      // The jQuery object is actually just the init constructor 'enhanced'
      // Need init if jQuery is called (just allow error to be thrown if not included)
      re…

如果需要,您可以尝试将 * jquery * 添加到窗口:

// app/javascript/application.js

window.$ = window.jQuery = jQuery;
//                         ^ this is automatically given by wepback.ProvidePlugin

现在,模块外有了$jQuery

// app/views/layouts/application.html.haml

:javascript
  console.log($) // works in development

我认为,它会在生产中失败,因为CSP策略,所以没有内联脚本。

内容分发网络

# app/views/layouts/application.html.haml

= javascript_include_tag "https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.min.js"

or

%script{ src: "https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.min.js" }

网络选项卡显示它不工作的原因:

jquery.min.js          (blocked:csp)        script

内容安全策略配置为不接受来自外部主机的脚本,并且仅为样式设置 * nonce *:

  • 一个月一次 *

你正在寻找script_src配置,它在那里的几个地方,我认为更新这一节应该足够了:

if Rails.env.development?
  webpacker_urls = %w(ws http).map { |protocol| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{Webpacker.dev_server.host_with_port}" }

  p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url, *webpacker_urls

  p.script_src  :self, :unsafe_inline, :unsafe_eval, assets_host, host_to_url("cdn.jsdelivr.net")
  # this one worked for me                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  p.child_src   :self, :blob, assets_host
  p.worker_src  :self, :blob, assets_host
else
  p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url

  p.script_src  :self, assets_host, "'wasm-unsafe-eval'", host_to_url("cdn.jsdelivr.net")
  # this one should work for you                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  p.child_src   :self, :blob, assets_host
  p.worker_src  :self, :blob, assets_host
end

主页是explore,如果你点击它在网络选项卡,你可以看到响应标题:

# just showing the relevant part
Content-Security-Policy:
# this is present only in development, that means no inline scripts in production
#                   vvvvvvvvvvvvvvv
  script-src 'self' 'unsafe-inline' 'unsafe-eval' http://mastodon.local http://cdn.jsdelivr.net;
# ^^^^^^^^^^                                                            ^^^^^^^^^^^^^^^^^^^^^^^
  • 一个月一次 *
  • 一对一 *

链轮

将 * jquery * 下载到vendor/assets/javascripts/jquery.js并添加它以进行预编译:

# config/initializers/assets.rb

Rails.application.config.assets.precompile += ["jquery.js"]

现在,您无需再对CSP进行干预:

# app/views/layouts/application.html.haml

= javascript_include_tag "jquery"

相关问题