Class: JSCommander::URLForwarder

Inherits:
WEBrick::HTTPServer
  • Object
show all
Defined in:
lib/jscmd/urlforwarder.rb

Instance Method Summary collapse

Constructor Details

#initialize(broker, args) ⇒ URLForwarder

Returns a new instance of URLForwarder.



5
6
7
8
9
10
# File 'lib/jscmd/urlforwarder.rb', line 5

def initialize(broker, args)
  @broker = broker
  super(args)
  mount_proc("/send/", method(:forward_url).to_proc)
  mount_proc("/", method(:show_bookmarklet).to_proc)
end

Instance Method Details

#forward_url(req, res) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/jscmd/urlforwarder.rb', line 34

def forward_url(req, res)
  uri = CGI.unescape(req.unparsed_uri.sub(%r{^/send/}, ""))
  puts "opening " + uri
  line = "(function() { var c = location.href; var l = '#{uri.gsub(/\\/, "\\\\").gsub("'", "\\'")}'; location.href = l; if (l.replace(/#.*$/,'') == c.replace(/#.*$/,'')) location.reload(); })()"
  @broker.send("commands", Message.new(line, :type => "eval"))
  res.content_type = "text/html"
  res.body = '<html><head><script type="text/javascript">window.close()</script></head></html>'
end

#show_bookmarklet(req, res) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jscmd/urlforwarder.rb', line 12

def show_bookmarklet(req, res)
  res.content_type = "text/html"
  res.body = <<EOS
<html>
<head>
<title>Bookmarklet for forwarding URL</title>
<script type="text/javascript">
function setBrowserName(name) {
  var link = document.getElementById("link");
  link.removeChild(link.firstChild);
  link.appendChild(document.createTextNode("Open with " + name));
}
</script>
</head>
<body>
<form onsubmit="return false">Enter the name of another browser: <input type="text" value="Wii" oninput="setBrowserName(this.value)"/></form>
<p>Drag the following link to your bookmarks toolbar.</p>
<p><a id="link" href="javascript:void(window.open('http://localhost:#{@config[:Port]}/send/'+escape(location.href)))">Open with Wii</a></p>
</body>
EOS
end