从另一个应用程序访问javascript文件

编程入门 行业动态 更新时间:2024-10-10 02:18:05
本文介绍了从另一个应用程序访问javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从另一个应用程序访问我的应用程序的预编译文件.我有一个特定的体系结构.这是一棵简化的树

I'm trying to access to my pre-compiled files of my application from another. I've a specific architecture. Here is a simplified tree

├── app │   ├── assets │   │   ├── javascripts │   │   │   ├── application.coffee │   │   │   ├── my_js_file.coffee ├── my-other-app │   ├── index.html │   ├── javascript │   │   ├── anotherJSFile.js

我想在index.html上加载my_js_file.coffee的编译文件.

I would like to load on index.html the compiled file of my_js_file.coffee.

my-other-app不是Rails应用.它包含一个基本的index.html文件,其中特定的URL重定向在我尝试类似的地方进行

my-other-app isn't a Rails app. It contains a basic index.html file where a specific URL redirect where I try something like:

<script src="myapp/assets/my_js_file.js"></script>

我已经在Apache配置文件中定义了它(这部分是可以的).

I've defined it on an Apache configuration file (this part is ok).

我的问题是我找不到任何访问已编译my_js_file.js文件的方法.访问文件和文件名本身(带有指纹).我该如何解决?

My problem is that I can't find any way to access to a compiled my_js_file.js file. The access to the file and the file name itself (with fingerprint). How could I solve this?

我认为主要问题来自指纹,因为我需要知道它才能动态地在第二个应用程序上调整我的网址.

Edit2: 我找到了一种使用正确的指纹生成动态网址的方法,但是我仍然无法访问已编译的文件(未经授权)

推荐答案

我做到了,但是有点棘手.

I did it but it was a bit tricky.

有2分:

  • js指纹
  • 从Rails应用程序到另一个网页(在该Rails应用程序环境之外)的身份验证令牌
  • 要解决第一点,我必须在部署结束时调用的模型上创建一个方法(使用capistrano),以动态修改我的index.html文件.这是我的操作方式:

    To solve the first point I had to create a method on a model that I call at the end of a deployment (with capistrano) to modify my index.html file dynamically. Here is how I did it:

    #my-other-app/index.html <!doctype html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!-- BEGIN DYNAMIC URI --> <!-- END DYNAMIC URI --> </head> <body > <div id="awesome"> Some stuff here.. </div> </body> </html>

    #myModel.rb def self.generate_index_file js_files = ['my_js_file.js'] tempfile = File.open Rails.root.join('my-other-app/index.tmp'), 'w' f = File.new('my-other-app/index.html') f.each do |line| if line =~ /^.*<!-- BEGIN DYNAMIC URI -->/ tempfile << line js_files.each do |filename| fingerprinted_name = Rails.application.assets.find_asset(filename).digest_path tempfile << "<script src='#{Rails.application.default_url_options[:host]}/assets/" + fingerprinted_name + "?body=1'></script>\n" end else tempfile << line end end f.close tempfile.close FileUtils.mv("my-other-app/index.tmp", "my-other-app/index.html") end

    #config/deploy/production.rb # Available only for Capistrano 2.x namespace :deploy do task :generate_samsung_index, roles: :app do run %Q{cd #{latest_release} && RAILS_ENV=#{rails_env} bundle exec rails runner 'MyModel.generate_index_file'} end end after "deploy:restart", "deploy:generate_samsung_index"

    现在,要解决问题的第二部分(身份验证部分),我需要先将令牌添加到url,然后再将其添加到<head>.这是我的代码:

    Now, to solve the second part of the problem (authentification part), I needed to add token to the url before adding it to the <head>. Here is my code:

    #my_models_controller.rb def my_method redirect_to "myawesomeurl?token=#{form_authenticity_token}" end

    #my-other-app/index.html # On <head> with my previous code <script language="javaScript" type="text/javascript"> meta1 = document.createElement("meta"); meta1.name = "csrf-param"; meta1.content = "authenticity_token"; $("head").append(meta1); token = "token" token_result = new RegExp(token + '=([^&]*)', 'i').exec(window.location.search) meta2 = document.createElement("meta"); meta2.name = "csrf-param"; meta2.content = token_result; $("head").append(meta1); $("head").append(meta2); </script>

    我希望这会对其他人有所帮助.

    I hope this will help someone else.

    更多推荐

    从另一个应用程序访问javascript文件

    本文发布于:2023-11-09 23:37:48,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1573755.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应用程序   文件   javascript

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!