7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Weinre使うならrack-weinre入れとけ

Posted at

weinreはスマホ向けJavaScriptリモートコンソールで、スマホ用のウェブアプリを作る時に凄く便利だ。だけどリモートコンソールを使いたいページに<script>タグを書かなきゃいけない。このとき、接続元から見たアプリのホスト名も意識しないといけない。しかも、必要がなくなったら消さないといけない。これは面倒。

そこでrack-weinreですよ。

rack-weinreはrack middlewareで、適切なホスト名の入った<script>タグを自動的に挿入してくれる。

例えば、下のようなconfig.ruを書いて、

config.ru
require 'sinatra'
require 'rack/weinre'

class Foo < Sinatra::Base
  get '/' do
    <<-HTML
      <html>
        <head></head>
        <body>foo</body>
      </html>
    HTML
  end
end

use Rack::Weinre
run Foo

rackupしてhttp://localhost:9292/を開けばこうなる(空白は調節した)。

<html>
  <head><script src="http://localhost:8080/target/target-script-min.js#anonymous"></script></head>
  <body>foo</body>
</html>

<script>タグをオン/オフするには、:switchオプションでスイッチとなるファイル名を指定する。例えば下のように書いて

use Rack::Weinre, :switch => 'bordeaux'

「bordeaux」という空ファイルをconfig.ruと同じフォルダに作れば<script>タグが入るが、bordeauxを消せば<script>タグも入らなくなる。

また、8080はweinreサーバのデフォルトポートだが、これを変更して使っているようならrack-weinreにも:portオプションを渡せばいい。

use Rack::Weinre, :port => 9090

Railsで使うなら、config/environments/development.rbに下のように書く。オプションは上に書いたのと同じ。

config/environments/develpment.rb
  config.middleware.use 'Rack::Weinre'
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?