从另一个应用程序访问javascript文件 [英] To access javascript files from another app
问题描述
我正在尝试从另一个应用程序访问我的应用程序的预编译文件.我有一个特定的体系结构.这是一棵简化的树
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="http://myapp.com/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 "http://myawesomeurl.com?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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!