-
Notifications
You must be signed in to change notification settings - Fork 3
/
minimap.rb
55 lines (50 loc) · 1.3 KB
/
minimap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'sinatra'
require 'erubis'
require 'open-uri'
require 'nokogiri'
#templates are just in .
set :views, File.dirname(__FILE__)
get '/' do
@url = request.query_string
erb @url.empty? ? :minimap : :iframe
end
get '/dev' do
@dev = true
@url = request.query_string
erb @url.empty? ? :minimap : :iframe
end
def get_or_post(path, opts={}, &block)
get(path, opts, &block)
post(path, opts, &block)
end
get_or_post '/proxy' do
address = URI.unescape(request.query_string)
if !URI.parse(address).absolute?
address = 'http://' + address
end
base_address = address.split('/')[0..2].join('/')
doc = Nokogiri::HTML(open(address))
elements = doc.css('*')
def absolutify(element, attribute, address)
at = element[attribute]
if at != nil && at[0] == '/'
element[attribute] = address + at
end
end
elements.each do |element|
absolutify(element,'href',base_address)
absolutify(element,'src',base_address)
absolutify(element,'action',base_address)
end
doc.css('a').each do |link|
if link['href'] != nil
link['href'] = '/proxy?' + URI.escape(link['href'])
end
end
doc.css('form').each do |form|
if form['action'] != nil
form['action'] = '/proxy?' + URI.escape(form['action'])
end
end
doc.to_s.gsub('url(/', 'url(' + address + '/')
end